#!/usr/bin/env bash
# Install Git Hooks for GitOps automation
# Must be run after cloning the repository

REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null)" || {
  echo "❌ Not in a git repository"
  exit 1
}

HOOKS_DIR="$REPO_ROOT/.git/hooks"
HOOK_SOURCE="$REPO_ROOT/scripts/hooks/pre-commit"
HOOK_DEST="$HOOKS_DIR/pre-commit"

# Verify source exists
if [ ! -f "$HOOK_SOURCE" ]; then
  echo "❌ Hook source not found: $HOOK_SOURCE"
  exit 1
fi

# Create symlink (force if exists)
mkdir -p "$HOOKS_DIR"
ln -sf "../../scripts/hooks/pre-commit" "$HOOK_DEST"
chmod +x "$HOOK_SOURCE"

echo "✅ Git hooks installed:"
echo "   • pre-commit: ConfigMap checksum auto-update"
echo "   See: docs/ops-configmap-sync.md"
