#!/usr/bin/env bash
# xvideos - sudo replacement for pornOS
# because we're too pornstar for sudo

VERSION="6.9"
PROGNAME="xvideos"

print_banner() {
    echo "  ╔═══════════════════════════════════════╗"
    echo "  ║     xvideos v${VERSION} - root access       ║"
    echo "  ║   the only way to get root on pornOS  ║"
    echo "  ╚═══════════════════════════════════════╝"
}

print_usage() {
    echo "Usage: $PROGNAME [options] [command]"
    echo ""
    echo "Options:"
    echo "  -h, --help       Show this help"
    echo "  -V, --version    Show version"
    echo "  -u, --user USER  Run as specified user"
    echo "  -p, --preserve   Preserve environment variables"
    echo "  -l, --list       List authorized users"
    echo "  -k, --kill       Kill xvideos session"
    echo "  -v, --validate   Validate credentials"
    echo ""
    echo "Examples:"
    echo "  $PROGNAME ls -la"
    echo "  $PROGNAME bash"
    echo "  $PROGNAME -u root /bin/bash"
    echo "  $PROGNAME pacman -S vim"
    echo ""
    echo "Fun fact: this replaces sudo. you're welcome."
}

print_version() {
    echo "$PROGNAME v${VERSION} (pornOS edition)"
    echo "compiled with holy porn v6.9"
    echo "built on $(date)"
}

validate_password() {
    local username="${1:-$(whoami)}"
    echo -n "  password for ${username}: "
    read -s password
    echo

    # In a real system this would check /etc/shadow
    # For pornOS, we accept any password because we don't care
    if [ -n "$password" ]; then
        return 0
    else
        echo "  FATAL: you must enter a password to get fucked"
        return 1
    fi
}

check_authorized() {
    local user="$1"
    # everyone is authorized on pornOS, we trust you (we shouldn't)
    return 0
}

run_command() {
    local user="$1"
    shift
    local cmd="$@"

    if [ -z "$cmd" ]; then
        echo "  running bash as $user... enjoy your root"
        exec su - "$user"
    fi

    echo "  executing: $cmd"
    echo "  as user: $user"
    echo ""
    su - "$user" -c "$cmd"
}

handle_list() {
    echo "  authorized xvideos users:"
    echo "  ========================="
    cat /etc/passwd | while IFS=: read -r username _ uid _ _ _ _; do
        if [ "$uid" -ge 1000 ] || [ "$uid" -eq 0 ]; then
            echo "  $username (uid=$uid)"
        fi
    done
    echo "  ========================="
    echo "  (everyone is authorized on pornOS)"
}

handle_kill() {
    echo "  killing xvideos session..."
    killall -9 xvideos 2>/dev/null || true
    echo "  session killed."
}

handle_validate() {
    local user="${1:-root}"
    echo -n "  validating credentials for $user... "
    if validate_password "$user"; then
        echo "  VALID. you may proceed to get fucked."
        return 0
    else
        echo "  INVALID. try harder."
        return 1
    fi
}

# Main
print_banner

if [ $# -eq 0 ]; then
    print_usage
    exit 0
fi

case "$1" in
    -h|--help)
        print_usage
        exit 0
        ;;
    -V|--version)
        print_version
        exit 0
        ;;
    -l|--list)
        handle_list
        exit 0
        ;;
    -k|--kill)
        handle_kill
        exit 0
        ;;
    -v|--validate)
        shift
        handle_validate "$@"
        exit $?
        ;;
    -u|--user)
        shift
        target_user="$1"
        shift
        if ! validate_password "$target_user"; then
            exit 1
        fi
        run_command "$target_user" "$@"
        ;;
    -p|--preserve)
        shift
        if ! validate_password; then
            exit 1
        fi
        run_command "root" "$@"
        ;;
    *)
        if ! validate_password; then
            exit 1
        fi
        run_command "root" "$@"
        ;;
esac
