#!/bin/bash
period=10

while true
do
	if [ "$(/usr/bin/qvm-run -p netvm 'cat /tmp/btdms_trigger' 2>/dev/null)" == "1" ]
	then
		#TODO: When Dom0 kexec is suppported, don't use a wiper program; instead kexec a memtesting kernel (a la Liberte Linux)
		cp /home/dom0_user/bin/wiper-stage0 /tmp/

		export DISPLAY=:0.0 #XXX -- should be unnecessary?
		/usr/bin/xscreensaver-command -lock #lock screen--FIXME doesn't work when this script launched via cron or XFCE autostart
		/usr/bin/qvm-shutdown --force --wait --all #try to gracefully shutdown any running VMs, but kill after a timeout

		/usr/bin/sync
		for dev in `ls /dev/mapper/*`
		do
			devname=$(echo "$dev" | awk -F/ '{print $NF}')
			/sbin/cryptsetup luksSuspend "$devname"
		done #wipe all disk crypto keys

		/tmp/wiper-stage0 #launch best-effort memory scrubber
	fi

	sleep $period
done
