#!/usr/bin/env bash
# Build the Nucleic Linux **accessibility (AT-SPI) agent** as an arm64 binary.
#
# Unlike the dependency-free static-C `nucleic-linux-agent`, this is a Rust crate (zbus/tokio) and is
# built with cargo. Run inside the Nucleic Linux build container / CI (arm64) — the macOS host can't
# build a Linux binary. The output is a build/CI artifact the base build bakes into the guest image
# (see MacVMEngine+LinuxProvision.swift), exactly like the C agent's prebuilt binary.
#
#   guest/nucleic-a11y-agent/build.sh [OUTDIR]
set -euo pipefail

here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
outdir="${1:-$here/build}"
mkdir -p "$outdir"

# Prefer a rustup-provided toolchain if present in the environment.
[ -f "$HOME/.cargo/env" ] && . "$HOME/.cargo/env"
command -v cargo >/dev/null || { echo "✗ cargo not found — install Rust (rustup) in the build container." >&2; exit 1; }

echo "▸ cargo build --release ($(uname -m)) …"
( cd "$here" && cargo build --release )

cp "$here/target/release/nucleic-a11y-agent" "$outdir/nucleic-a11y-agent"
strip "$outdir/nucleic-a11y-agent" 2>/dev/null || true
echo "built $outdir/nucleic-a11y-agent ($(uname -m), $(wc -c < "$outdir/nucleic-a11y-agent") bytes)"
