#!/bin/csh -f
#
#       %W% %E%
#

# called to get info on specified job

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

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

# get animation and project name
foreach i ( $arguments )
  switch ( $i )
    case '-d'
      shift arguments
      breaksw
    case '-v'
      shift arguments
      breaksw
    case '-V'
      shift arguments
      breaksw
    case '-r'
      shift arguments
      shift arguments
      breaksw
    case '-o'
      shift arguments
      shift arguments
      breaksw
    case '-p'
      set project = $arguments[2]
      shift arguments
      shift arguments
      breaksw
    case '-i'
      shift arguments
      shift arguments
      breaksw
    case '--'
      shift arguments
      break
  endsw
end

set aniname  = $arguments[1]

set argc     = $#argv
set gigargc  = $argc 
@   gigargc -= 2

set i = 0
set gigargv = ''
while ( $i < $gigargc )
  set gigargv = ($gigargv $argv[1])
  shift
  @ i += 1
end

if ( -e $rendir"/gigdonejobs_tmp" ) then
  /bin/rm $rendir"/gigdonejobs_tmp"
endif
touch $rendir"/gigdonejobs_tmp"

# you are right, the next part can be done smarter

set i = $argv[1]
set mach = ''
set ftim = ''

while ( $i <= $argv[2] )
  set entry = `egrep -e "$gigargv $i $project" $rendir"/gigdonejobs"`
  set count = `echo $entry | egrep error | awk '{print NF;exit}'`

  if ( $count != '' && $count != 0 ) then
    shift entry
    shift entry
    set cnt = 3
    while ( $entry[1] != 'error' && $cnt < $count )
      shift entry
      @ cnt += 1
    end
    if ( $cnt == $count ) then
      echo frame $i of animation $aniname in project $project NOT rendered : Unknown error
    else
      echo frame $i of animation $aniname in project $project NOT rendered : $entry
    endif
    
  else 
    echo $entry >> $rendir"/gigdonejobs_tmp"
    set indx = $#entry
    set mach = $entry[$indx]
    if ( $mach != '' ) then
      @ indx -= 1
      set ftim = $entry[$indx]
      echo frame $i of animation $aniname in project $project rendered in $ftim sec on $mach
    else
      echo frame $i of animation $aniname in project $project NOT rendered
    endif
    
  endif
  @ i += 1
    
end

set total = `cat $rendir"/gigdonejobs_tmp" | awk 'BEGIN{}{s+=$(NF-1)}END{print s}' s=0 -`

echo Total render time : $total sec

/bin/rm $rendir"/gigdonejobs_tmp"
