#!/usr/bin/env bash
# Unwire the adom auto-update hook from Claude Code and Codex.
set -euo pipefail
HOOK="${HOME}/.adom/hooks/adom-core-update.sh"
HOOK_CMD="bash $HOOK"

unwire() {
  local path="$1"
  [ -f "$path" ] || return 0
  command -v python3 >/dev/null 2>&1 || return 0
  python3 - "$path" "$HOOK_CMD" <<'PY'
import json, sys
path, cmd = sys.argv[1], sys.argv[2]
try:
    with open(path) as f: d = json.load(f)
except Exception:
    sys.exit(0)
ups = d.get("hooks", {}).get("UserPromptSubmit")
if isinstance(ups, list):
    kept = []
    for g in ups:
        if isinstance(g, dict):
            g["hooks"] = [h for h in g.get("hooks", []) if not (isinstance(h, dict) and h.get("command") == cmd)]
            if g.get("hooks"):
                kept.append(g)
        else:
            kept.append(g)
    d["hooks"]["UserPromptSubmit"] = kept
    with open(path, "w") as f: json.dump(d, f, indent=2)
PY
}

unwire "${HOME}/.claude/settings.json"
unwire "${HOME}/.codex/hooks.json"

# Drop the headless cron trigger too, or uninstall would leave entries pointing at
# a script it just deleted (cron would then log a failure every 30 minutes).
if command -v crontab >/dev/null 2>&1; then
  if crontab -l 2>/dev/null | grep -q "adom-core-update.sh"; then
    { crontab -l 2>/dev/null | grep -v "adom-core-update.sh" || true; } | crontab - 2>/dev/null \
      && echo "  removed the adom-core-update cron entries"
  fi
fi

rm -f "$HOOK"
rm -rf "${HOME}/.claude/skills/adom-hook"
rm -f "${HOME}/.adom/last-update.status" "${HOME}/.adom/last-update.log" "${HOME}/.adom/last-core-check"
echo "Removed adom-hook (unwired from Claude + Codex, cron entries dropped)"
