#!/bin/bash
set -euo pipefail
BIN="$HOME/.arthneura/bin"
mkdir -p "$BIN"
curl -fsSL -o "$BIN/arthneura-mcp" "https://arthneura.com/download/arthneura-mcp"
curl -fsSL -o "$BIN/register-me" "https://arthneura.com/download/register-me"
curl -fsSL -o "$BIN/list-me" "https://arthneura.com/download/list-me"
curl -fsSL -o "$BIN/offer-me" "https://arthneura.com/download/offer-me"
curl -fsSL -o "$BIN/rule-me" "https://arthneura.com/download/rule-me"
curl -fsSL -o "$BIN/decide-me" "https://arthneura.com/download/decide-me"
curl -fsSL -o "$BIN/accept-me" "https://arthneura.com/download/accept-me"
curl -fsSL -o "$BIN/lock-me" "https://arthneura.com/download/lock-me"
curl -fsSL -o "$BIN/offchain-vector-db" "https://arthneura.com/download/offchain-vector-db"
curl -fsSL -o "$BIN/chain-register-me" "https://arthneura.com/download/chain-register-me"
curl -fsSL -o "$BIN/announce" "$BIN/pull" "$BIN/provider" "https://arthneura.com/download/announce"
curl -fsSL -o "$BIN/provider" "https://arthneura.com/download/provider"
curl -fsSL -o "$BIN/pull" "$BIN/provider" "https://arthneura.com/download/pull"
curl -fsSL -o "$BIN/provider" "https://arthneura.com/download/provider"
curl -fsSL -o "$BIN/spec-me" "https://arthneura.com/download/spec-me"
curl -fsSL -o "$BIN/commit-me" "https://arthneura.com/download/commit-me"
curl -fsSL -o "$BIN/decide-loop.sh" "https://arthneura.com/download/decide-loop.sh"
chmod 755 "$BIN/arthneura-mcp" "$BIN/register-me" "$BIN/list-me" "$BIN/offer-me" "$BIN/rule-me" "$BIN/decide-me" "$BIN/accept-me" "$BIN/lock-me" "$BIN/offchain-vector-db" "$BIN/chain-register-me" "$BIN/spec-me" "$BIN/announce" "$BIN/pull" "$BIN/provider" "$BIN/commit-me" "$BIN/decide-loop.sh"
CFG="$HOME/Library/Application Support/Claude/claude_desktop_config.json"
mkdir -p "$(dirname "$CFG")"
python3 - "$CFG" "$BIN" << 'PY'
import json, sys
from pathlib import Path
cfg, bindir = Path(sys.argv[1]), Path(sys.argv[2])
data = {}
if cfg.exists() and cfg.read_text().strip():
    data = json.loads(cfg.read_text())
data.setdefault("mcpServers", {})
data["mcpServers"]["arthneura"] = {
    "command": str(bindir / "arthneura-mcp"),
    "args": [],
    "env": {
        "MARKET_URL": "https://api.arthneura.com",
        "DOOR": "https://id.arthneura.com",
        "REGISTER_BIN": str(bindir / "register-me"),
        "LIST_BIN": str(bindir / "list-me"),
        "OFFER_BIN": str(bindir / "offer-me"),
        "RULE_BIN": str(bindir / "rule-me"),
        "DECIDE_BIN": str(bindir / "decide-me"),
        "ACCEPT_BIN": str(bindir / "accept-me"),
        "LOCK_BIN": str(bindir / "lock-me"),
        "CHAIN_REGISTER_BIN": str(bindir / "chain-register-me"),
        "SPEC_BIN": str(bindir / "spec-me"),
        "COMMIT_BIN": str(bindir / "commit-me"),
        "KEYSTORE_PASS": "dev-passphrase",
    },
}
cfg.write_text(json.dumps(data, indent=2) + "\n")
print("Installed. Quit Claude and open it again.")
print(cfg)
plist = Path.home() / "Library/LaunchAgents/com.arthneura.decide.plist"
plist.parent.mkdir(parents=True, exist_ok=True)
loop = bindir / "decide-loop.sh"
plist.write_text("""<?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>com.arthneura.decide</string>
  <key>ProgramArguments</key>
  <array>
    <string>%s</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>StandardOutPath</key>
  <string>%s</string>
  <key>StandardErrorPath</key>
  <string>%s</string>
  <key>StartInterval</key>
  <integer>60</integer>
</dict>
</plist>
""" % (loop, str(Path.home() / "Library/Logs/arthneura-decide.log"), str(Path.home() / "Library/Logs/arthneura-decide.log")))
print("login agent", plist)
PY

PLIST="$HOME/Library/LaunchAgents/com.arthneura.decide.plist"
mkdir -p "$HOME/Library/Logs"
launchctl bootout "gui/$(id -u)/com.arthneura.decide" 2>/dev/null || true
launchctl load -w "$PLIST"
echo "LaunchAgent loaded (runs every 60s)"

# --- agent-runtime ---
curl -fsSL -o "$BIN/agent-runtime" "https://arthneura.com/download/agent-runtime"
chmod 755 "$BIN/agent-runtime"

PLIST_RT="$HOME/Library/LaunchAgents/com.arthneura.agent-runtime.plist"
mkdir -p "$HOME/.arthneura/logs"
cat > "$PLIST_RT" << PLIST
<?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>com.arthneura.agent-runtime</string>
  <key>ProgramArguments</key>
  <array>
    <string>$BIN/agent-runtime</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>KeepAlive</key>
  <true/>
  <key>StandardOutPath</key>
  <string>$HOME/.arthneura/logs/agent-runtime.log</string>
  <key>StandardErrorPath</key>
  <string>$HOME/.arthneura/logs/agent-runtime.log</string>
</dict>
</plist>
PLIST
launchctl unload "$PLIST_RT" 2>/dev/null || true
launchctl load "$PLIST_RT"
echo "agent-runtime LaunchAgent loaded"
