#!/usr/bin/env bash
# scripts/opnsense-build-config-iso.sh <config.xml path> <output.iso path>
#
# ############################################################################
# # RETIRED (D-112) -- THIS PATH CANNOT WORK. DO NOT USE IT.                 #
# #                                                                          #
# # The ISO this builds is INERT. It is never read. Root-caused from         #
# # upstream source (opnsense/core, src/sbin/opnsense-importer):             #
# #                                                                          #
# #     INSTALL="/.probe.for.readonly"                                       #
# #     touch ${INSTALL} 2> /dev/null                                        #
# #     if [ -f ${INSTALL} -a -f /conf/config.xml ]; then                    #
# #             bootstrap_and_exit 0                                         #
# #                                                                          #
# # The touch is a PROBE FOR A READ-ONLY ROOT. On INSTALLER media the root   #
# # is read-only, the probe fails, and the importer goes on to scan attached #
# # media (cd9660 included). On our PRE-INSTALLED NANO image the root is     #
# # WRITABLE and a factory /conf/config.xml ALREADY EXISTS -- so both        #
# # conditions hold and it bootstrap_and_exit 0's IMMEDIATELY, without ever  #
# # enumerating a single device.                                             #
# #                                                                          #
# # The Configuration Importer can NEVER fire on a nano image, BY DESIGN.    #
# # Nothing was ever going to read this ISO.                                 #
# #                                                                          #
# # The working path is D-112(c): boot on factory defaults -> console        #
# # bootstrap -> enable SSH + install the key -> configure over the network  #
# # (now the REST API, per D-113(a2): scripts/opnsense-api.sh).              #
# #                                                                          #
# # Kept (not deleted) only because D-112 says to retire it in the same      #
# # change that reduces the template -- which has NOT yet landed. Its        #
# # harness still passes; that proves the ISO is well-formed, NOT that       #
# # anything reads it.                                                       #
# ############################################################################
#
# Builds a plain ISO9660 image containing /conf/config.xml, for OPNsense's
# Configuration Importer (see opentofu/README.md's "OPNsense deployment
# research" section) -- ISO9660 support was added to the Importer
# specifically so a config ISO could be attached as a secondary CD-ROM,
# mechanically identical to how modules/cloudinit-vm attaches its NoCloud
# seed via a libvirt_volume + create.content.url pointing at a local path.
#
# NOTE (updated after a 2026-07-09 audit pass): the genisoimage/xorriso flags
# below (-V volume label, -J Joliet, -R Rock Ridge) are CONFIRMED standard,
# correct usage for building a plain cross-platform ISO9660 image from a
# directory -- this part is no longer a guess. What remains genuinely
# unverified is a DIFFERENT claim: whether OPNsense's Configuration Importer
# actually reads /conf/config.xml back off an ISO9660 volume built this way
# once booted for real (neither genisoimage/xorriso nor a real OPNsense VM
# was available this session to test that end-to-end). Confirm that
# specifically before relying on it operationally.
#
# Requires: genisoimage or xorriso. Read-only w.r.t. the repo; writes only
# to <output.iso path> and a self-cleaning temp dir.
# Exit: 0 built | 1 bad input | 2 required tool missing.
set -euo pipefail

CONFIG_XML="${1:?usage: opnsense-build-config-iso.sh <config.xml> <output.iso>}"
OUT="${2:?usage: opnsense-build-config-iso.sh <config.xml> <output.iso>}"

[ -r "$CONFIG_XML" ] || { echo "FAIL: cannot read $CONFIG_XML"; exit 1; }

TMP="$(mktemp -d)"; trap 'rm -rf "$TMP"' EXIT
mkdir -p "$TMP/conf"
cp "$CONFIG_XML" "$TMP/conf/config.xml"

if command -v genisoimage >/dev/null 2>&1; then
  genisoimage -o "$OUT" -V "OPNSENSE_CFG" -J -R "$TMP"
elif command -v xorriso >/dev/null 2>&1; then
  xorriso -as genisoimage -o "$OUT" -V "OPNSENSE_CFG" -J -R "$TMP"
else
  echo "FAIL: genisoimage or xorriso required on PATH"
  exit 2
fi

echo "PASS: built $OUT (contains /conf/config.xml)"
