#!/bin/bash

	#------------------------------#
	#      Animation Indoor        #
	#------------------------------#

source ./configuration.sh

DDIR="frames/indoor"
TMPF="/dev/shm/tmp.png"

FIMGDIR="/silo/a/tth/Roulotte/"

POVOPT=" -d +q9 -a +W1920 +H1080 -v +WT7"

# ------------------------------------------------------------
#
#	dynamic failure picture
#
mk_fail_pic ()
{
src=$1
dst=$2
echo "mk fail pic from" $src "to" $dst

line=$((   200 + ($RANDOM % 600) ))
column=$(( 200 + ($RANDOM % 900) ))

convert		$src					\
	-font Lato-Bold					\
	-pointsize 128					\
	-kerning 10					\
	-fill Red					\
	-annotate +${line}+${column} 'Epic Fail !'	\
	$dst
}
# ------------------------------------------------------------

raytrace ()
{
echo "++++++ raytracing "  $1 $2

debut=$(date +"%s")

povray -Iindoor.pov -K$1 $POVOPT -O$TMPF 2> WS/pov.stderr

if [ $? -ne 0 ]
then
	log_event "FAIL $2"
	mk_fail_pic WS/failure.png $2
	tail -15 WS/pov.stderr
	sleep 99
else
	txt=$(printf "%04d" $1 | tr  '01'  'ol')
	convert	$TMPF				\
		-alpha off			\
		-pointsize 84			\
		-kerning 6			\
		-font Courier-Bold		\
		-gravity South-West 		\
						\
		-fill 	$COLTEXT_A		\
		-annotate +60+25 "$txt"		\
		-fill 	$COLTEXT_B		\
		-annotate +65+30 "$txt"		\
						\
		computed.png
	mv computed.png $2

	negfile=$(printf "frames/negatif/%04d.png" $1)
	echo "negatif = " $negfile
	convert	$TMPF 				\
		-colorspace gray		\
		-negate				\
		-equalize			\
		computed.png
	mv computed.png $negfile

	# echo $txt
fi

fin=$(date +"%s")

echo $1 $((fin-debut)) | tee -a WS/chrono.indoor

}

# --------------------------------- MAIN ---------------------

DEBUT=0
NBIMG=2000
COUNT=2001

if [ $# -gt 0 ]
then
	DEBUT=$1
fi
if [ $# -gt 1 ]
then
	COUNT=$2
fi

echo "debut = " $DEBUT "count = " $COUNT
log_event "start mkanim $DEBUT $COUNT"

printf "#declare NbImg = %d;\n" $NBIMG > WS/nbimg.inc

for idx in $( seq  $DEBUT $((NBIMG-1)) )
do

	### foo=$(( (idx+45) % 480 ))
	### rushpic=$(printf "../%s/%04d.png" "frames/rush" $foo)
	### # echo "rushpic " $rushpic >> toto
	### ln -s -f $rushpic WS/rushpic.png 

	### foo=$(( idx % 340 ))
	### wallpic=$(printf "../%s/%04d.png" "Wallpicz" $foo)
	### # echo "wallpic " $wallpic >> toto
	### ln -s -f $wallpic WS/roulotte.png

	# afficher le futur sur l'ecran
	foo=$(( ( idx + 747 ) % NBIMG ))
	futur=$(printf "%s/%04d.png" $DDIR $foo)
	if [ -r $futur ]
	then
		ln -sf ../$futur        WS/tableau.png
		# echo $futur OK
	else
		echo $futur not available
		ln -sfv $PWD/panneau.png      WS/tableau.png
	fi

	img=$(printf "%s/%04d.png" $DDIR $idx)
	# echo image $img

	raytrace $idx $img

	COUNT=$(( COUNT - 1 ))
	echo "remain to trace" $COUNT
	if [ $COUNT -eq 0 ]
	then
		banner "OK BOOMER"
		break
	fi
	sleep  19
done

beep -f 300.7 -r 2 -d 100 -l 400

log_event "end mkanim from $DEBUT"  

beep -f 1000 -r 2 -n -r 5 -l 10 --new

# -----------------------------  the end  ---------
