#!/bin/sh
# Blocks a commit when tools/scan_secrets.py finds a secret or personal data in the
# tracked, staged or untracked files or in the history. It prints only MATCH lines or CLEAN.
#
# Turn it on once per clone:  git config core.hooksPath .githooks
# The scanner reads your printer access code from the OS keychain in its own process only.
# Set BAMBUTLER_NO_KEYCHAIN=1 to skip that (the pattern checks still run).

repo=$(git rev-parse --show-toplevel) || exit 1
python=${PYTHON:-python3}
if ! command -v "$python" >/dev/null 2>&1; then
    echo "pre-commit: $python not found; cannot run tools/scan_secrets.py" >&2
    exit 1
fi
"$python" "$repo/tools/scan_secrets.py" --repo "$repo"
status=$?
if [ "$status" -ne 0 ]; then
    echo "pre-commit: tools/scan_secrets.py blocked this commit (exit $status)." >&2
    echo "pre-commit: remove each MATCH above, then commit again." >&2
    exit 1
fi
exit 0
