#!/bin/sh
# =============================================================================
# freebsd-test.sh — runs INSIDE the FreeBSD VM (vmactions/freebsd-vm `run`).
# =============================================================================
# Role: run cargo fmt / clippy / test natively on FreeBSD for THIS crate.
# This repo is a single crate at the repo root (Cargo.toml at root), so the
# checks run at the workspace root directly. The path dependency on audio-core
# is satisfied by the workflow checking audio-core out as a sibling.
#
# This is the native FreeBSD regression gate. It
# CANNOT run on a Linux host because a FreeBSD test binary may link system
# libraries absent on Linux (audio-codec-bsd itself is pure Rust, but the gate is
# run uniformly to catch std/libc divergence early, per the toolkit CI policy).
#
# Arg:  $1 = ci_type (default|quick|full). `full` additionally builds docs.
# Exit: non-zero on the first failing check (set -e), failing the CI job.
# Portability: /bin/sh on FreeBSD (no bashisms).
# =============================================================================
set -eu

ci_type="${1:-default}"

# Make cargo available (rustup was installed by freebsd-setup.sh).
if [ -f "$HOME/.cargo/env" ]; then
  # shellcheck disable=SC1091
  . "$HOME/.cargo/env"
fi

echo "============================================================"
echo "  audio-codec-bsd  (ci_type=$ci_type, $(uname -sr))"
echo "============================================================"

echo "--- cargo fmt --check ---"
cargo fmt --check

echo "--- cargo clippy --all-targets -- -D warnings ---"
cargo clippy --all-targets -- -D warnings

echo "--- cargo test (native FreeBSD) ---"
cargo test

# opt-in: full ci_type builds API docs to catch broken intra-doc links.
case "$ci_type" in
  full)
    echo "--- cargo doc --no-deps ---"
    cargo doc --no-deps
    ;;
esac

echo "============================================================"
echo "  RESULT: passed (ci_type=$ci_type)"
echo "============================================================"
