#!/usr/bin/env bash
# scripts/prereqs/install-all.sh [--dry-run]
#
# Orchestrator: runs every prereq installer in a sensible order, then a final
# read-only check. Passes --dry-run through to each. Each installer is itself
# idempotent, so re-running is safe. Debian/Ubuntu (apt).
#
#   --dry-run   print what each installer would do; mutate nothing
#   (no flag)   install all missing prereqs (uses sudo)
#
# Exit: 0 all ok | 1 an installer failed | 2 bad args. ASCII + LF.
set -uo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
. "$HERE/lib-prereq.sh"

pq_parse_mode "$@"; rc=$?
[ "$rc" = "10" ] && { echo "usage: bash scripts/prereqs/install-all.sh [--dry-run]"; exit 0; }
[ "$rc" = "0" ] || exit "$rc"
FLAG=""; [ "$PQ_DRYRUN" = "1" ] && FLAG="--dry-run"

# virtualization first (substrate), then its AppArmor override (must follow the
# libvirt install -- it edits libvirt's own profile dir), then tofu, then the
# smaller tools.
for s in install-virtualization install-apparmor-libvirt install-opentofu install-jq install-image-tools; do
  echo "== $s =="
  bash "$HERE/$s.sh" $FLAG || { echo "FAIL: $s.sh exited $?" >&2; exit 1; }
  echo
done

echo "== final read-only check =="
bash "$HERE/check-prereqs.sh" || true   # report only; install-all's own success is per-installer above
echo
echo "install-all: done${FLAG:+ (dry-run)}"
