#!/bin/sh

CONFIG_FILE="/var/mobile/.config/cmdlog/cmdlog.conf"
LOG_FILE="/var/mobile/log.txt"
CLIPBOARD_ENABLED=1

if [ -f "$CONFIG_FILE" ]; then
    . "$CONFIG_FILE"
fi

show_help() {
    echo "Usage: cmdlog [OPTIONS] <command>"
    echo
    echo "Description:"
    echo "  Runs a command and captures both the command line and its output."
    echo "  The result is displayed, saved to log file, and optionally copied to clipboard."
    echo
    echo "Options:"
    echo "  -h, --help             Show this help message"
    echo "  -s, --set-log PATH     Set permanent log file location"
    echo "  -t, --toggle-clip      Toggle clipboard functionality"
    echo "  -c, --show-config      Show current configuration"
    echo
    echo "Examples:"
    echo "  cmdlog echo 'Hello World'"
    echo "  cmdlog -s /var/mobile/Documents/cmdlog.txt"
    echo "  cmdlog -t"
    echo
    echo "\033[1;36mCurrent Configuration:"
    echo "  \033[0mLog Location: \033[1;33m$LOG_FILE"
    echo "  \033[0mClipboard: $([ "$CLIPBOARD_ENABLED" = "1" ] && echo "\033[1;32mON\033[0m" || echo "\033[1;31mOFF\033[0m")"
    echo ""
    exit 0
}

save_config() {
    mkdir -p "$(dirname "$CONFIG_FILE")"
    echo "LOG_FILE=\"$LOG_FILE\"" > "$CONFIG_FILE"
    echo "CLIPBOARD_ENABLED=$CLIPBOARD_ENABLED" >> "$CONFIG_FILE"
}

show_config() {
    echo ""
    echo "\033[1;36mCurrent Configuration:"
    echo "  \033[0mLog Location: \033[1;33m$LOG_FILE"
    echo "  \033[0mClipboard: $([ "$CLIPBOARD_ENABLED" = "1" ] && echo "\033[1;32mON\033[0m" || echo "\033[1;31mOFF\033[0m")"
    echo ""
    exit 0
}

validate_path() {
    local path="$1"
    
    if [ ! -d "$(dirname "$path")" ]; then
        return 1
    fi
    
    if ! echo "$path" | grep -q "cmdlog.txt$"; then
        return 1
    fi
    
    return 0
}

while [ $# -gt 0 ]; do
    case "$1" in
        -h|--help)
            show_help
            ;;
        -s|--set-log)
            if [ -z "$2" ]; then
                echo ""
                echo "  \033[1;31mError: Log path not provided!"
                echo ""
                exit 1
            fi
            
            orig_path="$2"
            if [ "$#" -gt 2 ]; then
                echo ""
                echo "  \033[1;31mError: Path contains spaces. Please wrap the path in quotes."
                echo ""
                exit 1
            fi
            
            path="$2"
            if [ -d "$path" ]; then
                path="${path%/}/cmdlog.txt"
            fi
            
            if ! validate_path "$path"; then
								echo ""
                echo "  \033[1;31mError: Invalid path."
								echo ""
                exit 1
            fi
            
            LOG_FILE="$path"
            save_config
            echo ""
            echo "  \033[1;32mSuccessfully set log location to: \033[1;33m$path"
            echo ""
            exit 0
            ;;
        -t|--toggle-clip)
            CLIPBOARD_ENABLED=$((1 - CLIPBOARD_ENABLED))
            save_config
            if [ "$CLIPBOARD_ENABLED" = "1" ]; then
                echo ""
                echo "  \033[0mClipboard: \033[1;32mON\033[0m"
                echo ""
            else
                echo ""
                echo "  \033[0mClipboard: \033[1;31mOFF\033[0m"
                echo ""
            fi
            exit 0
            ;;
        -c|--show-config)
            show_config
            ;;
        -*)
            echo ""
						echo "  \033[1;31mError: Invalid option '$1'. Use -h for help."
						echo ""
            exit 1
            ;;
        *)
            first_arg=$(echo "$1" | cut -d' ' -f1)
            if ! command -v "$first_arg" >/dev/null 2>&1; then
                echo ""
                echo "  \033[1;31mError: Command must be used with proper arguments. Use -h for help."
                echo ""
                exit 1
            fi
            break
            ;;
    esac
    shift
done

if [ $# -eq 0 ]; then
		echo ""
    echo "  \033[1;31mError: No command provided. Use -h for help."
		echo ""
    exit 1
fi

user=$(whoami)
dir=$(pwd)
device_name=$(uname -n 2>/dev/null)
if [ -z "$device_name" ]; then
    device_name="iOS-Device"
fi

log_dir=$(dirname "$LOG_FILE")
mkdir -p "$log_dir"

output=$(printf "%s@%s[%s]# %s\n" "$user" "$device_name" "$dir" "$*"; eval "$*" 2>&1)
echo "$output"
echo "$output" > "$LOG_FILE"

if [ "$CLIPBOARD_ENABLED" = "1" ]; then
    echo "$output" | pbcopy
fi