#!/usr/bin/env bash
# install-hooks — symlink pre-commit-review into a project's .git/hooks/
# Usage: install-hooks <project-directory>

set -euo pipefail

HOOK_SRC="/Users/sj/SaneApps/infra/SaneProcess/scripts/git-hooks/pre-commit-review"

if [ $# -lt 1 ]; then
  echo >&2 "Usage: install-hooks <project-directory>"
  echo >&2 "Example: install-hooks ~/SaneApps/apps/SaneBar"
  exit 1
fi

PROJECT_DIR="$1"

# Expand tilde if present
PROJECT_DIR="${PROJECT_DIR/#\~/$HOME}"

# Verify it is a git repo
if [ ! -d "$PROJECT_DIR/.git" ]; then
  echo >&2 "Error: $PROJECT_DIR is not a git repository (no .git directory)."
  exit 1
fi

HOOKS_DIR="$PROJECT_DIR/.git/hooks"
TARGET="$HOOKS_DIR/pre-commit"

# Create hooks dir if somehow missing
mkdir -p "$HOOKS_DIR"

# Warn if a pre-commit hook already exists and is not our symlink
if [ -e "$TARGET" ] && [ ! -L "$TARGET" ]; then
  echo >&2 "Warning: $TARGET already exists and is not a symlink."
  echo >&2 "Back it up or remove it, then re-run."
  exit 1
fi

ln -sf "$HOOK_SRC" "$TARGET"
echo "Installed: $TARGET -> $HOOK_SRC"
