#!/bin/bash

# Remote Touchpad CLI Wrapper
# Provides easy access to remote-touchpad service

SERVICE_NAME="remote-touchpad"
SERVICE_USER="$USER"

install() {
    echo "Installing remote-touchpad as a user systemd service..."
    
    # Check if Go is installed
    if ! command -v go &> /dev/null; then
        echo "Error: Go is not installed. Please install Go first."
        return 1
    fi
    
    echo "Building remote-touchpad binary..."
    go build -tags portal,uinput,x11 -o remote-touchpad
    
    if [ ! -f "remote-touchpad" ]; then
        echo "Error: Failed to build remote-touchpad binary"
        return 1
    fi
    
    # Setup directories
    mkdir -p ~/.local/bin ~/.config/systemd/user
    
    echo "Installing service files..."
    cp remote-touchpad ~/.local/bin/ || echo "Error: cp to ~/.local/bin failed"
    cp remote-touchpad.service ~/.config/systemd/user/ || echo "Error: cp to ~/.config/systemd/user failed"
    
    echo "Enabling and starting service..."
    systemctl --user daemon-reload
    systemctl --user enable --now ${SERVICE_NAME}.service
    
    echo "✓ remote-touchpad user service installed and started"
    echo ""
    echo "Use './rts status' to check the service"
    echo "Use './rts url' to get the connection URL"
}

start() {
    systemctl --user start ${SERVICE_NAME}.service
    echo "Started remote-touchpad service"
}

stop() {
    systemctl --user stop ${SERVICE_NAME}.service
    echo "Stopped remote-touchpad service"
}

status() {
    systemctl --user status ${SERVICE_NAME}.service --no-pager
}

restart() {
    systemctl --user restart ${SERVICE_NAME}.service
    echo "Restarted remote-touchpad service"
}

url() {
    # Extract URL from service logs
    echo "Fetching remote-touchpad URL..."
    local url=$(journalctl --user -u ${SERVICE_NAME}.service -n 100 --no-pager 2>/dev/null | grep -oE "http://[^ ]+|https://[^ ]+" | tail -1)
    if [ -n "$url" ]; then
        echo "Remote Touchpad URL: $url"
        # Generate QR code if qrencode is available
        if command -v qrencode &> /dev/null; then
            echo "Generating QR code..."
            echo "$url" | qrencode -t ansiutf8
        else
            echo "QR code generation requires qrencode. Install it with:"
            echo "  sudo apt-get install qrencode  # Debian/Ubuntu"
            echo "  sudo dnf install qrencode      # Fedora"
            echo "  brew install qrencode          # macOS"
        fi
    else
        echo "Could not find URL in service logs. Is the service running?"
    fi
}

log() {
    echo "Viewing remote-touchpad service logs..."
    journalctl --user -u ${SERVICE_NAME}.service --no-pager
}

uninstall() {
    echo "Uninstalling remote-touchpad..."
    systemctl --user stop ${SERVICE_NAME}.service
    systemctl --user disable ${SERVICE_NAME}.service
    rm -f ~/.config/systemd/user/${SERVICE_NAME}.service
    rm -f ~/.local/bin/remote-touchpad
    systemctl --user daemon-reload
    echo "✓ remote-touchpad uninstalled"
}

help() {
    echo "Remote Touchpad CLI Wrapper"
    echo ""
    echo "Usage: rts [command]"
    echo ""
    echo "Commands:"
    echo "  install    - Install remote-touchpad as a systemd user service"
    echo "  start      - Start the remote-touchpad service"
    echo "  stop       - Stop the remote-touchpad service"
    echo "  status     - Check service status"
    echo "  restart    - Restart the service"
    echo "  url        - Show the remote-touchpad URL"
    echo "  log        - View service logs"
    echo "  uninstall  - Uninstall the service"
    echo "  help       - Show this help message"
}

# Main command handler
case "$1" in
    install)
        install
        ;;
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status
        ;;
    restart)
        restart
        ;;
    url)
        url
        ;;
    log)
        log
        ;;
    uninstall)
        uninstall
        ;;
    help|--help|-h|"")
        help
        ;;
    *)
        echo "Unknown command: $1"
        help
        exit 1
        ;;
esac
