#!/bin/bash

set -e ; set -u

source tools/config.sh
source tools/fonctions.sh

SEQNAME="$1"
START=0

if [ $# == 2 ]
then
	START=$2
fi

echo "$0 $SEQNAME from $START" >> WS/log

DIMS="-W$Img_Width -H$Img_Height"
POVOPT="+q9 +A0.02 -d ${DIMS} -WT5"
echo $POVOPT ; echo ; sleep 2

TMPIMG=/dev/shm/${SEQNAME}-tmp.png

for frame in $(seq $START $((NBFRAMES-1)))
do

	img=$(printf "frames/%s/%05d.png" $SEQNAME $frame)
	debut=$(date +%s)
	set +e
	povray ${POVOPT} +K${frame} -i${SEQNAME}.pov -o${TMPIMG}
	err=$?
	if [ $err != 0 ] ; then
		echo "$SEQNAME fail $frame" >> WS/log
		mogrify -colorspace gray -blur 5x5 $img
		visual_sleep "render fail" 12
		continue
	fi
	set -e
	txt=$(printf "%s  %03d" ${SEQNAME} $frame | tr '01' 'ol')
	# echo $frame $img $txt
	convert    ${TMPIMG}			\
		-font Courier-Bold		\
		-pointsize 12			\
		-kerning   8			\
		-fill ${TEXTCOL}		\
		-gravity north			\
		-annotate +12+5 "$txt"		\
		${img}
	fin=$(date +%s)
	echo
	printf "%-12s %4d %4d\n" 	\
		${SEQNAME} $frame $(( fin-debut )) | \
				tee -a WS/mp4.timing
	echo ; sleep 2
done

date --utc > WS/$SEQNAME.done
echo 

ff_encodage $SEQNAME wip-${SEQNAME}.mp4

echo "+------- build of $SEQNAME done"