#!/bin/dash

### configuration
QEMU_AUDIO_DRV=${QEMU_AUDIO_DRV-alsa}
export QEMU_AUDIO_DRV
CPUS=${CPUS:-1}
MEM=${MEM:-1536}
VGA_TYPE=${VGA_TYPE:-cirrus} # or std, or vmware
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--soundhw 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"
[ "$HOSTDVD" ] && ! lsmod | grep -q ^sg && modprobe sg
# -usbdevice tablet in older qemu
case $VGA_TYPE in
	vmware) VGA_TYPE="vmware -device usb-tablet" # -usbdevice tablet in older qemu
esac

exec qemu-system-x86_64 -enable-kvm -soundhw hda -machine $MACH_TYPE \
$USESDL -m $MEM -vga $VGA_TYPE -smp $CPUS \
$UEFI -nic $NIC -usb $SOUNDHW \
-cdrom fatdog.iso $HDA $HDB $HDD $HOSTDVD \
-boot d "$@"
