#!/usr/bin/env bash
# AgentCharter deterministic validation hook. Installed by: agentcharter install-hooks
set -euo pipefail

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

# The interpreter that installed this hook is tried first. This hook used to run a bare
# `python`, which resolves to whatever is first on PATH -- and agentcharter is normally
# installed into a virtualenv, so that is usually a different interpreter with no
# agentcharter in it. Every commit then failed with "No module named agentcharter":
# the hook bricked the repo instead of validating it.
PY=""
for candidate in "@AGENTCHARTER_PYTHON@" python3 python py; do
    [ -n "$candidate" ] || continue
    if "$candidate" -c 'import agentcharter' >/dev/null 2>&1; then
        PY="$candidate"
        break
    fi
done

# Fail open, loudly. A missing interpreter means the check did not happen, which is not the
# same as a policy violation -- and a guard that bricks the repo is worse than no guard.
if [ -z "$PY" ]; then
    echo "agentcharter: no interpreter found that can import agentcharter; validation skipped." >&2
    echo "agentcharter: install it, then re-run 'agentcharter install-hooks'." >&2
    exit 0
fi

"$PY" -m agentcharter validate "$REPO_ROOT"
