#!/bin/sh

. /opt/etc/globals

lockfile=/tmp/`$basename $0`.lock
runfile=/tmp/`$basename $0`.running

if [ -f $lockfile ]; then
#	echo "$0: recursive call!" >> $logfile
	echo "$0: recursive call!"
	exit
fi

$su -l $vmuser -c "$vboxmanage list runningvms" > $runfile
vms=`$cat $runfile | $tr -d \" | $awk '{print $1;}'`
$rm $runfile

for vm in $vms; do
	echo -n "Saving state of VM $vm... " | $tee -a $logfile
	$su -l $vmuser -c "$vboxmanage controlvm $vm savestate" 2>&1 | $tee -a $logfile
	echo >> $logfile
done

# rm -f /opt/etc/vmmon/*.pid
