#! /bin/sh
#
# File: raystart
# Description: Rendering pov-files on remote machines.
#              Doing this by starting of many raymany-script,
#              each on a different computer.
# Programmer: stolz (Horst Stolz)
#       
# try raystart -help for an usage-text!
#

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

# default computerlist
#sun10="chronos matisse mondrian"
#sun4="aeolos agamemnon ajax alkinoos aphrodite barlach dix escher kalypso kerberos kirke klee monet nausikaa odysseus pablo penelope phoenix pluton salvador telemach vasarely vincent"
#hp9000="duerer"
#rs6000="antigone brahma eteokles indra kali oedipus polynikes wischnu"
#computers="$sun10 $hp9000 $rs6000 $sun4"
computers=$HOST

# default settings
comlist=${RAYSTART_COMPUTERS-${computers}}
raydir=`pwd`
width=640
height=480
nodimopt="true"

# 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
            nodimopt=""
	    shift
	    ;;
	-height)
	    if [ $# -lt 2 ]; then
		err "$1 requires the picture-height in pixels"
	    fi
	    height=$2
            nodimopt=""
	    shift
	    ;;
	-name)
	    if [ $# -lt 2 ]; then
		err "$1 needs the dir-path of the pov-files"
	    fi
	    raydir=$2
	    shift
	    ;;
	-computers)
	    if [ $# -lt 2 ]; then
		err "$1 needs an list of computernames"
	    fi
	    comlist=$2
	    shift
	    exitwhile=0
	    while [ $# -gt 1 ]; do
	        case "$2" in	
         	-*)
		    break		
	            ;;
		*)
		    comlist="$comlist $2"
		    shift
		    ;;
		esac
	    done
	    ;;
	-raycommand)
	    if [ $# -lt 2 ]; then
		err "$1 needs the name of the povraytracer"
	    fi
	    raycomoption="-raycommand \"$2\""
	    shift
	    ;;
	-login)
	    loginopton="true"
	    ;;
	-zippov)
	    zipoption="-zippov"
	    ;;
	[hH] | -*)
            echo "#######################################################"
	    echo "USAGE: raystart [-name <directory>]"
            echo "                [-width <number>] [-height <number>]"
            echo "                [-computers \"<list of hostnames>\"]"
            echo "                [-raycommand <povray-name>]"
            echo "                [-zippov]"
            echo "                [-login]"
	    echo ""
	    echo "DESCRIPTION: Invoke the raymany-script on the computers"
            echo "  defined by the <list of hostnames> to render the pov-"
            echo "  files in the <directory> by the pov-raytracer. The"
            echo "  imagesize can be defined be the options -width and"
            echo "  -height."
            echo "  \"raystart\" redirects the output of the raymany-script"
            echo "  to out.\$HOST. and save the list of hosts at the file"
            echo "  \"list.computers\". The image-dimension are saved at"
            echo "  \"widthxheight\". The pid-numbers of the rsh-commands"
            echo "  are stored in \$HOST.pid. All files are located in the"
            echo "  dir <directory>."
            echo "  Use the \"rayclear\ <directory>-script to stop all"
            echo "  running raymany-scripts or if something has gone wrong!"
            echo "  The default name of the povraytrace is \"povray\". This can"
            echo "  be overridden by the -raycommand-option."
            echo "  If the *.pov-files are very big, use -zippov to compress"
            echo "  them after the render-prozess."
            echo "  If you have access to a machine with an different login-"
            echo "  name. Write the loginname into the file \$HOME/\$HOST.login"
            echo "  and turn on the -login option."
            echo ""
            echo "  For an selective kill use the stored pid-nummbers."
            echo ""
            echo "  To render with new image-dimension remove all *.ppm.Z"
            echo "  and widthxheight and *.tga-file and invoke rayclear -cleanup."
            echo ""
            echo "  WARNINGS: 1. don't use a hostname twice!"
            echo "            2. use a path who is valid on all hosts!"
            echo "            3. never kill the povray-command only, cause"
            echo "               the shell-script keep running!"
            echo ""
            echo "ENVIRONMENT:"
            echo "  The variable RAYSTART_COMPUTERS overwrites the default"
            echo "  list of hostnames."
            echo "  Please use POVRAYOPT to spezifie the include-path!"  
            echo ""
	    echo "DEFAULT SETTINGS:"
            echo "name = $raydir, width = $width, height = $height"
 	    echo "computers = $comlist"
            
	    exit 1
 	    ;;
	*)
	
	    err "unknown option $1"
	    ;;
    esac
    shift
done


echo "#######################################################"
echo "### RAYSTART, Remote Start of raymany               ###"
echo "###"
echo ""
echo "#######################################################"
echo "### current x/y image dimensions are $width x $height"
if [ -s $raydir/widthxheight ];
then
  read <$raydir/widthxheight lastwidth lastheight
  echo "### last saved dimensions: $lastwidth x $lastheight"

  if [ "$nodimopt" ];
  then
    width=$lastwidth
    height=$lastheight
    echo "### using last saved dimensions."
  fi

  if [ $width -ne $lastwidth -o  $height -ne $lastheight ];
  then
    echo "" 
    echo "#######################################################"
    echo "### WARNING: CURRENT AND LAST IMAGE DIMENSIONS ARE"
    echo "### NOT EQUAL!"
    echo "### Execute \"rayclear\" for an correct termination"
    echo "### of running raytracers. Than delete all obsolent"
    echo "### image-files and the file \"widthxheight\"."
    echo "### Than you can call \"raystart\" again."
    echo "#######################################################"
    echo ""
    exit 1
  fi
fi

echo ""
echo "#######################################################"
echo "### starting raymany-script on remote computers"
echo "### and remember rsh-processes in $HOST.pid"
echo ""

for computer in $comlist
do
  if [ "$loginopton" ];
  then
    if [ -s $HOME/$computer.login ];
    then 
      loginopt="-l `cat $HOME/$computer.login`"
    else
      loginopt=""
    fi
  fi
  echo "rsh $loginopt $computer raymany -quiet $zipoption $raycomoption -name $raydir/. -width $width -height $height 1>>$raydir/out.$computer 2>&1 &"
  rsh $loginopt $computer raymany -quiet $zipoption $raycomoption -name $raydir/. -width $width -height $height >>$raydir/out.$computer 2>&1 &
  echo ""
  echo -n " $!" >>$raydir/$HOST.pid
done

echo ""
echo "### save list of computers and image-dimensions"
echo "### to \"$raydir/list.computers\""
echo "### and \"$raydir/widthxheight\""

echo $comlist >$raydir/list.computers
echo $width $height >$raydir/widthxheight

