#! /bin/csh -f

# Convert between nTITLE and (other) cell numbering style.
# ie: 003_akbar.sgi <-> jeff.003
# April 13 1992    Keith Gordon
#

# Get args and do a little error catching
if ( $#argv != 7 ) then
	goto usage
endif

set self	= $0
set self	= self:t
@ start		= ( $1 )
@ end		= ( $2 )
set from_name	= $3
set from_suffix	= $4
set to_name	= $5
set to_suffix	= $6
set direction	= $7
@ remaining	= ( $end - $start )
@ remaining	= ( $remaining + 1 )
@ done		= ( 0 )
@ frame		= ( $start )

if ( $direction != "ntitle" && $direction != "other" ) then
	goto usage
endif

if( $direction == "ntitle" ) then
	goto to_ntitle
else
	goto to_other
endif
exit -1

# Convert from (other) to nTITLE format.
to_ntitle:
while ( $remaining )
	if( $frame < 10 ) then
		set from_file = $from_name.00$frame
		set to_file   = "00"$frame"_"$to_name.$to_suffix
	else if ( $frame < 100 ) then
		set from_file = $from_name.0$frame
		set to_file   = "0"$frame"_"$to_name.$to_suffix
	else
		set from_file = $from_name.$frame
		set to_file   = $frame"_"$to_name.$to_suffix
	endif

	# Move the file.
	echo "mv $from_file $to_file"
	mv $from_file $to_file
	
	@ remaining	= ( $remaining  - 1 )
	@ frame		= ( $frame + 1 )
	@ done		= ( $done + 1 )
end
exit 0

# Convert from nTITLE format to (other) format.
to_other:
while ( $remaining )
	if( $frame < 10 ) then
		set from_file = "00"$frame"_"$from_name.$from_suffix
		set to_file   = $to_name.00$frame
	else if ( $frame < 100 ) then
		set from_file = "0"$frame"_"$from_name.$from_suffix
		set to_file   = $to_name.0$frame
	else
		set from_file = $frame"_"$from_name.$from_suffix
		set to_file   = $to_name.$frame
	endif

	# Move the file.
	echo "mv $from_file $to_file"
	mv $from_file $to_file

	@ remaining	= ( $remaining  - 1 )
	@ frame		= ( $frame + 1 )
	@ done		= ( $done + 1 )
end
exit 0


# Print usage info...
usage:
	echo "Convert animation from nTITLE to (other) numbering style"
	echo "usage: $0 start end fromname fromsuffix toname tosuffix ntitle|other"
	echo "$0 0 30 akbar sgi jeff bleh other"
	echo "	to go from ###_akbar.sgi to jeff.###"
	echo "$0 0 30 jeff bleh akbar sgi ntitle"
	echo "	to go from  jeff.### to ###_akbar.sgi"
	exit 0

