#! /bin/sh
#
# File: raystat
# Description: prints statistic and progress information 
# Programmer: stolz (Horst Stolz)
#       
# try raystat -help for an usage-text!
#

case "$HOSTTYPE" in
	sun4) 
		lscomand="ls -lg"
		;;
	*)
		lscomand="ls -l"
		;;
esac

filelength () {
        echo "$5"
}


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
	    ;;
	-time)
	    timestat="true"
	    ;;
	-error)
	    errorstat="true"
	    ;;			
	-who)
	    whostat="true"
	    ;;			
	[hH] | -*)
            echo "#######################################################"
	    echo "USAGE: raystat [-name <directory>]"
	    echo "               [-time] [-error] [-who]
	    echo ""
	    echo "DESCRIPTION: Prints out statistic and progress infor-"
            echo "  information like # of *.povs*, # of *.tga and there"
            echo "  progress in %. By specifying the -time option, raystat"
            echo "  shows also the \"Time for Trace\" line from the out.*"
            echo "  files. Note that if the render-process was stopped,"
            echo "  this time is not accurate!"
            echo "  The -error option prints out all lines from the out.*"
            echo "  files in who it found the strings: error, started,"
            echo "  povray, term, kill."
            echo "  The -who options checks all out.* files for the"
            echo "  name of the picture *.pov* to check which host"
            echo "  rendered which picture."
            echo ""
	    echo "DEFAULT SETTINGS:"
            echo "name = ."
            
	    exit 1
 	    ;;
	*)
	    err "unknown option $1, use -help for the usage-text"
	    ;;
    esac
    shift
done


echo "#######################################################"
echo "### RAYSTAT: statistic und progress information"
echo "###"


lastdir=`pwd`

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

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"
else
  echo "### something wrong with file widthxheight"
fi

echo "###"
echo "### General picture progress information"
nrpovs=`ls *.pov* | wc -l`
nrppmz=`ls *.tga.gz | wc -l`
nrtga=`ls *.tga | wc -l`
nractive=`ls *.active | wc -l`
echo "### total number of pics: $nrpovs"
echo "### active povs         : $nractive"
echo "### rendered pictures   : $nrppmz"
echo "### number of tga-files : $nrtga"


# Show total progress-statistic for rendering pictures
echo "###"
echo "### Show Progress of *.tga pictures"
files=`ls *.tga`
for file in $files
do
  bname=`basename $file .tga`
  lenname=`$lscomand $file`
  flen=`filelength $lenname`
  progress=`expr $flen \* 100 / $tgasize`
  echo -n "$file: ${progress}% rendered"
  if [ -s $bname.pov.active ];
  then
    echo ", currently activated"
  else
    echo ""
  fi
done

if [ -n "$errorstat" ]; 
then
  # Show extract of the out.* files for error diagnostic
  echo "###"
  echo "### Show Error-Messages from out.* files"
  files=`ls out.*`
  for file in $files
  do
    echo "$file:"
    egrep -i '(error|started|povray|term|kill)' $file
    echo ""
  done
fi

if [ -n "$timestat" ];
then
  # Show total time-statistic for rendering a picture
  echo "###"
  echo "### Show Raytrace-Time from *.out files"
  files=`ls out.*`
  for file in $files
  do
    echo "$file:"
    grep -i "Time For Trace" $file
  done
fi

if [ -n "$whostat" ];
then
  # Show who-rendered-what statistic
  echo "###"
  echo "### Show which host rendered what picture"
  files=`ls *.pov*`
  for file in $files
  do
    picfile=`basename $file .gz`
    picfile=`basename $picfile .active`
    bname=`basename $picfile .pov`
    echo "# $picfile:"
    grep -l "+i$picfile" out.*
    echo "# $picfile finished by: "
    grep -l "gzip $bname" out.*
    echo ""
  done
fi

echo "###"

cd $lastdir

