#!/usr/bin/env bash
# gimme-the-lint: Pre-Commit Hook
# Runs progressive linting on changed files before every commit
# Bypass: git commit --no-verify
#
# gtl-hook-contract: 2
#
# The contract version above is how `gimme-the-lint status` detects that an
# installed hook predates the current engine. Hooks are installed FILES: upgrading
# the package does not rewrite them. Bump this whenever the flags the hook passes
# change, so a stale hook is reported rather than silently under-checking.
#
# Contract 2 (v2.6.0): passes --stage. `commit` runs the fast per-file linters;
# the slower whole-app checks (which must import your application) run on push.

PROJECT_ROOT="$(git rev-parse --show-toplevel)"

# Look for gimme-the-lint scripts in node_modules or local
SCRIPT=""
if [ -f "${PROJECT_ROOT}/node_modules/@theglitchking/gimme-the-lint/scripts/run-checks.sh" ]; then
    SCRIPT="${PROJECT_ROOT}/node_modules/@theglitchking/gimme-the-lint/scripts/run-checks.sh"
elif [ -f "${PROJECT_ROOT}/node_modules/gimme-the-lint/scripts/run-checks.sh" ]; then
    SCRIPT="${PROJECT_ROOT}/node_modules/gimme-the-lint/scripts/run-checks.sh"
elif command -v gimme-the-lint &>/dev/null; then
    # Global install
    GIMME_PROJECT_ROOT="$PROJECT_ROOT" gimme-the-lint check --stage=commit
    exit $?
fi

if [ -z "$SCRIPT" ]; then
    echo "gimme-the-lint: run-checks.sh not found, skipping"
    exit 0
fi

echo "Running pre-commit checks (gimme-the-lint)..."
echo ""

export GIMME_PROJECT_ROOT="$PROJECT_ROOT"

# NOTE: the remediation guidance ("run --fix", "for LLMs: ...") is deliberately
# NOT printed here. `check` already prints it, and only it knows whether the
# linter that failed HAS an autofix. A hardcoded "run --fix" here would be a lie
# for any rule that cannot be auto-fixed — and the reader's next move, when --fix
# changes nothing, is to reach for `baseline`, which grandfathers the defect
# instead of repairing it. Let the engine speak; the hook just relays its verdict.
if bash "$SCRIPT" --stage=commit; then
    echo ""
    echo "Pre-commit checks passed!"
    exit 0
else
    echo ""
    echo "  Bypass (emergency): git commit --no-verify"
    echo ""
    exit 1
fi
