#!/bin/csh -f
#************************************************************************
#
# Name  : newGrab
#
# Date  : 10/02/1992
#
# Author: Alex Urrutia (Softimage)
#
#************************************************************************


if ( "$1" == "-h" ) then
    echo
    echo "This is version 1.0 of Softimage newGrab (dated 02 Oct 1992)"
    echo
    exit 0
endif

if ( $#argv == 0 ) then
    goto usage
endif

# Set Defaults
set name = "default"
set rate = 30
set path = (. $path /usr/softimage/bin)

while ( $#argv > 0 )
    if ( "$1" !~ \-* ) then
	set name = $1
	if ($?timeCode) then
		break
	endif
	echo "ERROR: No Time Code set."
	echo "NOTE : Order of parameters must be preserved.  See usage."
	exit -1
    endif
    switch ( "$1" )
	case -b:
	    shift
	    set	bufferType = $1
	    breaksw
	case -pal:
	    set	rate = 25
	    breaksw
	case -t:
	    shift
	    set	timeCode = $1
	    breaksw
	case -v:
	    set	verb
	    breaksw
	case -s:
	    shift
	    @ start = $1
	    shift
	    @ end = $1
	    breaksw
	default:
	    goto usage
    endsw
    shift
end

if (! $?start) then
	echo "ERROR: Start and End frames have to be specified."
	exit -1
endif

# MAIN

# Break Time Code

echo $timeCode | sed 's/:/./g' > tmp
set timeCode = `cat tmp`
rm tmp
@ frames = $timeCode:e
if ($frames >= $rate) then
	echo "ERROR: Invalid number of frames: $frames"
	exit -1
endif
set timeCode = $timeCode:r
@ seconds = $timeCode:e
if ($seconds >= 60) then
	echo "ERROR: Invalid number of seconds: $seconds"
	exit -1
endif
set timeCode = $timeCode:r
@ minutes = $timeCode:e
if ($minutes >= 60) then
	echo "ERROR: Invalid number of minutes: $minutes"
	exit -1
endif
set timeCode = $timeCode:r
@ hours = $timeCode

set COUNT = $start
set STOP = $end

if ($frames < 10) then
	set frames = "0$frames"
endif
if ($seconds < 10) then
	set seconds = "0$seconds"
endif
if ($minutes < 10) then
	set minutes = "0$minutes"
endif
if ($hours < 10) then
	set hours = "0$hours"
endif

while ( $COUNT <= $STOP)
	
	set timeCode = ""$hours":"$minutes":"$seconds":"$frames""
	if ($?bufferType) then
		if ($?verb) then
			echo "grab -b $bufferType -v $timeCode $name"
		endif
		grab -b $bufferType -v $timeCode $name
		if ($status != 0) then
			echo "ERROR: cannot execute grab -b $buffertype -v $timeCode $name"
			exit -1
		endif

	else
		if ($?verb) then
			echo "grab -v $timeCode $name"
		endif
		grab -v $timeCode $name
		if ($status != 0) then
			echo "ERROR: cannot execute grab -v $timeCode $name"
			exit -1
		endif
	endif

	mv $name $name.sgi
	sgi2soft $name $name.$COUNT
	rm $name.sgi

	@ frames ++
	if ($frames < 10) then
		set frames = "0$frames"
	endif
	if ($frames >= $rate) then
		set frames = "00"
		@ seconds ++
		if ($seconds < 10) then
			set seconds = "0$seconds"
		endif
		if ($seconds >= 60) then
			set seconds = "00"
			@ minutes ++
			if ($minutes < 10) then
				set minutes = "0$minutes"
			endif
			if ($minutes >= 60) then
				set minutes = "00"
				@ hours ++
			endif
		endif
	endif
	@ COUNT ++
end


exit 0

#  Usage message.
#

usage:

echo
echo "Usage: newGrab -s <start> <end> [-b Buffertype] -t Timecode [-pal] [-v] [Name]"
echo "               -b   : Do a man grab to see valid Buffertypes "
echo "               -t   : Timecode in the following format:  "
echo "                      hour:minute:second:frame "
echo "               -pal : For PAL users "
echo "               -v   : Verbose "
echo "               -h   : gives you version info."
echo
exit 0
