#!/usr/bin/env bash
# Local pre-push gate: the same checks, in the same order, as rust.yml and
# homologation.yml. Machine-wide state goes to a throwaway TOKENIX_HOME, so a
# run never touches ~/.tokenix (trust store, hook log).
#
#   ./scripts/verify.sh            # fmt, clippy, tests, release build, CLI + hooks
set -euo pipefail
cd "$(dirname "$0")/.."

export CI=true
TOKENIX_HOME="$(mktemp -d)"
export TOKENIX_HOME
trap 'rm -rf "$TOKENIX_HOME"' EXIT

step() { printf '\n==> %s\n' "$*"; }

step "fmt";    cargo fmt --all -- --check
step "clippy"; cargo clippy --all-targets --locked --quiet -- -D warnings
# fuzz/ includes src/chunker.rs with its own lockfile; cflite_pr.yml builds it.
step "fuzz targets"; cargo check --locked --quiet --manifest-path fuzz/Cargo.toml --bins
step "test";   cargo test --locked --quiet

step "release build"
cargo build --release --locked --quiet
bin="$PWD/target/release/tokenix"
[[ -x "$bin" ]] || bin="$bin.exe"

step "CLI homologation"; bash scripts/homologation.sh "$bin"
step "hook contracts";   bash scripts/test_hooks.sh "$bin"
step "Copilot hook";     bash scripts/test_copilot_hook.sh "$bin"

printf '\nverify: every gate green\n'
