#!/usr/bin/env bash
# ============================================================================
# scripts/install-hooks.sh
# ----------------------------------------------------------------------------
# One-time setup per clone. Points git at the versioned .githooks/ folder
# so the post-merge hook (which auto-runs DB migrations) is active for every
# subsequent `git pull`.
#
# Run this once after the first pull that includes this file:
#     bash scripts/install-hooks.sh
#
# Safe to re-run — it's idempotent.
# ============================================================================
set -euo pipefail

cd "$(git rev-parse --show-toplevel)"

# 1. Point git at the versioned hooks dir
git config core.hooksPath .githooks
echo "  ✓ git config core.hooksPath -> .githooks"

# 2. Make sure the hook and scripts are executable
chmod +x .githooks/* 2>/dev/null || true
chmod +x scripts/*.sh 2>/dev/null || true
echo "  ✓ chmod +x on .githooks/* and scripts/*.sh"

echo ""
echo "Hooks installed. Future 'git pull' on this clone will automatically"
echo "run: php scripts/migrate.php"
echo ""
echo "To apply migrations right now (without waiting for the next pull):"
echo "    php scripts/migrate.php"
