#!/bin/bash
# Preview only. ROOT must be an already mounted inactive disposable BE.
set -euo pipefail
[[ $# == 2 ]] || { echo "usage: $0 mounted-test-BE full|drivers" >&2; exit 2; }
root=$(cd -P -- "$1" && pwd)
[[ "$root" != / && -f "$root/etc/release" ]] || { echo "inactive BE root required" >&2; exit 2; }
active=$(beadm list -H | awk -F';' -v r="$root" '$4 == r { print "BE:" $3 }')
[[ "$active" == BE:* && "$active" != *[NRT]* ]] || { echo "ROOT must match a mounted non-default BE, with no N/R/T flags" >&2; exit 2; }
here=$(cd -P -- "$(dirname -- "$0")" && pwd)
case "$2" in
 full) archive=$here/i915n-display-hotplug-20261018.p5p; list=$here/graphics-fmris.txt;;
 drivers) archive=$here/i915n-drivers-20261018.p5p; list=$here/driver-fmris.txt;;
 *) exit 2;;
esac
[[ -r "$archive" && -r "$list" ]]
fmris=()
while IFS= read -r fmri; do
    [[ "$fmri" == pkg://openindiana.org/*@* ]] || exit 2
    fmris+=("$fmri")
done < "$list"
echo "Review this dry run and TESTING.md before applying:"
pkg -R "$root" install -nv -g "$archive" "${fmris[@]}"
echo "Only after review, the corresponding apply command is:"
printf '%q ' pkg -R "$root" install -v -g "$archive" "${fmris[@]}"
printf '\n'
