#!/bin/sh
# =============================================================================
# freebsd-setup.sh — runs INSIDE the FreeBSD VM (vmactions/freebsd-vm `prepare`).
# =============================================================================
# Role: install the Rust toolchain + this crate's system dependency (alsa-lib).
# 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.
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

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 dependencies -------------------------------------
# audio-io-bsd's `cpal-backend` feature links cpal → alsa-sys → libasound.
# On FreeBSD the `alsa-lib` port provides this (it translates ALSA calls onto
# OSS /dev/dsp). Without it the cpal-backend feature cannot compile/link.
# `pkgconf` is required for alsa-sys's build script to discover alsa-lib.
echo "installing alsa-lib + pkgconf (cpal ALSA backend dependencies)..."
pkg install -y alsa-lib pkgconf

echo "setup: ok"
