#!/bin/bash
# install-hotkey.sh — Compile and install CTM global hotkey daemon
# Usage: bash install-hotkey.sh [install|uninstall|status]
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
BINARY="$HOME/.local/bin/ctm-hotkey"
PLIST_NAME="com.ctm.hotkey"
PLIST="$HOME/Library/LaunchAgents/${PLIST_NAME}.plist"
SOURCE="$SCRIPT_DIR/ctm-hotkey.swift"

case "${1:-install}" in
  install)
    echo "=== CTM Global Hotkey Installer ==="
    echo ""

    # 1. Compile
    echo "[1/3] Compiling ctm-hotkey..."
    mkdir -p "$(dirname "$BINARY")"
    swiftc -O -o "$BINARY" "$SOURCE" -framework Cocoa
    chmod +x "$BINARY"
    echo "      Binary: $BINARY ($(du -h "$BINARY" | cut -f1 | xargs))"

    # 2. Create launchd plist
    echo "[2/3] Installing launchd agent..."
    mkdir -p "$(dirname "$PLIST")"
    cat > "$PLIST" <<PLISTEOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>${PLIST_NAME}</string>
  <key>ProgramArguments</key>
  <array>
    <string>${BINARY}</string>
  </array>
  <key>EnvironmentVariables</key>
  <dict>
    <key>CTM_PORT</key>
    <string>3456</string>
  </dict>
  <key>RunAtLoad</key>
  <true/>
  <key>KeepAlive</key>
  <true/>
  <key>StandardOutPath</key>
  <string>${HOME}/.local/log/ctm-hotkey.log</string>
  <key>StandardErrorPath</key>
  <string>${HOME}/.local/log/ctm-hotkey.log</string>
</dict>
</plist>
PLISTEOF

    mkdir -p "$HOME/.local/log"

    # 3. Load the agent
    echo "[3/3] Starting daemon..."
    launchctl unload "$PLIST" 2>/dev/null || true
    launchctl load "$PLIST"

    echo ""
    echo "=== Installed! ==="
    echo "  Hotkey:  Cmd+Shift+S (global, works in any app)"
    echo "  Action:  Triggers CTM screenshot + annotation editor"
    echo "  Daemon:  Auto-starts on login"
    echo "  Log:     ~/.local/log/ctm-hotkey.log"
    echo ""
    echo "IMPORTANT: You need to grant Accessibility permission:"
    echo "  System Settings > Privacy & Security > Accessibility"
    echo "  Add: $BINARY"
    echo ""
    echo "To uninstall: bash $0 uninstall"
    ;;

  uninstall)
    echo "=== CTM Global Hotkey Uninstaller ==="
    if [ -f "$PLIST" ]; then
      echo "Stopping daemon..."
      launchctl unload "$PLIST" 2>/dev/null || true
      rm -f "$PLIST"
      echo "Removed: $PLIST"
    fi
    if [ -f "$BINARY" ]; then
      rm -f "$BINARY"
      echo "Removed: $BINARY"
    fi
    echo "Done. Hotkey disabled."
    ;;

  status)
    echo "=== CTM Global Hotkey Status ==="
    if [ -f "$BINARY" ]; then
      echo "Binary:  $BINARY (exists)"
    else
      echo "Binary:  not installed"
    fi
    if [ -f "$PLIST" ]; then
      echo "Plist:   $PLIST (exists)"
    else
      echo "Plist:   not installed"
    fi
    if launchctl list | grep -q "$PLIST_NAME" 2>/dev/null; then
      echo "Daemon:  running"
      PID=$(launchctl list | grep "$PLIST_NAME" | awk '{print $1}')
      echo "PID:     $PID"
    else
      echo "Daemon:  not running"
    fi
    if [ -f "$HOME/.local/log/ctm-hotkey.log" ]; then
      echo "Log (last 3 lines):"
      tail -3 "$HOME/.local/log/ctm-hotkey.log" | sed 's/^/  /'
    fi
    ;;

  *)
    echo "Usage: $0 [install|uninstall|status]"
    exit 1
    ;;
esac
