#!/bin/bash
# Installs ThetaCog git hooks: the async cognitive-ingest post-commit, and — with --pre-push (or
# the `npx thetacog-mcp init-hooks` entrypoint) — the README-as-spec ADVISORY governor pre-push.

set -e

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
GIT_HOOK_DIR=".git/hooks"

if [ ! -d ".git" ]; then
    echo "❌ Error: Must be run from the root of a git repository."
    exit 1
fi
mkdir -p "$GIT_HOOK_DIR"

install_hook() {   # <src> <dest-name> <label>
    local src="$1" dest="$GIT_HOOK_DIR/$2" label="$3"
    if [ -f "$dest" ] && ! grep -q "ThetaCog" "$dest" 2>/dev/null; then
        echo "⚠️  A $2 hook already exists at $dest — not overwriting."
        echo "   Merge manually from: $src"
        return 0
    fi
    cp "$src" "$dest"; chmod +x "$dest"
    echo "✅ Installed $label at $dest"
}

install_hook "$DIR/../hooks/post-commit.example" "post-commit" "ThetaCog async-ingest post-commit"

# --pre-push (or `init-hooks`): arm the README-as-spec advisory governor.
if [ "${1:-}" = "--pre-push" ] || [ "${1:-}" = "--all" ]; then
    install_hook "$DIR/../hooks/pre-push.example" "pre-push" "ThetaCog README-as-spec governor (advisory pre-push)"
    echo ""
    echo "🛰️  The governor is armed. On every push it measures code-vs-README-spec (LLM-free, ~1s) and,"
    echo "    on a rupture, prints a 'get to work' investigation prompt to .thetacog/prepush.log."
    echo "    It NEVER blocks the push. Recompute any time: npx thetacog-mcp spec-drift"
fi

echo "   Customize post-commit to point to your preferred LLM CLI (Gemini or Claude)."
