#!/bin/sh

if [ -z $1 ];then 
  echo Filename requiered
  exit 1
fi

cat >encode-notes-image <<EOF
-V -o 01-orig
-V -o 02-I1 -I1
-V -o 03-I3 -I3
   -o 04-smart -k -z -J smartdeinter=diffmode=2:highq=1:cubic=1
-V -o 05-pplb -J pp=lb
-V -o 06-dilyuvmmx -J dilyuvmmx
-V -o 07-dilyuvmmx-dnr -J dilyuvmmx,dnr
-V -o 08-ppfull-I3 -I 3 -J pp=de
-V -o 09-ppfull-I3-dnr -I 3 -J pp=de,dnr
-V -o 10-dnr-ppfull-I3 -I 3 -J dnr,pp=de
   -o 11-smart-sharp -k -z -J smartdeinter=diffmode=2:highq=1:cubic=1,xsharpen=strength=110
EOF

while read i; do
   echo "transcode -i $1 -y im,null -F ppm -c '32-33' $i"
   transcode -i $1 -y im,null -F ppm -c '32-33' $i
 done < encode-notes-image

rm -f encode-notes-image

set +x

echo Fireing up convert

for i in *00.ppm; do 
  TEXT=`echo $i | sed 's,00*.ppm,,;s|...||'`;
  echo -n Processing $i ; 
  convert -crop 100x100+28+365 $i 1-temp-$i;
  echo -n .
  convert -fill pink -draw "text 0,100 $TEXT" 1-temp-$i 1-$i
  echo -n .
  convert -crop 100x101+520+50 $i 2-$i; 
  echo -n .
  rm $i 1-temp-$i; 
  echo .
done

echo Generating intermediate images
convert -append 1* 1-append.ppm 
convert -append 2* 2-append.ppm 
ls *00.ppm | grep -v append | xargs rm -f
echo Generating final image
convert -append 1-append* 2-append* final-append.ppm
rm -f 1-append* 2-append*
