#!/bin/bash

IMAGE=$1
TYPE=$2

if [ ! -f $IMAGE ]; then
	exit 123
fi

if [ -z $TYPE ]; then
	exit 124
fi

MACADDR="DE:AD:BE:EF:42:14"

QEMU_AUDIO_DRV=pa
SNAPSHOT=" -snapshot"

case $2 in 
	tap)
	    NETWORK="-device e1000,netdev=tap0,mac=$MACADDR -netdev tap,id=tap0"
	;;
	local)
	    NETWORK="-net user,hostfwd=tcp::2222-:22"
	;;
esac

case $3 in
	snapshot)
		SNAPSHOT=" -snapshot"
	;;
	nosnapshot)
		SNAPSHOT=""
	;;
	*)
	;;
esac

# console=ttyS0,115200n8 console=tty0 reboot=efi
#qemu-system-x86_64 -m 2048 -enable-kvm -cpu host \
#    -hda $IMAGE -smp 2 -kernel bzImage --append 'console=ttyS0,115200n8 console=tty0 reboot=efi root=/dev/hda2' \
#    -soundhw hda -net nic \
#    -device virtio-rng-pci \
#    -display gtk -device virtio-vga,virgl=on \
#    -serial mon:stdio -serial null \
#    -device nec-usb-xhci -device usb-mouse -device usb-kbd -show-cursor -usb -device usb-tablet \
#    $NETWORK
#

CPUVERS=" -cpu host -machine q35"
#CPUVERS="-cpu kvm64 -cpu qemu64,+ssse3,+sse4.1,+sse4.2,+popcnt -machine q35"
#CPUVERS="-cpu kvm64 -cpu qemu64,+ssse3,+sse4.1,+sse4.2,+popcnt"

qemu-system-x86_64 -m 2048 -enable-kvm  $CPUVERS \
    -hda $IMAGE -bios /usr/share/ovmf/OVMF.fd -smp 2 \
    -soundhw hda -net nic \
    -device virtio-rng-pci \
    -display gtk -device virtio-vga,virgl=on,max_outputs=1,id=video0 \
    -serial mon:stdio -serial null \
    -device nec-usb-xhci -device usb-mouse -device usb-kbd -show-cursor -usb -device usb-tablet \
    $SNAPSHOT $NETWORK
