#!/bin/bash
# GhostWriter — Stealth Human Typer
# Double-click to build & run

cd "$(dirname "$0")"

echo "================================================"
echo "  GhostWriter — Stealth Human Typer"
echo "================================================"
echo ""

# Check for Accessibility permission
if ! osascript -e 'tell application "System Events" to keystroke ""' &>/dev/null 2>&1; then
    echo "[!] Accessibility permission may be needed."
    echo "    System Settings > Privacy & Security > Accessibility"
    echo "    Add Terminal (or iTerm) to the list."
    echo ""
fi

# Build if needed
BINARY="./ghostwriterd"
if [ ! -f "$BINARY" ] || [ "ghostwriter.swift" -nt "$BINARY" ]; then
    echo "[*] Compiling ghostwriter.swift..."
    swiftc -O -o "$BINARY" ghostwriter.swift -framework Cocoa -framework Carbon 2>&1
    if [ $? -ne 0 ]; then
        echo ""
        echo "[ERROR] Compilation failed. Check the output above."
        echo "Press any key to exit..."
        read -n 1
        exit 1
    fi
    echo "[*] Build successful."
else
    echo "[*] Binary up to date."
fi

echo "[*] Starting GhostWriter daemon..."
echo "[*] UI will open at http://127.0.0.1:9753"
echo ""
echo "    Ctrl/Cmd+Shift+G     = Type next in queue (or clipboard)"
echo "    Ctrl/Cmd+Shift+Space = Pause / Resume typing"
echo "    Escape               = Emergency stop"
echo ""
echo "    Close this window or Ctrl+C to quit."
echo "------------------------------------------------"
echo ""

# Run
"$BINARY"
