#!/bin/dash
# Copyright (C) James Budiono
# License: GNU GPL Version 3 or later
#
# called by AppRun
#exec 2> /tmp/log-clock
APPDIR=${0%/*}

update_clock() {
	# date
	sed -i -e "s/ary>.*</ary>$(date +"%A %e %B %Y")</" \
${APPDIR}/AppInfo.xml
	
	# time
	time=$(date +%I-%M)
	hour=${time%-*} 
	minute=${time#*-}
	hour=${hour#0}	
	minute=${minute#0}

	hour_rot=$(( $hour*30 + $minute/2 ))
	min_rot=$(( $minute*6 ))
	#echo $hour_rot $min_rot
	
	sed -i -e "
/\#handh/ s/rotate(.*)/rotate($hour_rot)/
/\#handm/ s/rotate(.*)/rotate($min_rot)/
" ${APPDIR}/clock.svg
	rox -x ${APPDIR}	
}

# 1. initial update
update_clock

# 2. sync to the start of the minute
while [ $(date +"%S") -ne 0 ]; do sleep 1; done 

# 3. run the clock proper
while true; do
	update_clock
	sleep 60
done
