#!/usr/bin/env bash
# Nomarchy pre-commit hook.
#
# Enable per-clone with:
#   git config core.hooksPath .githooks
#
# Two responsibilities:
#  1. Lint changed nomarchy-* scripts (bash -n + shellcheck if available)
#     so syntax errors and unquoted-var bugs don't ship.
#  2. Regenerate docs/SCRIPTS.md when any nomarchy-* script under the three
#     script directories is added, modified, or deleted in this commit, and
#     stage the refreshed file so it lands with the change.

set -e

repo_root="$(git rev-parse --show-toplevel)"
cd "$repo_root"

script_dirs_re='^(features/scripts/utils|core/system/scripts|themes/engine/scripts)/nomarchy-'

# 1. Lint changed scripts. bash -n catches syntax errors (always fatal).
# shellcheck catches unquoted-var, use-before-define, missing-shebang, etc.
# We only fail on severity=error so the long tail of pre-existing warnings
# (info / style / warning) doesn't block commits — those can be cleaned up
# incrementally without a flag day.
changed_scripts=$(git diff --cached --name-only --diff-filter=ACMR \
                  | grep -E "$script_dirs_re" || true)
if [[ -n "$changed_scripts" ]]; then
  while IFS= read -r script; do
    [[ -f "$script" ]] || continue
    # Only lint scripts with a bash shebang. nomarchy-* is a name
    # convention, not a language guarantee — at least one Python helper
    # ships under the same prefix (nomarchy-haptic-touchpad).
    head -1 "$script" | grep -qE '^#!.*\bbash\b' || continue
    if ! bash -n "$script"; then
      echo "pre-commit: bash syntax error in $script — aborting commit." >&2
      exit 1
    fi
    if command -v shellcheck >/dev/null 2>&1; then
      if ! shellcheck --severity=error --shell=bash "$script"; then
        echo "pre-commit: shellcheck found error-level issues in $script — aborting commit." >&2
        echo "pre-commit: fix the reported issues, or rerun with --no-verify after a deliberate decision to ship." >&2
        exit 1
      fi
    fi
  done <<< "$changed_scripts"
fi

# 2. Regenerate the script audit doc.
if git diff --cached --name-only --diff-filter=ACMRD | grep -qE "$script_dirs_re"; then
  echo "pre-commit: regenerating docs/SCRIPTS.md (script change detected)…"
  ./bin/utils/nomarchy-docs-scripts --out docs/SCRIPTS.md
  git add docs/SCRIPTS.md
fi
