#!/bin/bash

echo $0

if [ $# -eq 0 ]
then
	echo "NEED A SEQUENCE NAME"
	exit 1
fi

SEQ=$1

figlet -W ${SEQ}	; echo

source ./configuration.sh

# ./runme.sh ${SEQ}

STDERR="WS/${SEQ}.stderr"
TMPIMG="/dev/shm/tmp${SEQ}.png"
LAST="WS/${SEQ}.last"
DDIR="frames/"${SEQ}"/"

if [ ! -r $LAST ] 
then
	echo 0 >  $LAST
fi

echo "Running for $PASS passes"

for pass in $(seq 0 $PASS)
do

        idx=$(cat $LAST)
        next=$(( ( idx + 1 ) % NBFRAMES ))

        outfile=$(printf "%s/%04d.png" $DDIR $idx ) 
        printf "  %4d / %4d      %s\n" $pass $PASS $outfile

	povray -i${SEQ}.pov -K${idx} ${POVOPT} -O${TMPIMG}  2> ${STDERR}
	error=$?
	if [ $error -ne 0 ]
	then
		echo ${SEQ} "ERRCODE" $error
		echo "+++++++++++++ EPICFAIL ++++++++++++++"
		tail ${STDERR}
		exit 1
	fi

	cp ${TMPIMG} $outfile

	echo $next > $LAST

	sleep 10

done
