#!/bin/sh
# Pre-commit hook: ensures README.md and windows-releases/template/README.md
# are always generated from docs/README.template.md and never edited directly.
#
# Install: make install-hooks

README_FILES="README.md windows-releases/template/README.md"

# Only run if the template or any generated README is being committed
TEMPLATE_STAGED=$(git diff --cached --name-only | grep -q "docs/README.template.md" && echo "yes" || echo "no")
READMES_STAGED=$(git diff --cached --name-only | grep -E "^README\.md$|^windows-releases/template/README\.md$" && echo "yes" || echo "no")

if [ "$READMES_STAGED" = "yes" ] && [ "$TEMPLATE_STAGED" = "no" ]; then
  echo "❌ README files cannot be edited directly."
  echo "   Edit docs/README.template.md and run 'make readme' to regenerate."
  exit 1
fi

if [ "$TEMPLATE_STAGED" = "yes" ]; then
  # Template is being committed — regenerate and auto-stage the outputs
  node scripts/build-readme.js
  git add $README_FILES
  echo "✅ README files regenerated from template and staged"
fi

exit 0
