#!/bin/bash

source ./configuration.sh

# TXTCOLOR="RosyBrown"
TXTCOLOR="Gray70"

DDIR="frames/snapshot"
ERR="WS/snap.error"
IDXF="WS/snap.next"

clock=$(head -1 $IDXF)

nice -10 make CLOCK=$clock extsnap.png 2> $ERR
errcode=$?
if [ $errcode -ne 0 ]
then
	echo '*** rendering fail ***'
	tail -22 $ERR
	sleep 35
	exit 1
fi

if [ -r WS/snapflag ]
then 
	tdate=$(date --utc +'%F %H:%M' | tr '01' 'ol')
	frame=$(printf "k=%04d" $clock | tr '01' 'ol')
	fname=$(printf "%s/%s.png" $DDIR $(echo "$tdate" | sed 's/ /+/'))
	texte=$(printf "%s utc" "$tdate" | tr '01' 'ol')

	# echo $0 '=' $texte ' ' $clock $fname >> toto

	echo "making " $fname
	convert		extsnap.png			\
			-fill $COLTEXT_A		\
			-font Courier-Bold		\
			-pointsize 22			\
			-gravity North			\
			-annotate +126+15 "${texte}"	\
			-fill $COLTEXT_B		\
			-annotate +378+15 "${frame}"	\
			$fname
	rm WS/snapflag
	grep 'Trace Time' $ERR

	clock=$(( ( clock +1 ) % 2000 ))
	log_event "snap $clock"

	echo $clock > $IDXF

else
	attente=$(( ($RANDOM  % 111) + 111))
	echo '++++++ waiting for ' $attente "s."
	sleep $attente
fi

