#!/bin/bash

#Get the name of the VM that should be run
vboxmachine=$1
#Get the IP of the m23 server to send connection URL to
m23server=$2



#Try to fetch the m23 client ID
id=`cat /m23clientID 2> /dev/null`
if test $id
then
	idvar="?m23clientID=$id"
fi

#Get the visual connection URL
url=`cat /tmp/$vboxmachine.vnc`

#Send the visual connection URL to the m23 server
wget -T5 -t0 --post-data="url=$url&clientName=$vboxmachine&type=MSR_VM_setVisualURL" "https://$m23server/postMessage.php?$idvar"  --no-check-certificate -O "/tmp/MSR_VM_setVisualURL-$vboxmachine.log"

#Display the background image
xloadimage -onroot /m23/vms/vbox/bin/vboxBackground.png & echo $! > /tmp/m23VBoxVNCPIDs-$vboxmachine
#Start the window manager
flwm& echo $! > /tmp/m23VBoxVNCPIDs-$vboxmachine
#Give the user a terminal
xterm& echo $! >> /tmp/m23VBoxVNCPIDs-$vboxmachine
#Start the clock to make the VNC screen get updated every second
xclock -update 1& echo $! >> /tmp/m23VBoxVNCPIDs-$vboxmachine
#xte 'mousermove -1 0'; sleep 1; xte 'mousermove 1 0'; sleep 1
#Start the VirtualBox instance
VirtualBox -startvm $vboxmachine& echo $! >> /tmp/m23VBoxVNCPIDs-$vboxmachine
#Show the window to kill the current session
/m23/vms/vbox/bin/killCurrentVNCSession.sh $vboxmachine