#!/usr/bin/env bash
# Install the universal pieces of this kit into ~/.claude (global, all projects).
# Additive + non-destructive: backs up any file it would overwrite. Does NOT touch your
# ~/.claude/CLAUDE.md or settings.json — it prints the settings snippet for you to merge.
set -euo pipefail

SRC="$(cd "$(dirname "$0")/.claude" && pwd)"
DEST="$HOME/.claude"
stamp="$(date +%Y%m%d-%H%M%S)"

mkdir -p "$DEST/commands" "$DEST/agents" "$DEST/hooks" "$DEST/skills"

copy() { # $1 = path relative to .claude/
  local rel="$1" from="$SRC/$1" to="$DEST/$1"
  [ -e "$from" ] || return 0
  mkdir -p "$(dirname "$to")"
  if [ -e "$to" ]; then
    cp -R "$to" "$to.bak-$stamp"
    echo "  backed up existing $rel -> $rel.bak-$stamp"
  fi
  cp -R "$from" "$to"
  echo "  installed $rel"
}

echo "Installing universal kit pieces into $DEST ..."
copy "statusline.sh"
copy "hooks/secret-guard.sh"
copy "hooks/format-on-write.sh"
copy "hooks/skill-scout-nudge.sh"
copy "hooks/verify-gate.sh"
copy "hooks/context-watch.sh"
copy "hooks/handoff-inject.sh"
copy "commands/ship.md"
copy "commands/spec.md"
copy "commands/scout.md"
copy "commands/handoff.md"
copy "agents/reviewer.md"
copy "skills/worktree-setup/SKILL.md"
copy "skills/skill-scout"

chmod +x "$DEST/statusline.sh" "$DEST/hooks/"*.sh "$DEST/skills/skill-scout/audit.sh" "$DEST/skills/skill-scout/test/run.sh" 2>/dev/null || true

cat <<'EOF'

Done. Now add these to ~/.claude/settings.json (merge by hand — not auto-edited):

  "statusLine": { "type": "command", "command": "~/.claude/statusline.sh" },
  "hooks": {
    "SessionStart": [ { "hooks": [ { "type": "command", "command": "~/.claude/hooks/handoff-inject.sh" },
                                   { "type": "command", "command": "~/.claude/hooks/skill-scout-nudge.sh" } ] } ],
    "PreToolUse":  [ { "matcher": "Write|Edit|MultiEdit", "hooks": [ { "type": "command", "command": "~/.claude/hooks/secret-guard.sh" } ] } ],
    "PostToolUse": [ { "matcher": "Write|Edit|MultiEdit", "hooks": [ { "type": "command", "command": "~/.claude/hooks/format-on-write.sh" } ] } ],
    "Stop":        [ { "hooks": [ { "type": "command", "command": "~/.claude/hooks/context-watch.sh" },
                                  { "type": "command", "command": "~/.claude/hooks/verify-gate.sh" } ] } ]
  }

/ship /spec /scout /handoff, the reviewer agent, and the worktree-setup + skill-scout skills are now global.
The status line now shows a ctx meter; at 300k tokens run /handoff, then /clear.
EOF
