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

# --- Path dependency: audio-core-bsd --------------------------------------
# This crate depends on audio-core-bsd via a relative path dep
# (`path = "../audio-core-bsd"`). In CI the repo is checked out alone, so the
# sibling directory does not exist. Clone it as a sibling to satisfy the path
# dep. git is preinstalled on the FreeBSD VM image.
ACORE_DIR="../audio-core-bsd"
if [ ! -d "$ACORE_DIR/Cargo.toml" ] && [ ! -f "$ACORE_DIR/Cargo.toml" ]; then
  echo "provisioning path dependency audio-core-bsd..."
  if [ ! -d "$ACORE_DIR" ]; then
    git clone --depth 1 https://github.com/IT-Whistle/audio-core-bsd "$ACORE_DIR"
  fi
fi

# --- This crate's system dependencies -------------------------------------
# audio-resample-bsd is pure Rust (rubato, thiserror, audio-core-bsd) -> 0
# system deps. If a future version links a FreeBSD system library, add it here:
#   pkg install -y alsa-lib   # for an ALSA/OSS backend

echo "setup: ok"
