#!/bin/bash
#
# Copyright 2007-2008 Samuel Thibault <samuel.thibault@eu.citrix.net>
#
# dm script around stubdomains.
#

DIR=`dirname $0`
. "${DIR}/stubdompath.sh"
stubdom_configdir="${XEN_CONFIG_DIR}/stubdoms"

# To fit xterms nicely
height=339

# Parse arguments

domid=
domname=
vncviewer=0
vncpid=
extra=
sdl=0
opengl=1
vnc=0
vncunused=0
while [ "$#" -gt 0 ];
do
    if [ "$#" -ge 2 ];
    then
	case "$1" in
	    -d)
                domid=$2;
                extra="$extra -d $domid";
                shift
                ;;
	    -domain-name)
                domname=$2;
                shift
                ;;
	    -vnc)
                vnc=1
                op=${2%,*}
		ip=${op%:*};
		vnc_port=${op#*:};
		shift
		;;
            -vncunused)
                vncunused=1
                shift
                ;;
            -loadvm)
                extra="$extra -loadvm $2";
                shift
                ;;
            -k)
                keymap=$2
                shift
                ;;
	    -serial)
		serial="$2"
		shift
		;;
            -monitor)
		monitor="$2"
		shift
		;;
	esac
    fi
    case "$1" in
	-vncviewer) vncviewer=1 ;;
        -sdl) sdl=1 ;;
        -disable-opengl) opengl=0 ;;
    esac
    shift
done

[ -z "$domid"   ] && { echo "couldn't find domain ID" ; exit 1; }
[ -z "$domname" ] && { echo "couldn't find domain name" ; exit 1; }

# Termination handler

term() {
    [ -n "$vncpid" ] && kill -9 $vncpid
    rm -f /tmp/domname-dm
    rm ${stubdom_configdir}/$domname-dm
    exit 0
}

trap term SIGHUP

############
# stubdomain
# Wait for any previous stubdom to terminate
while xm list | grep -w $domname-dm
do
	sleep 1
done

# Generate stubdom config file
mkdir -p ${stubdom_configdir} &>/dev/null
echo "#This file is autogenerated, edit $domname instead!" > ${stubdom_configdir}/$domname-dm
echo "kernel = '${XENFIRMWAREDIR}/ioemu-stubdom.gz'" >> ${stubdom_configdir}/$domname-dm

vfb="sdl=$sdl, opengl=$opengl"
test "$DISPLAY" && vfb="$vfb, display=$DISPLAY"
test "$XAUTHORITY" && vfb="$vfb, xauthority=$XAUTHORITY"
test $vnc != 0 && vfb="$vfb, vnc=$vnc, vncdisplay=$vnc_port, vnclisten=$ip, vncunused=$vncunused"
vncpasswd=`xenstore-read /local/domain/0/backend/vfb/$domid/0/vncpasswd 2>/dev/null`
test "$vncpasswd" && vfb="$vfb, vncpasswd=$vncpasswd"
test "$keymap" && vfb="$vfb, keymap=$keymap"
test "$monitor" && vfb="$vfb, monitor=$monitor"
test "$serial" && vfb="$vfb, serial=$serial"
echo "vfb = ['$vfb']" >> ${stubdom_configdir}/$domname-dm

echo -n "disk = [ " >> ${stubdom_configdir}/$domname-dm
j=0
for i in `xenstore-ls /local/domain/$domid/device/vbd | grep 'backend =' | awk '{print $3}'`
do
    i=${i%\"}
    i=${i#\"}
    vbd_mode=`xenstore-read $i/mode`
    vbd_disk=`xenstore-read $i/params`
    vbd_type=`xenstore-read $i/type`
    vbd_dev=`xenstore-read $i/dev`
    vbd_front=`xenstore-read $i/frontend`
    vbd_devtype=`xenstore-read $vbd_front/device-type`
    if [ $j -ne 0 ]
    then
        echo -n "," >> ${stubdom_configdir}/$domname-dm
    fi
    echo -n "'$vbd_type:$vbd_disk,$vbd_dev:$vbd_devtype,$vbd_mode'" >> ${stubdom_configdir}/$domname-dm
    j=$(( $j + 1 ))
done
echo " ] " >> ${stubdom_configdir}/$domname-dm
echo -n "vif = [ " >> ${stubdom_configdir}/$domname-dm
j=0
for i in `xenstore-ls /local/domain/$domid/device/vif | grep 'backend =' | awk '{print $3}'`
do
    i=${i%\"}
    i=${i#\"}
    vif_mac=`xenstore-read $i/mac`
    vif_bridge=`xenstore-read $i/bridge`
    if [ $j -ne 0 ]
    then
        echo -n "," >> ${stubdom_configdir}/$domname-dm
    fi
    echo -n "'mac=$vif_mac" >> ${stubdom_configdir}/$domname-dm
    if [ "$vif_bridge" ]
    then
        echo -n ",bridge=$vif_bridge'" >> ${stubdom_configdir}/$domname-dm
    else
        echo -n "'" >> ${stubdom_configdir}/$domname-dm
    fi
    j=$(( $j + 1 ))
done
echo " ] " >> ${stubdom_configdir}/$domname-dm

mkfifo /tmp/$domname-dm
xm create -c ${stubdom_configdir}/$domname-dm target=$domid memory=32 extra="$extra" < /tmp/$domname-dm &
exec 4>/tmp/$domname-dm


###########
# vncviewer
if [ "$vncviewer" = 1 ]
then
    # Wait for vnc server to appear
    while ! vnc_port=`xenstore-read /local/domain/$domid/console/vnc-port`
    do
        # Check that the stubdom job is still alive
        kill -0 $consolepid || term
	sleep 1
    done

    vncviewer $ip:$vnc_port &
    vncpid=$!
fi

# wait for SIGHUP or stubdom termination
wait

term
