#!/bin/sh
DEPLOY=build/qemu/deploy
CONSOLE_SOCK=${XDG_RUNTIME_DIR}/qemu-console.sock

while getopts "d:k:" flag; do
	case $flag in
	d) MODULES_DIRECTORY=$OPTARG ;;
	k) KERNEL=$OPTARG ;;
	esac
done

if [[ -z "$MODULES_DIRECTORY" || -z "$KERNEL" || -z "$@" || "-h" = "$1" || "--help" = "$1" ]]; then
	printf >&2 'Usage: %s -d <modules-directory> -k <kernel>\n' "$0"
	exit 1
fi

qemu-system-x86_64 \
	-cpu qemu64-v1 \
	-m 512 \
	-kernel ${KERNEL} \
	-initrd ${DEPLOY}/minimal-initramfs.cpio.gz \
	-monitor stdio \
	-chardev socket,id=serial0,path=${CONSOLE_SOCK},server=on \
	-serial chardev:serial0 \
	-nographic \
	-vga none \
	-virtfs local,path=$MODULES_DIRECTORY,mount_tag=host0,security_model=mapped,id=host0 \
	-chardev socket,id=i2c1,path=${XDG_RUNTIME_DIR}/vhost-user-i2c-1.sock0 \
	-device vhost-user-i2c-pci,chardev=i2c1 \
	-object memory-backend-memfd,id=mem,size=512M,share=on \
	-numa node,memdev=mem \
	-append 'console=ttyS0,115200n8'
