#! /bin/sh
#
# File: rayclear
# Description: Performs a cleanup after raystart or raymany
# Programmer: stolz (Horst Stolz)
#       
# try rayclear -help for an usage-text!
#

filelength () {
        echo "$4"
}

work=.

# parse command line args
while [ $# -gt 0 ]; do
    case "$1" in
	-name)
	    if [ $# -lt 2 ]; then
		err "$1 needs the path to the pov-files directory"
	    fi
	    work=$2
	    shift
	    ;;
        -unzip)
            zipopt="true"
            ;;
	-cleanup)
            cleanup="true"
            ;;
	[hH] | -*)
            echo "#######################################################"
	    echo "USAGE: rayclear [-name <directory>]"
            echo "                [-unzip]"
            echo "                [-cleanup]"
	    echo ""
	    echo "DESCRIPTION: Cleanup-skript to stop all remote raymany-"
            echo "  scripts, which are started by raystart! Then rename"
            echo "  all active *.pov.active-files back to *.pov for new"
            echo "  rendering. Also delete the ppm.Z-file of all *.tga"
            echo "  and transform the tga-file with the correct file-"
            echo "  length to ppm.Z. Note that after killing the *.pid"
            echo "  files are deleted."
            echo "  To continue the rendering of the remaining *.tga-files"
            echo "  use raystart."
            echo "  the -name option gives the directory which hold the"
            echo "  image- and pov-files and the files created by raystart."
            echo "  If you want to uncompress the *.pov.Z-files, use the"
            echo "  -unzip option."
            echo "  If the -cleanup option is activated. Additionally all"
            echo "  files generated by raystart or raymany are deleted."
            echo "  This are out.*, widthxheight and list.computers."
            echo "" 
            echo "  WARNINGS: 1. raymany-scripts which are not started by"
            echo "               raystart are not killed!"
            echo "            2. user rayclear -cleanup only if all pics" 
            echo "               are rendered."
            echo "            3. start rayclear only one time!"
            echo ""
	    echo "DEFAULT SETTINGS:"
            echo "name = ."
            
	    exit 1
 	    ;;
	*)
	    err "unknown option $1, use -help for the usage-text"
	    ;;
    esac
    shift
done


echo "#######################################################"
echo "### RAYCLEAR: Cleanup script after an erronous raystart"
echo "###"


lastdir=`pwd`

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

echo ""

# kill all rsh-processes on starter-machine
# to kill the remote raymany-scripts, hope so!
files=`ls *.pid`
if [ "$files" ];
then
  for file in $files
  do
    computer=`basename $file .pid`
    echo "rsh $computer kill `cat $file`"
    rsh $computer kill `cat $file`	
  done

  echo "### remove *.pid files"
  rm -f *.pid
else
  echo "### no remote raymany-scripts running?!"
fi


echo ""
echo "#######################################################"
echo "### determine Picture size from file widthxheight"
if [ -s widthxheight ];
then
  read <widthxheight width height
  tgasize=`expr $width \* $height \* 3 + 18`
  echo "### $width x $height -> tga-size is $tgasize"

  echo ""
  echo "#######################################################"
  echo "### Transform all correct *.tga image to *.ppm.Z"
  echo "### after removing of corresponded *.ppm.Z"

  files=`ls *.tga`
  for file in $files
  do
    bname=`basename $file .tga`

    echo "### remove ${bname}.ppm.Z"
    rm -f ${bname}.ppm.Z

    lenname=`ls -l ${bname}.tga`
    flen=`filelength $lenname`
    if [ $flen -eq $tgasize ];
    then 
      echo "### It seems that $bname.tga is ok!, transform it to -> ppm.Z"
      echo "tgatoppm <$file | compress >$bname.ppm.Z"
      tgatoppm <$file | compress >$bname.ppm.Z
      if [ $? -gt 0 ];
      then
        echo "### error while transforming to ${bname}.tga - ${bname}.ppm.Z"
        echo "### probably is PBM+ not installed!"
        echo "### removing ${bname}.ppm.Z"
        rm -f ${bname}.ppm.Z
        exit 1
      fi
      echo "### remove $bname.tga"
      rm -f $file
    fi
  done
else
  echo "### something wrong with file widthxheight"
fi



echo ""
echo "#######################################################"
echo "### Renaming all *.pov.active to *.pov"
echo "###"

files=`ls *.pov.active`
if [ "$files" ];
then
  for file in $files
  do
    orgname=`basename $file .active`
    bname=`basename $orgname .pov`

    echo "### renaming $file to $orgname"
    mv $file $orgname
  done
fi


if [ "$zipopt" ];
then
  echo ""
  echo "#######################################################"
  echo "### uncompress all compress *.pov.Z files"

  files=`ls *.pov.Z`
  for file in $files
  do
    echo "uncompress $file"
    uncompress $file
  done
fi


if [ "$cleanup" ];
then
  echo ""
  echo "#######################################################"
  echo "### CLEANUP all files generated by the ray-scripts"
  echo "### remove *.out files"
  echo "rm -f out.* widthxheight list.computers"
  rm -f out.* widthxheight list.computers
fi


cd $lastdir

