#!/usr/bin/env bash
# scripts/prereqs/install-image-tools.sh [--check|--dry-run]
#
# Prereq installer: image/ISO tooling for the OPNsense-edge build path
# (dc-dc Stage 2/3). qemu-img (from qemu-utils) is used by
# scripts/opnsense-prep-image.sh; an ISO9660 builder (xorriso preferred, or
# genisoimage) is used by scripts/opnsense-build-config-iso.sh. Debian/Ubuntu
# (apt). Idempotent.
#
# --check report qemu-img + iso-builder presence; exit 0/1
# --dry-run print what it would do; mutate nothing
# (no flag) install qemu-utils + xorriso (sudo)
#
# Exit: 0 ok/installed | 1 check-failed | 2 bad args | 3 unsupported OS | 4 install failed.
# ASCII + LF.
set -uo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
. "$HERE/lib-prereq.sh"
PKGS="qemu-utils xorriso"
pq_parse_mode "$@"; rc=$?
[ "$rc" = "10" ] && { echo "usage: bash scripts/prereqs/install-image-tools.sh [--check|--dry-run]"; exit 0; }
[ "$rc" = "0" ] || exit "$rc"
report() {
local ok=0
pq_have qemu-img && echo "OK: qemu-img present" || { echo "ABSENT: qemu-img (qemu-utils)"; ok=1; }
if pq_have xorriso; then echo "OK: xorriso present (ISO9660 builder)"
elif pq_have genisoimage; then echo "OK: genisoimage present (ISO9660 builder)"
else echo "ABSENT: no ISO9660 builder (xorriso or genisoimage)"; ok=1; fi
return "$ok"
}
report; st=$?
[ "$PQ_CHECK" = "1" ] && exit "$st"
[ "$st" = "0" ] && { echo "-- image/ISO tooling already satisfied --"; exit 0; }
pq_require_apt "image tools" || exit 3
pq_run "apt-get update" -- sudo apt-get update || { echo "FAIL: apt-get update" >&2; exit 4; }
# shellcheck disable=SC2086
pq_run "install: $PKGS" -- sudo apt-get install -y $PKGS || { echo "FAIL: package install" >&2; exit 4; }
[ "$PQ_DRYRUN" = "1" ] && { echo " (dry-run: no changes made)"; exit 0; }
report >/dev/null && { echo "INSTALLED: qemu-img + ISO builder"; exit 0; }
echo "FAIL: post-install check incomplete" >&2; exit 4