#!/bin/sh
# ---------------------------------------------------------------------------
# MSOS pre-commit hook — keeps derived files in sync automatically.
#
# On every commit it regenerates and stages:
#   - the News hub grid + homepage "Latest news"   (tools/build_listings.py)
#   - /llms.txt and /links-for-bots/               (tools/build_botlinks.py)
# so adding a blog post or an event/project flows through to News, the homepage
# and the bot pages without any manual command.
#
# It never blocks a commit: if Python or a generator fails, it warns and lets
# the commit proceed.
#
# Activate once per clone:   git config core.hooksPath tools/git-hooks
# ---------------------------------------------------------------------------
set -u

PY="$(command -v python || command -v python3 || true)"
if [ -z "$PY" ]; then
    echo "pre-commit: Python not found — skipping News/bot regeneration." >&2
    exit 0
fi

# Run from the repo root regardless of where git invoked the hook.
ROOT="$(git rev-parse --show-toplevel 2>/dev/null || echo .)"
cd "$ROOT" || exit 0

"$PY" tools/build_listings.py >/dev/null 2>&1 \
    || echo "pre-commit: build_listings.py failed — News/homepage not refreshed." >&2
"$PY" tools/build_botlinks.py >/dev/null 2>&1 \
    || echo "pre-commit: build_botlinks.py failed — bot pages not refreshed." >&2

# Stage only the generated outputs (no-op if unchanged).
git add -- \
    en/news/index.html mk/news/index.html si/news/index.html \
    en/index.html mk/index.html si/index.html \
    llms.txt links-for-bots >/dev/null 2>&1 || true

exit 0
