#!/usr/bin/env bash
# The gates from `docs/SPEC.md` section 10, runnable without CI.
#
# Each is a hard failure, not a warning. The `unsafe` gate is a grep rather than a lint because
# `#![deny(unsafe_code)]` cannot express "unsafe is allowed in exactly one file".
set -euo pipefail
cd "$(dirname "$0")/.."

fail() { echo "FAIL: $*" >&2; exit 1; }
pass() { echo "  ok: $*"; }

echo "==> build"
cargo build --all-targets
pass "build"

echo "==> no_std"
cargo build --no-default-features
pass "no_std (the crate is #![no_std]; --no-default-features drops the std dependency)"

# A real cross-compile proves more, and is worth doing when the target is installed.
for target in thumbv7em-none-eabihf aarch64-unknown-none; do
    if rustup target list --installed 2>/dev/null | grep -qx "$target"; then
        cargo build --no-default-features --target "$target"
        pass "no_std cross-compile to $target"
    else
        echo "  skip: $target not installed (rustup target add $target)"
    fi
done

echo "==> clippy"
cargo clippy --all-targets --all-features -- -D warnings
pass "clippy -D warnings"

echo "==> unsafe containment"
# The crate contains no `unsafe` at all. `#![deny(unsafe_code)]` already enforces that, and this
# check makes the claim explicit and greppable rather than implied -- and it fails if anyone later
# adds a local `#[allow(unsafe_code)]`, which is how a single-exception policy tends to creep back.
# The grep is for `unsafe` in code positions: a doc comment that merely names it is not a finding.
violations="$(grep -rnE --include='*.rs' '(unsafe[[:space:]]*\{|unsafe[[:space:]]+(fn|impl|trait|extern))' src/ || true)"
if [ -n "$violations" ]; then
    echo "$violations" >&2
    fail "unsafe code in src/"
fi
if grep -rn --include='*.rs' 'allow(unsafe_code)' src/ >/dev/null; then
    grep -rn --include='*.rs' 'allow(unsafe_code)' src/ >&2
    fail "an allow(unsafe_code) was added; the crate is meant to need none"
fi
pass "no unsafe in src/, and no allow(unsafe_code)"

echo "==> licence"
# A dual-licensed crate must ship both texts, and the placeholders in them must be filled: a
# licence naming nobody is not a licence. This fails rather than warns, so an unfilled file cannot
# reach crates.io by being overlooked.
for f in LICENSE-MIT LICENSE-APACHE; do
    [ -f "$f" ] || fail "$f is missing; the crate declares MIT OR Apache-2.0"
done
if grep -qE '<YEAR>|<COPYRIGHT HOLDER>|\[yyyy\]|<year>|<copyright holders>' LICENSE-MIT LICENSE-APACHE; then
    grep -nE '<YEAR>|<COPYRIGHT HOLDER>|\[yyyy\]|<year>|<copyright holders>' LICENSE-MIT LICENSE-APACHE >&2
    fail "licence placeholders are unfilled"
fi
pass "licence texts present and filled"

echo "==> tests"
cargo test --all-features
pass "tests"

echo "==> tests, repeated"
# The property tests reseed every run, and two of the bugs found during development only
# reproduced on some seeds. One green run is not evidence for either.
for _ in 1 2 3 4 5 6; do
    cargo test --all-features --quiet || fail "a repeated test run failed"
done
pass "6 repeated runs"

echo "==> doc tests"
cargo test --doc
pass "doc tests"

echo
echo "all gates passed"
