#!/bin/bash

source config.sh

DDIR="frames/timelapse"
DSTOPT="/dev/shm/timelapse-altitude.png"
FIMGTMP="/dev/shm/timelapse-cumul.fimg"

# rm "WS/log.timelapse"

DEBUT=$(date +'%s')

for i in $( seq 0 $((NBIMG - 1)) )
do

	dst=$(printf "%s/%05d.png" $DDIR $i)
	tag=$(printf "[%06d:%04d]"			\
			$(($(date +'%s')-$DEBUT))    $i   )
	echo $dst $tag

	j=$(( i * 2 ))
	fimgA=$(printf "%s/%04d.fimg" $SILO $j		)
	fimgB=$(printf "%s/%04d.fimg" $SILO $((j+1))	)
	fimgC=$(printf "%s/%04d.fimg" $SILO $((j+2))	)
	fimgD=$(printf "%s/%04d.fimg" $SILO $((j+3))	)
	fimgE=$(printf "%s/%04d.fimg" $SILO $((j+3))	)
	# echo $fimgA $fimgB $fimgC

	cumulfimgs -o $FIMGTMP   $fimgA $fimgB $fimgC $fimgD $fimgE
	fimg2pnm    -g $FIMGTMP WS/timelapse.pgm

	povray -itimelapse +K$i ${POVOPT} -o${DSTOPT} 2> ERR
	loc=$(tail -1 WS/log.timelapse |          \
		awk '{ printf "Loc %6.3f %6.3f %6.3f\n", $1, $2, $3}')
	lat=$(tail -1 WS/log.timelapse |          \
		awk '{ printf "Lat %6.3f %6.3f %6.3f\n", $4, $5, $6}')

	echo $loc $lat

	convert   $DSTOPT					\
			-font		Courier-Bold		\
			-fill		Orange			\
			-pointsize	36			\
			-annotate +21+49 "${tag}"		\
			-fill		Yellow			\
			-pointsize	32			\
			-annotate +21+112 "$loc"		\
			-annotate +21+150 "$lat"		\
			$dst

	sleep  4

done

echo "Encoding now..." ; echo
exec ./encode.sh   "$DDIR"   "timelapse-hf.mp4"

