#!/bin/bash

source ./configuration.sh

PASS=77
DELAY="  104 " 
NAME="OMG, a cl4ss1c g4rd3n"
NBFRAMES=800

TMPF="/dev/shm/rush.png"
DDIR="frames/rush"
STDERR="WS/rush.stderr"

POVOPT=" -d +q9 +a +W1024 +H768 -v +WT2"

log_event "start mkrush"

for pass in $(seq 0 $PASS)
do

	idx=$(cat WS/rush.last)
	next=$(( ( idx + 1 ) % NBFRAMES ))
	printf "pass %3d/%3d   next %3d\n" 	\
			$pass $PASS $next
	outfile=$(printf "%s/%04d.png" $DDIR $next)

	povray -irush.pov -K${next} $POVOPT -O${TMPF} 2> $STDERR

	if [ $? -ne 0 ]
	then
		mogrify -colorspace gray $outfile
		tail -14 $STDERR
		log_event "FAIL $outfile"
		sleep 180
	else
		txt=$(printf "C=%03d" $next | tr '01' 'ol')
		tdate=$(date --utc +'%F %H:%M' |  tr '01' 'ol')

		convert	${TMPF}				\
			-pointsize 32			\
			-kerning  2 			\
			-font Courier-Bold		\
							\
			-fill $COLTEXT_A		\
			-gravity South-West		\
			-annotate +876+50 "$txt"	\
			-annotate +31+50   "$NAME"	\
			-fill  $COLTEXT_B		\
			-annotate +879+53 "$txt"	\
			-annotate +34+53   "$NAME"	\
							\
			-gravity South			\
			-font $TEXT_TT			\
			-pointsize 16			\
			-fill $COLTIME			\
			-annotate +0+8  "$tdate"	\
			$outfile

		echo $outfile " done at " $tdate

	fi

	echo $next > WS/rush.last

	sleep $DELAY

done

# log_event "encoding rush"

ffmpeg  -nostdin                                                \
        -loglevel error                                         \
        -y -r 25 -f image2 -i $DDIR/%04d.png                   \
	-metadata artist='---[ tTh ]---'			\
	-metadata title='* this is just an ugly wip *'		\
        -c:v libx264 -pix_fmt yuv420p                           \
        rush.mp4

echo
figlet -W '* DONE *' | sed 's/^/          /'
beep -f 1119 -n -f 2093 -n -f 1568
log_event "end mkrush $next"
echo

