#!/bin/bash
# Install the system libraries this repo's build links against and the
# external tools its tests shell out to. The canonical ci.yml runs this in
# every repo before `cargo build`; a repo that needs neither keeps this
# script as an explicit no-op. Keep it strict: anything that fails to
# install must fail the build here, not surface later as a confusing
# build or test failure.
set -euo pipefail

# TARGET is set only by ci.yml's release build job, which runs nothing but
# `cargo build --release`. rsconstruct links no system library (mlua-sys and
# zstd-sys build from vendored sources; the macOS release jobs, which never
# run this script, prove the build needs nothing from it), so the release
# job has nothing to install. Installing the test matrix there anyway put
# every release behind ~10 downloads no step uses, and one of them,
# checkpatch.pl from raw.githubusercontent.com, failed a release on an
# anonymous-rate-limit 429 (run 37225847062).
if [[ -n "${TARGET:-}" ]]; then
	exit 0
fi

# The whole tool matrix, in one registry-driven command. `cargo run` builds
# rsconstruct first (the workflow's later Build step then reuses the same
# artifacts); every external tool comes from its own registry, so adding a
# processor never requires touching CI.
#
# Nothing is skipped and nothing is filtered — `--all` treats a tool it
# cannot install automatically as a hard error rather than a warning, so a
# registry entry that loses its install method fails this step instead of
# quietly shrinking the matrix.
#
# drawio's .deb URL is resolved from the GitHub releases API at install time,
# so this inherits GITHUB_TOKEN from the workflow step to stay under the
# authenticated rate limit (see the comment on that step in ci.yml). Unset
# locally, where one machine never approaches the anonymous 60/hour.
cargo run -- tool install --all

# The cargo subcommands the workflow's later steps run (cargo deny, cargo
# nextest) are declared under [dependencies] cargo in rsconstruct.toml, the
# fleet's one list of them; the binary just built reads it.
cargo run -- tool install-deps
