#!/usr/bin/env sh
# Coditation Sentinel — global pre-commit hook.
# Installed by `sentinel install` at ~/.coditation-sentinel/hooks/ and wired via
# `git config --global core.hooksPath`, so it runs for every repository.

# 1) Run Sentinel's checks on the staged changes.
if command -v sentinel >/dev/null 2>&1; then
  sentinel scan --staged
  sentinel_status=$?
elif command -v python3 >/dev/null 2>&1; then
  python3 -m coditation_sentinel scan --staged
  sentinel_status=$?
else
  echo "Coditation Sentinel: could not find 'sentinel' or 'python3' on PATH; commit allowed." >&2
  sentinel_status=0
fi

if [ "$sentinel_status" -ne 0 ]; then
  exit "$sentinel_status"
fi

# 2) A global core.hooksPath shadows a repo's own .git/hooks. If this repo has its
#    own pre-commit hook, run it too so we don't silently disable husky/pre-commit/etc.
repo_root=$(git rev-parse --show-toplevel 2>/dev/null)
if [ -n "$repo_root" ]; then
  local_hook="$repo_root/.git/hooks/pre-commit"
  if [ -x "$local_hook" ]; then
    "$local_hook" "$@"
    exit $?
  fi
fi

exit 0
