#!/usr/bin/env bash
# Reverse scripts/install_git_hooks.sh: remove Mugi-installed hooks and restore
# any preserved <hook>.local back to <hook>.
set -euo pipefail

cd "$(dirname "$0")/.."
hooks_dst=".git/hooks"

if [ ! -d ".git" ]; then
  echo "[uninstall] not in a git working tree; aborting."
  exit 1
fi

restore_one() {
  local hook="$1"
  local dst="$hooks_dst/$hook"
  if [ -L "$dst" ]; then
    rm -f "$dst"
    echo "[uninstall] removed symlink $dst"
  elif [ -f "$dst" ] && grep -q "Mugi" "$dst" 2>/dev/null; then
    rm -f "$dst"
    echo "[uninstall] removed file $dst"
  fi
  if [ -f "$dst.local" ]; then
    mv "$dst.local" "$dst"
    chmod +x "$dst" 2>/dev/null || true
    echo "[uninstall] restored $hook from $hook.local"
  fi
}

restore_one pre-commit
restore_one post-commit
restore_one pre-push
echo "[uninstall] Done."
