#!/bin/sh
#
# Pre-push hook for Keleusma. Mirrors the "Before opening a pull
# request" checklist in CONTRIBUTING.md so the same signals that gate
# CI are checked before code leaves the machine. Installed from this
# version-controlled copy by cargo-husky (see the dev-dependency in
# Cargo.toml) the next time the dev-dependencies compile.
#
# Bypass for a work-in-progress push with: git push --no-verify
#
# The doc step uses the same feature set and RUSTDOCFLAGS as the CI
# Doc job. Keep these in sync with .github/workflows/ci.yml and the
# CONTRIBUTING.md checklist if either changes.

set -eu

# Tests: run the binaries in parallel with cargo-nextest when it is installed
# (each integration-test file is a separate binary, and nextest runs them
# concurrently instead of one-at-a-time). nextest does not execute doc-tests, so
# run those separately. Fall back to the serial `cargo test` if nextest is not
# installed (install it with: cargo install cargo-nextest --locked).
if cargo nextest --version >/dev/null 2>&1; then
    echo "pre-push: cargo nextest run --workspace (parallel)"
    cargo nextest run --workspace
    echo "pre-push: cargo test --workspace --doc (doc-tests)"
    cargo test --workspace --doc
else
    echo "pre-push: cargo test --workspace (install cargo-nextest for a parallel run)"
    cargo test --workspace
fi

echo "pre-push: cargo fmt --all -- --check"
cargo fmt --all -- --check

echo "pre-push: cargo clippy --workspace --all-targets -- -D warnings"
cargo clippy --workspace --all-targets -- -D warnings

echo "pre-push: cargo doc (keleusma, docs.rs feature set, CI flags)"
RUSTDOCFLAGS="-D warnings -A rustdoc::redundant-explicit-links" \
    cargo doc -p keleusma --no-deps --features signatures,encryption,shell

echo "pre-push: keleusma run scripts/check-md-links.kel"
cargo run -q -p keleusma-cli -- run scripts/check-md-links.kel

echo "pre-push: all checks passed"
