set -ex
last_autoinstaller=$(ls -t1 autoinstall-2026* | head -n1)

echo " ....... $last_autoinstaller"
sleep 1

_cdrom=()
if [[ "$1" = "cdrom" ]]; then
  _cdrom=(
    -drive file=${last_autoinstaller},format=raw,if=none,id=usbdisk
    -device usb-ehci,id=usb
    -device usb-storage,drive=usbdisk,bus=usb.0,bootindex=1
    -bios /usr/share/ovmf/OVMF.fd
  )
fi

## Create a virtual disk first
## Uncomment 'create' if test-disk missing
# qemu-img create -f qcow2 /tmp/test-disk.qcow2 20G

# Boot the ISO as USB with the virtual disk as install target
qemu-system-x86_64 \
  -m 4096 \
  -enable-kvm \
  -drive file=/tmp/test-disk.qcow2,format=qcow2 \
  "${_cdrom[@]}" \
  -bios /usr/share/ovmf/OVMF.fd
