#!/bin/bash

source config.sh

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

POVLOG="WS/err.colormap"

DEBUT=$(date +'%s')

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

	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))	)
	# echo $fimgA $fimgB $fimgC

	cumulfimgs -o $FIMGTMP   $fimgA $fimgB $fimgC $fimgD
	fimgfx -v cos01 $FIMGTMP $FIMGTMP
	fimg2png  $FIMGTMP WS/the_color_map.png

	dst=$(printf "%s/%05d.png" $DDIR $i)
	tmod=$(date +'%s')
	tag=$(printf "%04XH %04dD %08XH" $RANDOM $i $tmod)
	echo  $dst $tag

	povray -icolormap +K$i ${POVOPT} -o${DSTOPT} 2> $POVLOG
	if [ 0 -ne $? ] ; then
		echo POVRAY ERROR
		tail $POVLOG
		echo 
		exit
	fi

	md5=$(md5sum -b $DSTOPT | awk '{print $1}')
	echo $md5

	hcam=$(tail -1 WS/log.colormap | awk '{print $2}')
	acam=$(tail -1 WS/log.colormap | awk '{print $3}')
	camt=$(printf "cam: %5.2f  %5.2f" $hcam $acam)
	echo $camt

	convert   $DSTOPT					\
			-font		Courier-Bold		\
			-pointsize	32			\
			-fill		GreenYellow		\
			-annotate +85+48 "${tag}"		\
			-annotate +85+96 "$camt"		\
								\
			-gravity	South-West		\
			-pointsize	18			\
			-fill		Orange			\
			-annotate +15+10 "${md5}"		\
			$dst

	sleep   15

done

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

