#!/bin/sh
#
# 	%W% %E%
#

sapath=$1
rmmach=$2
shift
shift

logfile=${GIG_D_ROOT}/projects/renmanadm/curf_${rmmach}/gigrs_logfile

if test $sapath = dumb
then
  GIGSA="gig_sa"
else
  GIGSA=$sapath
fi

GIGARG=""
intfiles="intjob giglist videoback giganimap giganibatch0"

if [ -x $GIG_D_BIN/$GIGSA.mips2 ]
then
  if hinv -t cpu | fgrep -s R4 
  then
    GIGSA=$GIGSA.mips2
  fi
fi

if [ -x $GIG_D_BIN/gig_saULTRA ]
then
  if uname -a | fgrep -s sun4u 
  then
    GIGSA=gig_saULTRA
  fi
fi

set -- `getopt vr:i:o:idp:f $*`
if [ $? != 0 -o $# -eq 1 ]
then
  echo 1 usage: $0 -p project animationname startframenr endframenr >> ${logfile}
  exit
fi

for i in $*
do
  case $i in
  -d)       GIGARG="$GIGARG -d"; shift;;
  -f)       GIGARG="$GIGARG -f"; shift;;
  -v | -V)  GIGARG="$GIGARG -r verbose"; shift;;
  -r)       GIGARG="$GIGARG -r $2"; shift 2;;
  -o)       GIGARG="$GIGARG -o $2"; shift 2;;
  -p)       GIGARG="$GIGARG -p $2"; project=$2; shift 2;;
  -i)       intfiles="$intfiles $2"; shift 2;;
  --)       shift; break;;
  esac
done

if [ $# -lt 2 ]
then
  echo 2 usage: $0 -p project animationname startframenr endframenr >> ${logfile}
  exit
fi

aniname=$1
frameno=$2

shift
shift

while [ $# != 0 ] 
do
  if test -r $1.int
  then
    intfiles=${intfiles}" "$1
    shift
  fi
done

host=`uname -n`

rendir=${GIG_D_ROOT}/projects/renmanadm/curf_${rmmach}

jobbie=${rendir}/${host}${aniname}${frameno}${project}
jobbieint=${rendir}/${aniname}${frameno}${project}.int
jobbieout=${rendir}/${aniname}${frameno}${project}.out

echo writepidtofile=\"fcreate\(\"\"$jobbie\"\"\)\; > ${jobbieint}
echo fde=fopen\(\"\"$jobbie\"\",\"\"r+\"\"\)\; pid=igetpid\(\)\; >> ${jobbieint}
echo fprintf\(fde,pid\)\; fclose\(fde\)\; \"\;  writepidtofile\(\)\; >> ${jobbieint}
echo addanibatch\( \"$aniname\", $frameno, $frameno\)\; >> ${jobbieint}
echo print\(igetpid\(\)\)\; >> ${jobbieint}
echo batchjob\(\)\; >> ${jobbieint}
echo forcedexit\(\)\; >> ${jobbieint}

intfiles=${intfiles}" "${jobbieint}

$GIGSA $GIGARG $intfiles > ${jobbieout} &

