#!/usr/bin/env bash
set -euo pipefail

EXTENSION_ID="${1:-}"
if [[ -z "$EXTENSION_ID" ]]; then
  echo "Usage: ./install_macos.sh <chrome-extension-id>"
  echo "Load the unpacked extension in Chrome, copy its ID, then run this installer."
  exit 1
fi

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
AGENT_PATH="$SCRIPT_DIR/agent.py"
HOST_DIR="$HOME/Library/Application Support/Google/Chrome/NativeMessagingHosts"
HOST_PATH="$HOST_DIR/com.zhenyu.ai_pm_agent.json"

chmod +x "$AGENT_PATH"
mkdir -p "$HOST_DIR"

cat > "$HOST_PATH" <<JSON
{
  "name": "com.zhenyu.ai_pm_agent",
  "description": "Personal AI Assistant local automation agent",
  "path": "$AGENT_PATH",
  "type": "stdio",
  "allowed_origins": [
    "chrome-extension://$EXTENSION_ID/"
  ]
}
JSON

echo "Installed Native Messaging host:"
echo "$HOST_PATH"
