#!/bin/sh
# vm-test-install.sh — boot the just-built Mountaineer ISO in a fresh
# disposable target VM and drive the installer non-interactively.
#
# Usage:
#   tools/qemu/vm-test-install.sh [<config-yaml>]
#
# <config-yaml> defaults to tools/qemu/test-install.yaml — the installer's
# --config schema for unattended install. The target VM is destroyed and
# recreated each run, so this is safely re-runnable.

. "$(dirname "$0")/common.sh"
ensure_dirs

ISO=$(ls -t "$ROOT/out"/*.iso 2>/dev/null | head -1 || true)
if [ -z "$ISO" ] || [ ! -f "$ISO" ]; then
    echo "error: no ISO found in $ROOT/out/. Run tools/qemu/vm-build.sh first." >&2
    exit 1
fi

DAEMON=0
CONFIG=""
for arg in "$@"; do
    case "$arg" in
        --daemonize) DAEMON=1 ;;
        *)           CONFIG="$arg" ;;
    esac
done
[ -n "$CONFIG" ] || CONFIG="$ROOT/tools/qemu/test-install.yaml"
if [ ! -f "$CONFIG" ]; then
    echo "error: install config $CONFIG not found" >&2
    exit 1
fi

TARGET_DISK="$DISK_DIR/target.qcow2"
TARGET_SIZE="${TARGET_SIZE:-20G}"

# Fresh disk every run.
rm -f "$TARGET_DISK"
qemu-img create -f qcow2 "$TARGET_DISK" "$TARGET_SIZE"

# Stage the config into a small fat32 image that the live ISO mounts.
# The installer looks for /media/cdrom/install.yaml first, then
# /mnt/config/install.yaml, then prompts interactively.
CFG_IMG="$DISK_DIR/install-config.img"
rm -f "$CFG_IMG"
truncate -s 8M "$CFG_IMG"
HAVE_CFG_IMG=0
if command -v mkfs.fat >/dev/null 2>&1 || command -v mkfs.vfat >/dev/null 2>&1; then
    if command -v mkfs.fat >/dev/null 2>&1; then
        mkfs.fat -n MTN-CFG "$CFG_IMG" >/dev/null 2>&1 && HAVE_CFG_IMG=1
    else
        mkfs.vfat -n MTN-CFG "$CFG_IMG" >/dev/null 2>&1 && HAVE_CFG_IMG=1
    fi
fi
if [ "$HAVE_CFG_IMG" = 1 ] && command -v mcopy >/dev/null 2>&1; then
    mcopy -i "$CFG_IMG" "$CONFIG" ::/install.yaml
else
    HAVE_CFG_IMG=0
    echo "note: dosfstools/mtools not installed; install config will not be auto-attached."
    echo "      brew install dosfstools mtools  # for fully unattended runs"
    echo "      meanwhile: scp $CONFIG over after boot and run mountaineer-install --config <path>"
fi

LOG="$VMS/logs/target-install-serial.log"
MON="$VMS/sockets/target-install-mon.sock"
rm -f "$MON"

echo "==> booting target VM with ISO $ISO and fresh disk $TARGET_DISK"

if [ "$DAEMON" = 1 ]; then
    echo "    serial log: $LOG"
    echo "    ssh:        ssh -p $TARGET_SSH_PORT -i $KEYS_DIR/builder_ed25519 root@127.0.0.1"
    echo "    stop:       printf 'quit\\n' | nc -U $MON"
    # shellcheck disable=SC2046
    qemu-system-x86_64 \
        $(qemu_base_args target) \
        -serial file:"$LOG" \
        -monitor unix:"$MON",server,nowait \
        -daemonize \
        -drive file="$TARGET_DISK",if=virtio,format=qcow2 \
        -drive file="$CFG_IMG",if=virtio,format=raw,readonly=on \
        -cdrom "$ISO" \
        -boot d \
        -netdev user,id=net0,hostfwd=tcp:127.0.0.1:$TARGET_SSH_PORT-:22 \
        -device virtio-net-pci,netdev=net0
else
    echo "    serial console on this terminal; quit qemu with Ctrl-A then X"
    echo ""
    # shellcheck disable=SC2046
    exec qemu-system-x86_64 \
        $(qemu_base_args target) \
        -serial mon:stdio \
        -drive file="$TARGET_DISK",if=virtio,format=qcow2 \
        -drive file="$CFG_IMG",if=virtio,format=raw,readonly=on \
        -cdrom "$ISO" \
        -boot d \
        -netdev user,id=net0,hostfwd=tcp:127.0.0.1:$TARGET_SSH_PORT-:22 \
        -device virtio-net-pci,netdev=net0
fi
