#!/usr/bin/env sh
set -eu

script_path=$0
while [ -L "$script_path" ]; do
    script_dir=$(CDPATH= cd -- "$(dirname -- "$script_path")" && pwd)
    script_path=$(readlink "$script_path")
    case "$script_path" in
        /*) ;;
        *) script_path="$script_dir/$script_path" ;;
    esac
done
script_dir=$(CDPATH= cd -- "$(dirname -- "$script_path")" && pwd)
global_app="${RAROCUA_APP:-/Applications/RaroCUA.app}"
global_service="${RAROCUA_SERVICE:-$global_app/Contents/MacOS/RaroCUAService}"
bundled_app="$script_dir/../vendor/RaroCUA.app"
bundled_service="$bundled_app/Contents/MacOS/RaroCUAService"

install_app() {
    case "$global_app" in
        /*/RaroCUA.app) ;;
        *) echo "raro-cua-mcp: install target must be an absolute path ending in /RaroCUA.app" >&2; exit 2 ;;
    esac
    case "$global_app" in
        */../*|*/./*) echo "raro-cua-mcp: install target must not contain . or .. path segments" >&2; exit 2 ;;
    esac
    if [ ! -x "$bundled_service" ]; then
        echo "raro-cua-mcp: this package does not include vendor/RaroCUA.app" >&2
        exit 127
    fi
    codesign --verify --deep --strict "$bundled_app"
    staging=$(mktemp -d "$(dirname -- "$global_app")/.raro-cua-install.XXXXXX")
    cp -R "$bundled_app" "$staging/RaroCUA.app"
    codesign --verify --deep --strict "$staging/RaroCUA.app"
    if [ -e "$global_app" ] || [ -L "$global_app" ]; then
        mv "$global_app" "$staging/Previous-RaroCUA.app"
        echo "previous app backup: $staging/Previous-RaroCUA.app"
    fi
    if ! mv "$staging/RaroCUA.app" "$global_app"; then
        if [ -e "$staging/Previous-RaroCUA.app" ]; then mv "$staging/Previous-RaroCUA.app" "$global_app"; fi
        echo "raro-cua-mcp: installation failed; previous app restored when available" >&2
        exit 1
    fi
    codesign --verify --deep --strict "$global_app"
    echo "installed app: $global_app"
    echo "checking first-run permissions (macOS requires your approval)..."
    if ! "$global_service" permissions; then
        echo "finish setup in the Privacy & Security pane that was opened, then reconnect your MCP client"
    fi
}

if [ "${1:-}" = "install" ]; then
    install_app
    exit 0
fi

if [ "${1:-}" = "--version" ] || [ "${1:-}" = "version" ] || [ "${1:-}" = "-v" ]; then
    echo "raro-cua-mcp npm package: $(/usr/bin/plutil -extract version raw -o - "$script_dir/../package.json" 2>/dev/null || echo unknown)"
    if [ -x "$global_service" ]; then
        "$global_service" version
    elif [ -x "$bundled_service" ]; then
        "$bundled_service" version
    else
        echo "installed app: none ($global_app)"
    fi
    exit 0
fi

if [ -x "$global_service" ]; then
    service="$global_service"
elif [ -x "$bundled_service" ]; then
    service="$bundled_service"
else
    cat >&2 <<'EOF'
raro-cua-mcp: RaroCUA.app is not installed.

Install the app from the npm package:
  npx -y @mectro/raro-cua install

Or install a release build manually, then make sure this exists:
  /Applications/RaroCUA.app/Contents/MacOS/RaroCUAService

After installation, grant Accessibility and Screen Recording permissions to RaroCUA.app.
EOF
    exit 127
fi

if [ "$#" -eq 0 ]; then
    exec "$service" mcp
fi
exec "$service" "$@"
