#!/bin/bash

if [ x"$1" == "x" ] ; then
	echo "Usage: $0 vm_params_file"
	echo "    Boot VM from hard disk, no cdrom for VM"
	echo
	echo "Usage: $0 vm_params_file cdrom_iso_filename"
	echo "    Boot VM from hard disk, cdrom_iso_filename is cdrom for VM"
	echo
	echo "Usage: $0 vm_params_file cdrom_iso_filename boot_from_cd"
	echo "    Boot VM from cdrom image cdrom_iso_filename"
	echo
	exit -1
fi

source "$1"

if [ x"${DISK0}" == "x" ] ; then
	"Error. vm_params_file "$1" does not contain definition for Disk0"
	exit -1
fi
if [ x"${MAC}" == "x" ] ; then
	"Error. vm_params_file "$1" does not contain definition for ethernet MAC"
	exit -1
fi
if [ x"${MEMORY}" == "x" ] ; then
	"Error. vm_params_file "$1" does not contain definition for VM memory"
	exit -1
fi
if [ x"${VNCDISP}" == "x" ] ; then
	"Error. vm_params_file "$1" does not contain definition for VNC Display"
	exit -1
fi

# Pick the first unused tap interface
TAP=""
for ((i=0; i < 128 ; i++)); do
	/sbin/ifconfig tap$i >& /dev/null
	if [ $? != 0 ] ; then
		TAP="tap$i"
		break
	fi
done

if [ x"${TAP}" == "x" ] ; then
	echo "Error: Unable to get an unused tap interface"
	exit -1
fi

# Setup the tap interface
/usr/sbin/tunctl -b -t "${TAP}" >& /dev/null
if [ $? != 0 ] ; then
	echo "Error: Unable to setup tap interface ${TAP}"
	exit -1
fi
/usr/sbin/brctl addif br0 "${TAP}" >& /dev/null
if [ $? != 0 ] ; then
	echo "Error: Unable to tap interface ${TAP} to bridge br0"
	exit -1
fi
/sbin/ifconfig "${TAP}" up 0.0.0.0 promisc >& /dev/null
if [ $? != 0 ] ; then
	echo "Error: Unable to bring up tap interface ${TAP}"
	exit -1
fi

#Finally startup the KVM VM
if [ x"$DISK1" == "x" ] ; then
	if [ x"$2" == "x" ] ; then
		/usr/libexec/qemu-kvm -drive "file=${DISK0},if=virtio,boot=on" -m "${MEMORY}" -net nic,model=virtio,macaddr="${MAC}" -net tap,ifname="${TAP}",script=no,downscript=no,macaddr="${MAC}" -vnc 0.0.0.0:${VNCDISP} -usb -usbdevice tablet
		RV=$?
	else
		if [ x"$3" == "xboot_from_cd" ] ; then
			/usr/libexec/qemu-kvm -drive "file=${DISK0},if=virtio" -m "${MEMORY}" -cdrom "${2}" -boot d -net nic,model=virtio,macaddr="${MAC}" -net tap,ifname="${TAP}",script=no,downscript=no,macaddr="${MAC}" -vnc 0.0.0.0:${VNCDISP} -usb -usbdevice tablet
			RV=$?
		else
			/usr/libexec/qemu-kvm -drive "file=${DISK0},if=virtio,boot=on" -m "${MEMORY}" -cdrom "${2}" -net nic,model=virtio,macaddr="${MAC}" -net tap,ifname="${TAP}",script=no,downscript=no,macaddr="${MAC}" -vnc 0.0.0.0:${VNCDISP} -usb -usbdevice tablet
			RV=$?
		fi
	fi
else
	if [ x"$2" == "x" ] ; then
		/usr/libexec/qemu-kvm -drive "file=${DISK0},if=virtio,boot=on" -drive "file=${DISK1},if=virtio" -m "${MEMORY}" -net nic,model=virtio,macaddr="${MAC}" -net tap,ifname="${TAP}",script=no,downscript=no,macaddr="${MAC}" -vnc 0.0.0.0:${VNCDISP} -usb -usbdevice tablet
		RV=$?
	else
		if [ x"$3" == "xboot_from_cd" ] ; then
			/usr/libexec/qemu-kvm -drive "file=${DISK0},if=virtio" -drive "file=${DISK1},if=virtio" -m "${MEMORY}" -cdrom "${2}" -boot d -net nic,model=virtio,macaddr="${MAC}" -net tap,ifname="${TAP}",script=no,downscript=no,macaddr="${MAC}" -vnc 0.0.0.0:${VNCDISP} -usb -usbdevice tablet
			RV=$?
		else
			/usr/libexec/qemu-kvm -drive "file=${DISK0},if=virtio,boot=on" -drive "file=${DISK1},if=virtio" -m "${MEMORY}" -cdrom "${2}" -net nic,model=virtio,macaddr="${MAC}" -net tap,ifname="${TAP}",script=no,downscript=no,macaddr="${MAC}" -vnc 0.0.0.0:${VNCDISP} -usb -usbdevice tablet
			RV=$?
		fi
	fi
fi
if [ ${RV} != 0 ] ; then
	echo "Error: qemu-kvm returned error ${RV}"
fi

# Tear down tap interface
/usr/sbin/brctl delif br0 "${TAP}" >& /dev/null
/usr/sbin/tunctl -d "${TAP}" >& /dev/null

exit ${RV}

