#!/usr/bin/env bash
# scripts/install-hooks.sh — install the pre-commit hook for this clone.
#
# Uses lefthook if available (recommended); falls back to a drop-in copy.
#
# Policy: docs/plans/How We Ship — Engineering Process Policy (v1.0).md §5
# Pilot:  docs/plans/2026-05-15-feat-how-we-ship-mp-1202-pilot-plan.md (sub-phase A)

set -euo pipefail

cd "$(dirname "$0")/.."

if command -v lefthook >/dev/null 2>&1; then
    # If core.hooksPath is set to the default location, lefthook refuses to install
    # unless --force is passed. That's a sensible guard against silently overwriting
    # an explicit user setting, but the default-pointing-at-default case is harmless.
    if [ "$(git config --local core.hooksPath 2>/dev/null)" = "$(pwd)/.git/hooks" ]; then
        lefthook install --force
    else
        lefthook install
    fi
    echo "✓ lefthook hooks installed."
else
    echo "lefthook not installed; using drop-in fallback."
    echo "  (Install lefthook for parallel + glob-aware hook execution:"
    echo "   https://github.com/evilmartians/lefthook/releases)"
    install -m 0755 scripts/git-hooks/pre-commit .git/hooks/pre-commit
    echo "✓ Drop-in hook installed at .git/hooks/pre-commit."
fi

echo
echo "Verify with:   git commit --allow-empty -m 'test pre-commit'"
echo "Emergency skip (CI still catches): git commit --no-verify"
echo "Lower PHPStan level for one commit: PHPSTAN_LEVEL=3 git commit -m '…'"
