#!/bin/csh -f
#
# 	%W% %E%
#
# script for generating a batch job to render animation files
#

set GIGARG   = ""
set intfiles = ""
set host = `uname -n`
set curf = curf_${host}
set rendir   = ${GIG_D_ROOT}/projects/renmanadm/${curf}

set task = $1
shift

set arguments = `getopt vr:i:o:idp: $*`

if ( ( $status != 0 ) || ( $#argv == 0 ) ) then
  echo usage: giganibatch [option] animationname startframenr endframenr 
  echo options:
  echo "      -i intercolfile"
  echo "      -r rendercommand"
  echo "      -o outputcommand"
  echo "      -v render verbose"
  echo "      -p projectname"
  echo eg.
  echo "      giganibatch -r size:100x100:25x25+50x50 -r alias:2,-2 -v -i extra test 1 5"
  echo "      giganibatch -r framestep:10 test 1 40 (renders 1, 11, 21, 31)"
  echo "      giganibatch -o path:/usr/tmp -o basename:hallo -o extension:tga test 1 5"
  echo ""
  echo " alias:2,-2   renders video setting from render menu:"
  echo "               |---|---|---|---|"
  echo "               |   |   |   | h | 0    h = high"
  echo "           |---|---|---|---|---|"
  echo "           |   |   |   |   |   | 1    v = video"
  echo "       |---|---|---|---|---|---|"
  echo "       |   |   |   |   | v |   | 2    d = default"
  echo "   |---|---|---|---|---|---|---|"
  echo "   | l |   |   | d |   |   |   | 3    l = low"
  echo "   |---|---|---|---|---|---|---|"
  echo "     3   2   1   0  -1  -2  -3"
  echo ""
  exit
endif

foreach i ( $arguments )
  switch ( $i )
    case '-d'
      set GIGARG = "$GIGARG -d"
      shift arguments
      breaksw
    case '-v'
      set GIGARG = "$GIGARG -v"
      shift arguments
      breaksw
    case '-V'
      set GIGARG = "$GIGARG -V"
      shift arguments
      breaksw
    case '-r'
      set GIGARG = "$GIGARG -r $arguments[2]"
      shift arguments
      shift arguments
      breaksw
    case '-o'
      set GIGARG = "$GIGARG -o $arguments[2]"
      shift arguments
      shift arguments
      breaksw
    case '-p'
      set GIGARG = "$GIGARG -p $arguments[2]"
      set project = $arguments[2]
      shift arguments
      shift arguments
      breaksw
    case '-i'
      set intfiles = "$intfiles $arguments[2]"
      shift arguments
      shift arguments
      breaksw
    case '--'
      shift arguments
      break
  endsw
end

if ( $intfiles != '' ) then
  set intfiles = "-i $intfiles"
endif

if ( $#arguments < 3 ) then
  echo usage: addjob -p project animationname startframenr endframenr 
  echo
  exit
endif

set aniname = $arguments[1]
set anifrm0 = $arguments[2]
set anifrmN = $arguments[3]

shift arguments
shift arguments
shift arguments

set error = 0

set extraintfiles = ""
while ( $#arguments != 0 )
  if ( -e $arguments[1].int ) then
    set extraintfiles = "$extraintfiles $arguments[1]"
    shift arguments
  else
    echo wrong arguments : $arguments[1].int not found
    @ error += 1
    shift arguments
  endif
end

set intfiles = (${intfiles} ${extraintfiles})

if ( $error != 0 ) then
  echo gigtestaddjob exited : job not added
  exit
endif

set nextf = $anifrm0
set lastf = $anifrmN
@ lastf  += 1

if ( $task == 'add' ) then

  echo $GIGARG $intfiles $aniname $anifrm0 $anifrmN >> ${rendir}/queued_anim

  touch ${rendir}/jobaddflag_1
  echo 2 > ${rendir}/jobaddturn
  set turn = `cat ${rendir}/jobaddturn`
  while ( ( -e ${rendir}/jobaddflag_2 ) && ( $turn == '2' ) )
    set turn = `cat ${rendir}/jobaddturn`
  end

  while ( $nextf < $lastf )
    echo a b c $GIGARG $intfiles $aniname $nextf $project ${aniname}${nextf}${project} >> ${rendir}/add_queued_jobs
    @ nextf += 1
  end

  /bin/rm -f ${rendir}/jobaddflag_1

  echo added job : $GIGARG $intfiles $aniname $anifrm0 $anifrmN $extraintfiles

else

  set pattern = ($GIGARG $intfiles $aniname $anifrm0 $anifrmN)
  egrep -v -e "$pattern" ${rendir}/queued_anim >! ${rendir}/queued_anim_tmp
  /bin/mv ${rendir}/queued_anim_tmp ${rendir}/queued_anim

  touch ${rendir}/jobdelflag_1
  echo 2 > ${rendir}/jobdelturn
  set turn = `cat ${rendir}/jobdelturn`
  while ( ( -e ${rendir}/jobdelflag_2 ) && ( $turn == '2' ) )
    set turn = `cat ${rendir}/jobdelturn`
  end

  while ( $nextf < $lastf )
    echo $GIGARG $intfiles $aniname $nextf $project ${aniname}${nextf}${project} >> ${rendir}/del_queued_jobs
    @ nextf += 1
  end

  /bin/rm -f ${rendir}/jobdelflag_1

  echo deleted job : $GIGARG $intfiles $aniname $anifrm0 $anifrmN $extraintfiles

endif
