#!/usr/bin/env bash
# Boot Ubuntu Core 26 amd64 image in QEMU (UEFI, user networking, SSH on localhost:8022).
set -euo pipefail

UC_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"

IMG="${UC_ROOT}/artifacts/images-dev/pc.img"
FIRMWARE_DIR="${UC_ROOT}/artifacts/firmware"

OVMF_CODE="${OVMF_CODE:-/usr/share/OVMF/OVMF_CODE_4M.secboot.fd}"
OVMF_VARS_TEMPLATE="${OVMF_VARS_TEMPLATE:-/usr/share/OVMF/OVMF_VARS_4M.ms.fd}"
OVMF_VARS="${FIRMWARE_DIR}/OVMF_VARS_4M.ms.fd"

RAM_MB="${RAM_MB:-2048}"
SMP="${SMP:-2}"
SSH_PORT="${SSH_PORT:-8022}"

usage() {
    cat <<'EOF'
Usage: run-qemu.sh [OPTIONS]

Boot a UC26 pc.img in QEMU.

Options:
  --img PATH        Disk image (default: artifacts/images-dev/pc.img)
  --stock           Use stock image artifacts/images/pc.img (console-conf / Ubuntu SSO)
  --ram MB          RAM in MiB (default: 2048)
  --smp N           vCPU count (default: 2)
  --ssh-port PORT   Host port forwarded to guest :22 (default: 8022)
  --reset-uefi-vars Recopy writable OVMF vars from host template
  -h, --help        Show this help

Dev image (default): SSH after first boot without Ubuntu One:
  ssh -i config/ssh/smo-dev smo@localhost -p 8022

Stock image (--stock): complete console-conf in the serial console first.
EOF
}

reset_vars=false

while [[ $# -gt 0 ]]; do
    case "$1" in
        --stock) IMG="${UC_ROOT}/artifacts/images/pc.img"; shift ;;
        --img) IMG="$2"; shift 2 ;;
        --ram) RAM_MB="$2"; shift 2 ;;
        --smp) SMP="$2"; shift 2 ;;
        --ssh-port) SSH_PORT="$2"; shift 2 ;;
        --reset-uefi-vars) reset_vars=true; shift ;;
        -h|--help) usage; exit 0 ;;
        *) echo "Unknown option: $1" >&2; usage >&2; exit 1 ;;
    esac
done

if [[ ! -f "$IMG" ]]; then
    echo "Image not found: $IMG" >&2
    echo "Build dev image:  scripts/build-dev-image.sh" >&2
    echo "Or stock image:   scripts/build-stock-image.sh  (then run-qemu.sh --stock)" >&2
    exit 1
fi

for f in "$OVMF_CODE" "$OVMF_VARS_TEMPLATE"; do
    if [[ ! -f "$f" ]]; then
        echo "Missing firmware file: $f" >&2
        echo "Install with: sudo apt install ovmf qemu-kvm" >&2
        exit 1
    fi
done

if ! command -v qemu-system-x86_64 >/dev/null 2>&1; then
    echo "qemu-system-x86_64 not found. Install with: sudo apt install qemu-kvm" >&2
    exit 1
fi

mkdir -p "$FIRMWARE_DIR"

if [[ "$reset_vars" == true || ! -f "$OVMF_VARS" ]]; then
    cp "$OVMF_VARS_TEMPLATE" "$OVMF_VARS"
    echo "UEFI vars: $OVMF_VARS (fresh copy from template)"
fi

kvm_args=()
if [[ -r /dev/kvm ]]; then
    kvm_args=(-enable-kvm -cpu host)
else
    echo "Warning: /dev/kvm not available; running without KVM" >&2
    kvm_args=(-cpu max)
fi

echo "Image: $IMG"
echo "RAM:   ${RAM_MB}M  SMP: $SMP  SSH: localhost:${SSH_PORT}"

exec qemu-system-x86_64 \
    "${kvm_args[@]}" \
    -smp "$SMP" \
    -m "$RAM_MB" \
    -machine q35 \
    -global ICH9-LPC.disable_s3=1 \
    -netdev "user,id=net0,hostfwd=tcp::${SSH_PORT}-:22" \
    -device virtio-net-pci,netdev=net0 \
    -drive "file=${OVMF_CODE},if=pflash,format=raw,unit=0,readonly=on" \
    -drive "file=${OVMF_VARS},if=pflash,format=raw,unit=1" \
    -drive "file=${IMG},if=none,format=raw,id=disk1" \
    -device virtio-blk-pci,drive=disk1,bootindex=1 \
    -serial mon:stdio
