#!/usr/bin/env bash
set -euo pipefail

# Self-installing git hooks for qmd
# Called from package.json "prepare" script after bun install / npm install

REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
HOOKS_DIR="$REPO_ROOT/.git/hooks"

if [[ ! -d "$HOOKS_DIR" ]]; then
  echo "Not a git repository, skipping hook install"
  exit 0
fi

INSTALLED=()

for hook in pre-commit pre-push; do
  src="$REPO_ROOT/scripts/$hook"
  if [[ -f "$src" ]]; then
    cp "$src" "$HOOKS_DIR/$hook"
    chmod +x "$HOOKS_DIR/$hook"
    INSTALLED+=("$hook")
  fi
done

echo "Installed git hooks: ${INSTALLED[*]}"
