#!/bin/sh

UPDATES_VM=`qvm-get-updatevm`

QREXEC_CLIENT=/usr/lib/qubes/qrexec_client

if [ -z "$UPDATES_VM" ]; then
    echo "UpdateVM not set, exiting!" >&2
    exit 1
fi

if ! xl domid "$UPDATES_VM" > /dev/null 2>&1; then
    echo "UpdateVM not started, exiting!"
    exit 1
fi

# dd is supposed to not allow memory exhaustion
# grep does basic sanity checking
# there seems to be no way to pass output of date +%s.%N to date,
# so we use human-readable format

CURRENT_TIME="$($QREXEC_CLIENT -d $UPDATES_VM 'user:date -u' |
	dd count=1 2>/dev/null |
	grep '^[A-Za-z]* [A-Za-z]* [ 0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [A-Z]* [0-9][0-9][0-9][0-9]$'|
	head -1)"

if [ -n "$CURRENT_TIME" ] ; then 
	echo Syncing Dom0 clock: setting time "$CURRENT_TIME"...
	sudo date -u -s "$CURRENT_TIME" ;
	echo Done.
else
	echo "Error while parsing the time obtained from the UpdateVM ($UPDATES_VM).."
fi