#!/bin/bash
# By Chih-Wei Huang <cwhuang@linux.org.tw>
# License: GNU Generic Public License v2

continue_or_stop()
{
	echo "Please Enter to continue or Ctrl-C to stop."
	read
}

QEMU_ARCH=`uname -m`
QEMU=qemu-system-${QEMU_ARCH}

which $QEMU > /dev/null 2>&1 || QEMU=qemu-system-i386
if ! which $QEMU > /dev/null 2>&1; then
	echo -e "Please install $QEMU to run the program.\n"
	exit 1
fi

cd ${OUT:-ANDROID_ROOT}

[ -e system.img ] && SYSTEMIMG=system.img || SYSTEMIMG=system.sfs

if [ -d data ]; then
	if [ `id -u` -eq 0 ]; then
		DATA="-virtfs local,id=data,path=data,security_model=passthrough,mount_tag=data"
		DATADEV='DATA=9p'
	else
		echo -e "\n$(realpath data) subfolder exists.\nIf you want to save data to it, run $0 as root:\n\n$ sudo $0\n"
		continue_or_stop
	fi
elif [ -e data.img ]; then
	if [ -w data.img ]; then
		DATA="-drive index=2,if=virtio,id=data,file=data.img"
		DATADEV='DATA=vdc'
	else
		echo -e "\n$(realpath data.img) exists but is not writable.\nPlease grant the write permission if you want to save data to it.\n"
		continue_or_stop
	fi
fi

$QEMU -enable-kvm -vga std -sdl \
	-kernel kernel \
	-append "CMDLINE console=ttyS0 RAMDISK=vdb $DATADEV" \
	-initrd initrd.img \
	-m 2048 -smp 2 -cpu host \
	-soundhw ac97 \
	-net nic,model=e1000 -net user \
	-serial mon:stdio \
	-boot menu=on \
	-drive index=0,if=virtio,id=system,file=$SYSTEMIMG,format=raw,readonly \
	-drive index=1,if=virtio,id=ramdisk,file=ramdisk.img,format=raw,readonly \
	$DATA $@
