#!/bin/bash
# Auxmem pre-commit hook. Installed by bootstrap.sh as .git/hooks/pre-commit.
# Validates staged .md files against the frontmatter schema.
# Portable: bash 3.2+ (macOS default /bin/bash).

set -euo pipefail

# transparent sync commits bypass validation; CI is their gate
if [ "${AUXMEM_AUTOSYNC:-${VAULT_AUTOSYNC:-0}}" = "1" ]; then exit 0; fi

AUXMEM_ROOT="$(git rev-parse --show-toplevel)"
VALIDATOR="$AUXMEM_ROOT/.scripts/validate_auxmem.py"

if [ ! -f "$VALIDATOR" ]; then
    echo "pre-commit: validator not found at $VALIDATOR" >&2
    exit 1
fi

staged=()
while IFS= read -r -d '' f; do
    staged+=("$f")
done < <(git diff --cached --name-only -z --diff-filter=ACMR -- '*.md' '72-tasks/todo.txt' '72-tasks/done.txt')

if [ ${#staged[@]} -eq 0 ]; then
    exit 0
fi

if ! python3 "$VALIDATOR" "${staged[@]}"; then
    echo ""
    echo "Commit blocked. Try:  python3 .scripts/validate_auxmem.py --fix --all   for the mechanical fixes,"
    echo "then ask your agent to handle the rest (see docs/FIXING.md), or bypass once with: git commit --no-verify"
    exit 1
fi
