#!/bin/bash
# Installs the WitWiki hooks for Claude Code into the repository you run it in.
#
#   curl -fsSL https://witwiki.app/hooks/install.sh | bash
#
# It writes .claude/hooks/witwiki-hooks.sh and adds three entries to
# .claude/settings.json: a reminder on every prompt, a check before Claude asks
# you a question or searches the web, and a nudge to record what changed when a
# turn ends. Entries already there are left alone, so running it twice is safe.
# It never writes an API key anywhere.
#
# Needs jq and curl. WITWIKI_HOOKS_URL overrides where the hook script comes from.

set -euo pipefail

BASE="${WITWIKI_HOOKS_URL:-https://witwiki.app/hooks}"

fail() { echo "witwiki hooks: $*" >&2; exit 1; }

command -v jq >/dev/null 2>&1 || fail "jq is required (the hooks use it too). Install jq and run this again."
command -v curl >/dev/null 2>&1 || fail "curl is required."

ROOT="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
HOOK="$ROOT/.claude/hooks/witwiki-hooks.sh"
SETTINGS="$ROOT/.claude/settings.json"

if [ -e "$SETTINGS" ] && ! jq -e 'type == "object"' "$SETTINGS" >/dev/null 2>&1; then
  fail "$SETTINGS is not a JSON object. Fix it first; nothing was changed."
fi

mkdir -p "$ROOT/.claude/hooks"
TMP="$(mktemp)"
trap 'rm -f "$TMP"' EXIT

curl -fsSL "$BASE/witwiki-hooks.sh" -o "$TMP" || fail "could not download $BASE/witwiki-hooks.sh"
head -n 1 "$TMP" | grep -q '^#!/bin/bash' || fail "$BASE/witwiki-hooks.sh is not the hook script. Nothing was changed."
cp "$TMP" "$HOOK"
chmod 755 "$HOOK"

[ -e "$SETTINGS" ] || echo '{}' >"$SETTINGS"

# Each entry is added only if no hook for that event already runs the same mode.
jq '
  def cmd(mode): "\"$CLAUDE_PROJECT_DIR/.claude/hooks/witwiki-hooks.sh\" " + mode;
  def has(event; mode):
    [.hooks[event][]?.hooks[]?.command // empty] | any(test("witwiki-hooks\\.sh\"? " + mode + "$"));
  def add(event; mode; entry):
    if has(event; mode) then . else .hooks[event] = ((.hooks[event] // []) + [entry]) end;
  add("UserPromptSubmit"; "remind";
      {hooks: [{type: "command", command: cmd("remind"), timeout: 5}]})
  | add("PreToolUse"; "check-first";
      {matcher: "AskUserQuestion|WebSearch|WebFetch",
       hooks: [{type: "command", command: cmd("check-first"), timeout: 10}]})
  | add("Stop"; "record";
      {hooks: [{type: "command", command: cmd("record"), timeout: 10}]})
' "$SETTINGS" >"$TMP" || fail "could not update $SETTINGS. The hook script was written; settings were not changed."
cp "$TMP" "$SETTINGS"

echo "WitWiki hooks installed in $ROOT"
echo "  .claude/hooks/witwiki-hooks.sh"
echo "  .claude/settings.json (UserPromptSubmit, PreToolUse, Stop)"

if ! jq -e '.mcpServers.witwiki' "$ROOT/.mcp.json" >/dev/null 2>&1; then
  cat <<'MSG'

One thing is missing: this repository has no MCP server named "witwiki".
The hooks tell Claude to call mcp__witwiki__ tools, so the server must have
that name. Connect it at https://witwiki.app/connect, or add this to .mcp.json
and export WITWIKI_API_KEY in your shell:

  {
    "mcpServers": {
      "witwiki": {
        "type": "http",
        "url": "https://api.witwiki.app/mcp",
        "headers": { "Authorization": "Bearer ${WITWIKI_API_KEY}" }
      }
    }
  }
MSG
fi

echo
echo "Commit .claude/ so teammates get the hooks from the checkout. Restart Claude Code, or open /hooks once, to load them."
