#!/usr/bin/env sh
# Auto-stamp `updated:` (and fill `date:` if missing) on every staged content
# page, then re-stage it so the fresh date is part of the commit.
#
# Enabled with:  git config core.hooksPath scripts/git-hooks
#            or:  pixi run setup-hooks

staged=$(git diff --cached --name-only --diff-filter=ACM | grep -E '^content/.+/index\.md$' || true)
[ -z "$staged" ] && exit 0

if ! command -v python3 >/dev/null 2>&1; then
    echo "pre-commit: python3 introuvable, dates non tamponnées" >&2
    exit 0
fi

root=$(git rev-parse --show-toplevel)
python3 "$root/scripts/stamp_dates.py" --today $staged || exit 1
echo "$staged" | xargs git add
