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

# Installer for git hooks: sets repository to use .githooks as hooks path
# and ensures the hook files are executable.

ROOT_DIR="$(git rev-parse --show-toplevel)"
if [[ -z "$ROOT_DIR" ]]; then
  echo "Not inside a git repository" >&2
  exit 1
fi

cd "$ROOT_DIR"

# Set git to use the .githooks directory
git config core.hooksPath ".githooks"

# Ensure hooks are executable
if [[ -d ".githooks" ]]; then
  find .githooks -type f -name "*" -print0 | xargs -0 chmod +x || true
  echo "Installed .githooks and marked executable. core.hooksPath set to .githooks"
else
  echo ".githooks directory not found in repo root" >&2
  exit 1
fi

echo "To undo: git config --unset core.hooksPath" 
