#!/bin/sh
# RefineID Windows push gate.
#
# Rejects a push whose Cargo.lock does not match the manifests, or pins a
# revision that is neither in Cargo's local cache nor fetchable from its
# remote. It guards consistency, not freshness: a lockfile that pins an
# older refineid-core commit pushes, and moving to a newer core is a
# deliberate change made in its own pull request.
#
# Then builds the workspace documentation with rustdoc warnings denied,
# once per target: for the native host and, when the host is another
# target and the Windows target is installed, x86_64-pc-windows-msvc.
#
# Enable local hooks once per clone with:
#
#     git config core.hooksPath .githooks

set -eu

root="$(git rev-parse --show-toplevel)"
cd "$root"
updates="$(mktemp "${TMPDIR:-/tmp}/refineid-windows-push-updates.XXXXXX")"
trap 'rm -f "$updates"' EXIT INT TERM
tr -d '\r' > "$updates"

# Classify before dependency refresh or any compiler invocation.
if python3 "$root/scripts/DocumentationGate.py" push "${1:-origin}" < "$updates"; then
    exit 0
else
    scope_status=$?
    if [ "$scope_status" -ne 1 ]; then
        exit "$scope_status"
    fi
fi

python3 -B scripts/TestDocumentationGate.py

echo "pre-push: magic-number policy"
zero="0000000000000000000000000000000000000000"
checked_any=0
while read -r _local_ref local_sha _remote_ref remote_sha; do
    if [ -z "$local_sha" ] || [ "$local_sha" = "$zero" ]; then
        continue
    fi
    if ! git cat-file -e "${local_sha}^{commit}" 2>/dev/null; then
        continue
    fi
    checked_any=1
    diff_base=""
    if [ "$remote_sha" != "$zero" ] && git cat-file -e "${remote_sha}^{commit}" 2>/dev/null; then
        diff_base="$remote_sha"
    elif git rev-parse --verify -q origin/main >/dev/null 2>&1; then
        diff_base="origin/main"
    fi
    if [ -n "$diff_base" ]; then
        RUSTFLAGS="${RUSTFLAGS:+${RUSTFLAGS} }-D warnings" cargo run -q -p xtask -- check-magic-numbers --diff-base "$diff_base" --diff-head "$local_sha"
    else
        RUSTFLAGS="${RUSTFLAGS:+${RUSTFLAGS} }-D warnings" cargo run -q -p xtask -- check-magic-numbers
    fi
done < "$updates"
if [ "$checked_any" -eq 0 ]; then
    if git rev-parse --verify -q origin/main >/dev/null 2>&1; then
        RUSTFLAGS="${RUSTFLAGS:+${RUSTFLAGS} }-D warnings" cargo run -q -p xtask -- check-magic-numbers --diff-base origin/main
    else
        RUSTFLAGS="${RUSTFLAGS:+${RUSTFLAGS} }-D warnings" cargo run -q -p xtask -- check-magic-numbers
    fi
fi

echo "pre-push: checking that Cargo.lock matches the manifests"

if ! cargo fetch --locked --quiet; then
    echo "pre-push: error: Cargo.lock does not match the manifests, or a pinned revision could not be fetched." >&2
    echo "pre-push: if the manifests changed, update the lockfile for that dependency, review the diff, and commit it;" >&2
    echo "pre-push: if a remote was unreachable, retry once it is." >&2
    exit 1
fi

WINDOWS_TARGET="x86_64-pc-windows-msvc"
HOST_TARGET="$(rustc -vV | sed -n 's/^host: //p')"

echo "pre-push: cargo doc with warnings denied (native host, $HOST_TARGET)"
RUSTDOCFLAGS="-D warnings" cargo doc --workspace --no-deps --quiet

if [ "$HOST_TARGET" != "$WINDOWS_TARGET" ] \
    && rustup target list --installed | grep -qx "$WINDOWS_TARGET"; then
    echo "pre-push: cargo doc with warnings denied ($WINDOWS_TARGET)"
    RUSTDOCFLAGS="-D warnings" cargo doc --workspace --no-deps --quiet --target "$WINDOWS_TARGET"
fi

echo "pre-push: ok (Cargo.lock matches the manifests, documentation builds)"
