#!/usr/bin/env bash
# Guards the silent-failure mode described in .agents/rules/gates.md.
#
# Husky points core.hooksPath at `.husky/_`, but that directory is GENERATED by
# `pnpm install` and is NOT committed. A fresh `git worktree add` therefore has
# hooks configured and the directory missing — so every hook silently does
# nothing and every commit passes unchecked.
#
# Run this in any worktree you did not create with worktree.sh.
set -euo pipefail

cd "$(git rev-parse --show-toplevel)"
fail=0

path=$(git config --get core.hooksPath || true)
if [ -z "$path" ]; then
  echo "✗ core.hooksPath is unset — husky was never installed here"
  fail=1
else
  echo "✓ core.hooksPath = $path"
fi

if [ ! -d "${path:-.husky/_}" ]; then
  echo "✗ ${path:-.husky/_}/ does not exist — HOOKS ARE NOT RUNNING"
  fail=1
else
  echo "✓ ${path} exists"
fi

for hook in pre-commit commit-msg pre-push; do
  if [ -f ".husky/$hook" ]; then
    echo "✓ .husky/$hook present"
  else
    echo "✗ .husky/$hook missing"
    fail=1
  fi
done

if [ ! -d node_modules ]; then
  echo "✗ node_modules missing — lint-staged and astro check cannot run"
  fail=1
fi

if [ "$fail" -ne 0 ]; then
  echo
  echo "Fix: pnpm install --frozen-lockfile   (its prepare script regenerates .husky/_)"
  exit 1
fi

echo
echo "hooks are live in this worktree"
