#!/bin/tcsh

set testnum = $1

if ($testnum < 10) then
set tt = "0"$1
else
set tt = $1
endif

set basedir = /mount/mrd-DL
set listdir = $basedir/list"$tt"
set imgdir = $basedir/combined2/img"$tt"
set animdir = $basedir/combined2/mpg"$tt"

if (! -d $listdir) then
mkdir $listdir
endif

if (! -d $imgdir) then
mkdir $imgdir
endif

if (! -d $animdir) then
mkdir $animdir
endif

cp -auR /data/river/results_demo"$testnum"/* $listdir/

###
#set n = 1
#while ($n <= 28)
###
set n = 3
while ($n <= 3)
##

if ($n < 10) then
set nn = "0"$n
else
set nn = $n
endif


set outdir = $imgdir/$nn
if (! -d $outdir) then
mkdir $outdir
else
#rm -rf $outdir/*.jpg
endif

set listfile = $listdir/pred_e"$nn"
#set imglist = `cat $listfile | awk '{print $1}' | head -10`
set imglist = `cat $listfile | awk '{print $1}'`
set flaglist = `cat $listfile | awk '{print $2}'`
set imgnum = 1

set outmpg = $animdir/$nn".mp4"

foreach orgimg ($imglist)
set flag = $flaglist[$imgnum]

if ($imgnum < 10) then
set nnnn = "000"$imgnum
else if ($imgnum < 100) then
set nnnn = "00"$imgnum
else if ($imgnum < 1000) then
set nnnn = "0"$imgnum
else 
set nnnn = $imgnum
endif


echo $nn $nnnn $orgimg $flag 

#set outfname = `basename $orgimg | cut -c1-13`".jpg"
set fn = `basename $orgimg | cut -c1-13`"_"$flag
set outfname = image"$nnnn".jpg

if ($flag == 0) then
set ccode = "00FF00"
else if ($flag == 1) then
set ccode = "FFF100"
else if ($flag == 2) then
set ccode = "FFA500"
else
set ccode = "FF0000"
endif


convert -pointsize 40 -gravity south -annotate +0+20 $fn -fill yellow $orgimg -bordercolor "#$ccode" -border 16x16 -resize 752x512! $outdir/$outfname
echo convert -pointsize 40 -gravity south -annotate 0+20 $fn -fill yellow $orgimg -bordercolor "#$ccode" -border 16x16 -resize 752x512! $outdir/$outfname


@ imgnum++
end


#set int = `echo "300 / $imgnum" | bc`
#set int = `echo "3000 / $imgnum" | bc`
set int = `echo "$imgnum / 30" | bc`
#convert -layers optimize -loop 0 -delay $int $outdir/image*.jpg $outgif &
ffmpeg -r $int -i $outdir"/image"%04d".jpg" -vcodec libx264 -pix_fmt yuv420p -r 30 $outmpg



@ n++
end

