#!/bin/dash

### configuration
ISO=${ISO:-fatdog.iso}
QEMU_AUDIO_DRV=${QEMU_AUDIO_DRV-alsa}
export QEMU_AUDIO_DRV
CPUS=${CPUS:-1}
MEM=${MEM:-1536}
VGA_TYPE=${VGA_TYPE:-cirrus-vga} # or VGA (std), or vmware-svga (vmware)
#TABLET_MODE= #default is empty
MACH_TYPE=${MACH_TYPE:-pc}
USESDL=${USESDL--sdl}
HOSTDVD=${HOSTDVD:+-drive if=scsi,file=/dev/$HOSTDVD,format=raw}
NIC=${NIC:-user,hostfwd=tcp::2222-:22}
UEFI_BIOS=${UEFI_BIOS:-uefi-bios.bin}
SOUNDHW=${SOUNDHW--device AC97}

### helpers ###
run_in_my_dir() {
        mydir=$(readlink -f "$0")
        mydir=${mydir%/*}
        cd $mydir
}

###
run_in_my_dir
case $1 in
	uefi) UEFI="-bios $UEFI_BIOS"; shift ;;
esac
[ -e fd64-nls.sfs ]  && HDB="-drive file=fd64-nls.sfs,index=1,media=disk,format=raw"
[ -e fd64-devx.sfs ] && HDD="-drive file=fd64-devx.sfs,index=3,media=disk,format=raw"
[ -e disk.ext3 ]     && HDA="-drive file=disk.ext3,index=0,media=disk,format=raw"
[ -e disk2.img ]     && VDA1="-drive file=disk2.img,if=virtio,media=disk,format=raw"
[ "$HOSTDVD" ] && ! lsmod | grep -q ^sg && modprobe sg
case $VGA_TYPE in
	*cirrus*|*VGA*) ;;
	*) TABLET_MODE=yes ;; # enable tablet mode for others, fixing crazy cursor
esac
[ "$TABLET_MODE" ] && TABLET_MODE="-device usb-tablet" # -usbdevice tablet in older qemu

exec qemu-system-x86_64 -enable-kvm -machine $MACH_TYPE \
$USESDL -m $MEM -device $VGA_TYPE $TABLET_MODE -smp $CPUS \
$UEFI -nic $NIC -usb $SOUNDHW \
-cdrom $ISO $HDA $HDB $HDD $VDA1 $HOSTDVD \
-boot d "$@"
