#! /bin/sh
#
# File: raymany
# Description: Rendering many pov-files sequential
# Programmer: stolz (Horst Stolz)
#       
# try raymany -help for an usage-text!
#

# error message function
err () {
        echo "`basename $0`: $@" 1>&2
        exit 1
}

# default settings
name="."
width=640
height=480
verbose="+v"
raycommand="povray"

# parse command line args
while [ $# -gt 0 ]; do
    case "$1" in
	-width)
	    if [ $# -lt 2 ]; then
		err "$1 requires the picture-width in pixels"
	    fi
	    width=$2
	    shift
	    ;;
	-height)
	    if [ $# -lt 2 ]; then
		err "$1 requires the picture-height in pixels"
	    fi
	    height=$2
	    shift
	    ;;
	-name)
	    if [ $# -lt 2 ]; then
		err "$1 needs the path or prefix of the pov-files"
	    fi
	    name=$2
	    shift
	    ;;
	-raycommand)
	    if [ $# -lt 2 ]; then
		err "$1 needs the path or prefix of the pov-files"
	    fi
	    raycommand=$2
	    shift
	    ;;
	-quiet)
	    verbose="-v"
	    ;;
	-verbose)
	    verbose="+v"
	    ;;
	-zippov)
	    zippov="true"
	    ;;
	[hH] | -*)
            echo "#######################################################"
	    echo "USAGE: raymany [-name <directory/name-prefix>]"
            echo "               [-width <number>] [-height <number>]"
            echo "               [-raycommand <povray-name>]"
            echo "               [-zippov]"
            echo "               [-quiet]"
	    echo ""
	    echo "DESCRIPTION: Use \"povray\" to render the pov-files found"
            echo "  in <dir/prefix>*.pov. The *.pov-files are renamed during"
            echo "  the render-process to *.pov.active. The rendered images"
            echo "  are transformed to PPM and then compressed -> *.ppm.Z ."
            echo "  This can be viewed by XV or xloadimage." 
            echo "  The imagesize can be defined be the options -width and"
            echo "  -height."
            echo "  If the *.pov-files are very big, use -zippov to compress"
            echo "  them after the render-prozess."
            echo "  Use the \"rayclear\" <directory>-script transform them back"
            echo "  to *.pov. Also run rayclear if not all *.tga are converted"
            echo "  to ppm.Z."
            echo "  The default name of the povraytracer is \"povray\". This can"
            echo "  be overridden by the -raycommand-option."
            echo "  If the option -quiet is defined, no progress-line ist printed"
            echo "  during the rendering."
            echo ""
            echo "  WARNINGS: 1. This script need following command:"
            echo "                 tgatoppm  - from PBM Plus"
            echo "                 povray    - POVraytracer"
            echo "                 compress  - packer"
            echo "               check this out before starting!"
            echo "            2. never kill the povray-command only, cause"
            echo "               the shell-script keep running and invoke"
            echo "               a new povray!" 
            echo "            3. don't use wildcards in the -name option!"
            echo ""
            echo "ENVIRONMENT:"
            echo "  Please use POVRAYOPT to spezify the include-path and other"  
            echo "  options."
            echo ""
	    echo "DEFAULT SETTINGS:"
            echo "name = ., width = $width, height = $height"
            
	    exit 1
 	    ;;
	*)
	    err "unknown option $1"
	    ;;
    esac
    shift
done

echo "##############################################################"
echo "### POV -> PPM.Z - Skript"
echo -n "### started on $HOST at: "
date


lastdir=`pwd`
work=`dirname ${name-.}`

if [ -d "$name" ];
then
  work="$name"
fi

echo "### Entering directory $work"
cd $work
base=""


base=`basename "$name" .pov`
if [ "$base" = "." ];
then
  base=""
fi

files=`ls ${base}*.pov`

for file in $files
do
  name=`basename $file .pov`

  if [ -s $name.tga.gz ];
  then
    echo "### Picture $name.tga.gz allready exists"
  elif [ -f $name.pov ];
  then
    echo "### I try to render $name.pov"
    echo "mv -f $name.pov $name.pov.active"
    mv -f $name.pov $name.pov.active
    if [ $? -eq 0 ];
    then
      if [ -s $name.tga ];
      then 
        contopt="+c"
	echo "### Continue rendering on image $name.pov"
      else 
        contopt=""
      fi

      echo "nice -20 $raycommand +i$name.pov.active +o$name.tga $contopt +ft $verbose +w$width +h$height"
      nice -20 $raycommand +i$name.pov.active +o$name.tga $contopt +ft $verbose +w$width +h$height 

      echo "gzip $name.tga"
      gzip $name.tga
      if [ "$?" -gt 0 ];
      then	
	echo "### ERROR, can't compress tga to tga.gz"
	echo "rm -f $name.tga.gz"
	rm -f $name.tga.gz
      else
        echo "mv $name.pov.active $name.pov"
        mv $name.pov.active $name.pov
      fi
      if [ -n "$zippov" ];
      then
	echo "gzip $name.pov"
	gzip $name.pov
      fi
    else
      echo "### Hoppla, someone was faster at file $name.pov than I"
    fi

  else
    echo "### It seems that $name.pov is in progress"
  fi
done

cd $lastdir

