#!/bin/sh
# >>> aegis pre-commit >>>
# Installed by aegis system hook install
# aegis-hook-format: 4
REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
if [ ! -d "$REPO_ROOT/.aegis" ] && [ ! -d "$REPO_ROOT/.soma" ]; then
  exit 0
fi
if command -v aegis >/dev/null 2>&1; then
  exec aegis check --hook
elif command -v soma >/dev/null 2>&1; then
  exec soma check --hook
elif [ -n "$VIRTUAL_ENV" ] && [ -x "$VIRTUAL_ENV/bin/python" ] && "$VIRTUAL_ENV/bin/python" -c 'import aegis_cli' >/dev/null 2>&1; then
  exec "$VIRTUAL_ENV/bin/python" -m aegis_cli check --hook
elif [ -n "$VIRTUAL_ENV" ] && [ -x "$VIRTUAL_ENV/Scripts/python.exe" ] && "$VIRTUAL_ENV/Scripts/python.exe" -c 'import aegis_cli' >/dev/null 2>&1; then
  exec "$VIRTUAL_ENV/Scripts/python.exe" -m aegis_cli check --hook
elif [ -x "$PWD/.venv/bin/python" ] && "$PWD/.venv/bin/python" -c 'import aegis_cli' >/dev/null 2>&1; then
  exec "$PWD/.venv/bin/python" -m aegis_cli check --hook
elif [ -x "$PWD/.venv/Scripts/python.exe" ] && "$PWD/.venv/Scripts/python.exe" -c 'import aegis_cli' >/dev/null 2>&1; then
  exec "$PWD/.venv/Scripts/python.exe" -m aegis_cli check --hook
elif command -v python3 >/dev/null 2>&1 && python3 -c 'import sys; sys.exit(0)' 2>/dev/null && python3 -c 'import aegis_cli' >/dev/null 2>&1; then
  exec python3 -m aegis_cli check --hook
elif [ -n "$VIRTUAL_ENV" ] && [ -x "$VIRTUAL_ENV/bin/python" ] && "$VIRTUAL_ENV/bin/python" -c 'import soma_cli' >/dev/null 2>&1; then
  exec "$VIRTUAL_ENV/bin/python" -m soma_cli check --hook
elif python3 -c 'import soma_cli' >/dev/null 2>&1; then
  exec python3 -m soma_cli check --hook
else
  echo "aegis pre-commit: cannot execute quality checks." >&2
  echo "  Neither 'aegis' nor a Python interpreter with 'aegis_cli' installed was found on PATH." >&2
  echo "  Run 'aegis system hook status' or 'aegis system doctor' to diagnose environment resolution." >&2
  exit 1
fi
# <<< aegis pre-commit <<<
