#!/bin/sh
# =============================================================================
# freebsd-setup.sh — runs INSIDE the FreeBSD VM (vmactions/freebsd-vm `prepare`).
# =============================================================================
# Role: install the Rust toolchain + this crate's system dep (libopus).
# Portability: /bin/sh on FreeBSD (no bashisms). Use [ ], case, parameter
# expansion only.
# =============================================================================
set -eu

# --- Rust toolchain -------------------------------------------------------
# MSRV floor is 1.85 (Cargo.toml `rust-version`). Stable satisfies it.
# A fresh FreeBSD image ships only `fetch`, not curl; rustup-init is fetched
# via curl, so install curl + CA roots first (idempotent, cheap).
# `--profile default` bundles clippy + rustfmt (rustup-init's `--component`
# flag takes one value per flag; `--component clippy rustfmt` is rejected).
if ! command -v cargo >/dev/null 2>&1; then
  echo "installing rustup + stable toolchain..."
  command -v curl >/dev/null 2>&1 || pkg install -y curl ca_root_nss
  curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs \
    | sh -s -- -y --profile default
fi

# Source cargo env so this shell (and the subsequent `run` step) see cargo.
if [ -f "$HOME/.cargo/env" ]; then
  # shellcheck disable=SC1091
  . "$HOME/.cargo/env"
fi

echo "rustc: $(rustc --version)"
echo "cargo: $(cargo --version)"

# --- This crate's system dependency ---------------------------------------
# audio-opus-bsd links the system libopus (BSD-3-Clause) through the `opus`
# crate's `audiopus_sys` FFI. This MUST be installed before `cargo build`/`test`
# or the link step fails with "ld: error: unable to find library -lopus".
#
# Mapping per umbrella TESTING-STANDARDS §6.1 step 3:
#   audio-opus-bsd) pkg install -y opus ;;
pkg install -y opus

echo "setup: ok"
