#!/bin/dash

### configuration
MEM=${MEM:-1024}
VGA_TYPE=${VGA_TYPE:-cirrus} # or std, or vmware (broken)
BOOT_PARAM=${BOOT_PARAM:-basesfs=device:sda:/ pfix=nox video=800x600-24} # or VGA-1:800x600-24

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

build_initrd() {
	if ! [ -e qemu-initrd ]; then
		INITRD_DIR=/tmp/initrd.$$ INITRD=$(readlink -f .)/qemu-initrd
		cp -a ../fatdog-initrd $INITRD_DIR
		cp ../kernel/kernel-modules.sfs $INITRD_DIR
		find $INITRD_DIR -name placeholder_for_fossil -delete
		( cd $INITRD_DIR; find . | cpio -o -H newc > $INITRD )
		rm -rf $INITRD_DIR
	fi
}

### main ###
run_in_my_dir
build_initrd
case $1 in
	uefi) UEFI="-bios uefi-bios.bin"; shift ;;
esac
[ -e fd64-nls.sfs ]  && HDB="-hdb fd64-nls.sfs"
[ -e fd64-devx.sfs ] && HDC="-hdc fd64-devx.sfs"
[ -e disk.ext3 ]     && HDD="-hdd disk.ext3"

exec qemu-system-x86_64 -sdl -enable-kvm -m $MEM -vga $VGA_TYPE -soundhw hda \
$UEFI -redir tcp:2222::22 \
-kernel ../kernel/vmlinuz -initrd qemu-initrd \
-append "$BOOT_PARAM $*" \
-hda fd64.sfs $HDB $HDC $HDD
