width=400
height=300
jump=1
start=auto
end=auto 
povray=povray
params=
frames=f%07g.pov
output=+FT
ext=tga
redo=""
try_again=""
while test "$1" != ""; do
  if test "$1" = "--help"; then
    echo "Usage:"
    echo "  render [options]"
    echo
    echo "This is a tool to render Animations generated by anitmt-calc"
    echo "before usage, go into the animtation directory."
    echo
    echo "options:"
    echo "  -w n        image width [400]"
    echo "  -h n        image height [300]"
    echo "  -j n        jump always <n> frames [1]"
    echo "  -s n        number of first frame to render [auto]"
    echo "  -e n        number of last frame to render [auto]"
    echo "  -p cmd      povray command [x-povray]"
    echo "  -f pattern  frame file pattern [f%07g.pov]"
    echo "  --redo      try to recover already rendered pictures"
    echo "  --tga       output frame files in tga format"
    echo "  --ppm       output frame files in ppm format"
    echo "  --png       output frame files in png format"
    echo 
    echo "example:"
    echo "  cd ani/"
    echo "  render -j 3"
    exit 0;
  elif test "$1" = "-w";then
    shift;
    width=$1;
  elif test "$1" = "-h";then
    shift;
    height=$1;
  elif test "$1" = "-j";then
    shift;
    jump=$1;
  elif test "$1" = "-s";then
    shift;
    start=$1;
  elif test "$1" = "-e";then
    shift;
    end=$1;
  elif test "$1" = "-p";then
    shift;
    povray=$1;
  elif test "$1" = "-f";then
    shift;
    frames=$1;
  elif test "$1" = "--redo";then
    redo=yes;
  elif test "$1" = "--tga";then
    output=+FT;
    ext=tga;
  elif test "$1" = "--ppm";then
    output=+FP;
    ext=ppm;
  elif test "$1" = "--png";then
    output=+FN;
    ext=png
  else
    params="$params $1"
  fi
  shift
done

if test "$start" = "auto"; then
  max=$(ls|wc -l);
  for (( i=0; i<=max ; i++ )) do 
    if test -e "$(seq -f $frames $i 1 $i)"; then break; fi;
  done
  if test "$i" -lt "0"; then
    echo No frame files with pattern $frames found in this directory;
    exit 1;
  fi
  start=$i;
fi
if test "$end" = "auto"; then
  i=$(ls|wc -l);
  for (( ; i>=0 ; i-- )) do 
    if test -e "$(seq -f $frames $i 1 $i)"; then break; fi;
  done
  if test "$i" -lt "0"; then
    echo No frame files with pattern $frames found in this directory;
    exit 1;
  fi
  end=$i;
fi

if test $jump -lt 0; then
  echo negative jump
  if test $start -lt $end; then
    buf=$start;
    start=$end;
    end=$buf;
  fi
else
  echo positive jump
  if test $end -lt $start; then
    buf=$start;
    start=$end;
    end=$buf;
  fi
fi

echo > image_file_list

for i in $(seq -f $frames $start $jump $end); do
  try_again="";
  picture_file=$(basename $i .pov).$ext;
  if test "$redo" != ""; then
    if test -e $picture_file; then
      if identify $picture_file; then
        if test $picture_file -nt $i; then
          continue;
        else
	  echo render frame again
        fi
      else
        echo continue frame;
        try_again="+C";
      fi
    fi
  fi
  echo $picture_file >> image_file_list
  echo =====================================================================
  echo $povray +I$i +w$width +h$height +D $output $try_again $params ; 
  $povray +I$i +w$width +h$height $output $try_again $params || exit -5 ; 
done  

exit 0;
