#!/usr/bin/env bash
# Copyright 2026 Petri Koistinen. Licensed under the Apache License, Version 2.0.
#
# Mandatory pre-commit gate; enabled per clone by scripts/install-hooks.sh.
# Bypassing it (--no-verify or otherwise) is prohibited for every
# contributor, human or AI agent alike.

set -eu

root=$(git rev-parse --show-toplevel)

# Only an entirely prose-only staged change takes the documentation lane.
if python3 "$root/scripts/documentation-gate.py" commit; then
    exit 0
else
    status=$?
    [ "$status" -eq 1 ] || exit "$status"
fi

# The workspace version is the UTC day the tree was last committed on.
# When the day has changed since the stamp, the commit carries a fresh
# stamp, so no build published from main names an earlier day.
# The workspace version names the UTC day of the last code-level change.
# A commit that touches only documentation, workflows or hooks keeps it.
if git diff --cached --name-only | grep -q -v -E '^(\.github/|\.githooks/|doc/|docs/)|\.md$|^LICENSE|^\.gitignore$|^\.editorconfig$' ; then
    stamped=$("$root/scripts/version-stamp.sh")
    if [ -n "$stamped" ]; then
        echo "$stamped"
        git add "$root/Cargo.toml" "$root/Cargo.lock"
    fi
fi

# Mandatory fast quality gates; bypassing them (--no-verify or
# otherwise) is prohibited for every contributor, human or AI agent.
exec "$root/scripts/verify-commit.sh"
