#!/bin/csh -f

# slice C Shell script written by BJ MacGowan - Alias Assist October 1991
# revised 3 Feb 1992
# This program is provided free of charge and unsupported by Alias Research Inc.

# If the user hits ^C the shell will goto the interrupt rountine at the end
#   of the shell
onintr interrupt

set program = `basename $0`

if ($#argv < 1) then
	cat << EOF

Usage: $program [-efkprs] <pix>

The $program shell script is used for finishing an Alias pix file that has been
only partially rendered.  $program will determine the last scanline completed in
the original pix filename and will render a new file starting at the next
scanline of the original pix filename.  Upon completion of the new render the
two files will be combined to make a complete pix file. 

-e edit and exit.  A new sdl will be created that will be editted with the
   proper values for the remaining part of the imcomplete image. Once the 
   sdl has been editted $program will exit without rendering the sdl.
-f startframe number.  If not supplied the default startframe number is 1
-k keep original filenames.  There are no arguments to the option.  The -
-p output pix filename.  If not supplied supplied the final output pix
   filename will be the same as the original incomplete pix file.
-r raytracer. There are no arguments to the option.  The raytracer will
   used instead of the renderer.
-s sdl filename.  If not supplied the default file to be rendered is assumed
   to be the same filename as the pix name.

EOF
	exit 1
endif

# Set up the initial defaults

set count = 1
set count2 = 2
set startframe = 1
set keep = "FALSE"
set renderer = "renderer"
set temp_dir = "/usr/tmp/slice"
set pix = "$argv[$#argv]"
set final_pix = "$argv[$#argv]"
set sdl = ""
set log = "$program.log"
set Edit = "FALSE"

# Make a temporary directory
mkdir $temp_dir >& /dev/null

# Parse through the command line arguments

if ($#argv > 1) then
	while ($count < $#argv)
		switch ($argv[$count])

			case "-e":
				set Edit = "TRUE"
				@ count --
				@ count2 --
			breaksw

			case "-s":
				set sdl = "$argv[$count2]"
			breaksw

			case "-f":
				set startframe = $argv[$count2]
			breaksw

			case "-p":
				set final_pix = "$argv[$count2]"
			breaksw

			case "-k":
				set keep = "TRUE"
				@ count --
			breaksw

			case "-r":
				set renderer = "raytracer"
				@ count --
				@ count2 --
			breaksw
		endsw 
		@ count = $count + 2
		@ count2 = $count2 + 2
	end
endif

# If there is only one command line argument then make the sdl and pix name
#   equal to the argument

if ($sdl == "") set sdl = "sdl/`expr //$pix : '.*/\(.*\)'`" 

# Set todays date into the next line of the log file
echo "\n`date`" >> $log

# Test to see if the specified pix file can be read

if (! -r $pix) then
	echo "\07Cannot read $pix"
	exit
endif

# Test to see if the specified sdl file can be read

if (! -r $sdl) then
	echo "\07Cannot read $sdl"
	exit
endif

# Determine the out_file name

set out_file = `expr //$pix : '.*/\(.*\)'`

# If the sdl has a ramp background the resulting combined pix file will be
#   incorrect
	fgrep -s 'background = ramp(' $sdl
	if ($status == 0) then
		echo "\07\n"
		echo "$sdl has a background ramp assigned to it." | tee -a $log
		echo "Background ramps can create incorrect results" | tee -a $log
		echo -n "Do you wish to continue (y/N) "
		set reply = $<
		if ($reply == "n" || $reply == "N" || $reply == "") then
			echo "Exiting from the render process" | tee -a $log
			goto interrupt
		endif
	endif

# Read the pix file to determine how much of the image was completed.
#   Variable wanted is what the pix file size should be when completed.
#   Variable done is how much of the original pix file was rendered.

set wanted = `readrl $pix | head -1 | cut -d: -f3` 
set done = `fstats $pix | cut -d= -f3`            
if ($done == $wanted) then
	echo "\07$pix is complete."
	exit
endif

@ wanted --
@ remain = $wanted - $done

set has_background = "FALSE"
grep 'background = "' $sdl > $temp_dir/bg1
if ($status == 0) set has_background = "TRUE"

if ($has_background == "TRUE") then
	echo -n "Slicing the background file. "
	nawk '{FS = "[\"]"; print $2}' $temp_dir/bg1 > $temp_dir/bg2
	set bg_path = `cat $temp_dir/bg2`
	set background = `nawk '{FS = "/";print $(NF-1)"/"$NF}' $temp_dir/bg2`
	set vi_bg = `nawk '{FS = "/";print $1"\\/"$2}' $temp_dir/bg2`

	# bg_path: is the full path of the background
	# background: is the path for the background on the remote machine
	# vi_bg: places special control characters into the background path so that
	#        when the sdl files are editted the / in the path will be 
	#        interpeted properly.
	
	# Determine the X and Y resolution of the background
	set xres = `readrl $bg_path | head -1 | cut -d" " -f2`


	# Since the original background has to be sliced up we have to determine
	#   how big those slices must be.

	# The resolution for the slices must start at 0 so the X & Y resolutions
	#   must be decreased by 1  
	set xres = `echo "$xres - 1" | bc`
	set yhigh = $remain 

	# Convert the background to SGI RGB format so that it can be sliced
	fromalias $bg_path $background.rgb 

	set ylow = 0
	
	# subimg is an SGI utility that will take a subsection of an image
	subimg $background.rgb $background.rgb.slice 0 $xres $ylow $yhigh 
	# Convert the new background slice back to Alias format for rendering
	toalias $background.rgb.slice $background.slice

	# Remove any .rgb files that were created
	/bin/rm $background.rgb*
	echo "Done"
endif 

# Edit the sdl so that the proper amount is rendered

if ($has_background != "TRUE") then
	ex -s $sdl << EOF
	/yhigh  = $wanted/s//yhigh  = $remain
	/yhigh)/s//$wanted)
	/(yhigh/s//($wanted
	w! sdl/$out_file.slice
	q
EOF
else
	ex -s $sdl << EOF
	/yhigh  = $wanted/s//yhigh  = $remain
	/yhigh)/s//$wanted)
	/(yhigh/s//($wanted
	/$vi_bg/s//$vi_bg.slice
	w! sdl/$out_file.slice
	q
EOF
endif

if ($Edit == "TRUE") then
	echo "\n\07"
	echo "sdl/$out_file.slice has been created." | tee -a $log
	exit 1
endif

# Do the rendering

echo -n "$pix \t Rendering $remain remaining scanlines of $wanted. "|tee -a $log

echo $pix > .pix
echo $out_file > .out_file
echo $startframe > .startframe
echo $renderer > .renderer

# The rendering will be done using a bourne shell, this way if the renderer
#   exits abnormally the output can be sent to the out file.

sh -cf 'cat="/bin/cat";startframe=`$cat .startframe`;\
out_file=`$cat .out_file`;pix=`$cat .pix`;renderer=`$cat .renderer`\
$renderer -s$startframe -p $pix.slice sdl/$out_file.slice > sdl/out.$out_file 2>&1'

# Test to see if the file exited normally
set done = `fgrep -s done sdl/out.$out_file`
if ($status != 0) then
	echo "\07\n"
	echo "\n$sdl exited abnormally. " | tee -a $log
	echo "Verify sdl/out.$out_file before restarting the render process" | tee -a $log
	goto interrupt
else
	echo "Done" | tee -a $log
endif

# Paste the two files together

echo -n "Pasting $pix together. "
fpaste $pix $pix.slice $pix.combined
echo "Done"

# See if the user wants to keep the separate rendered pieces. If $keep does not
#   equal TRUE then rename the combined pix file to the original pix filename 
#   and remove the rendered slice

if ($keep != "TRUE") then
	/bin/mv $pix.combined $final_pix
	/bin/rm $pix.slice
	/bin/rm $background.slice
	/bin/rm sdl/$out_file.slice
endif

echo "\07\n$pix has been completed" | tee -a $log

interrupt:
/bin/rm .out_file >& /dev/null
/bin/rm .pix >& /dev/null
/bin/rm .startframe >& /dev/null
/bin/rm .renderer >& /dev/null
/bin/rm -fr $temp_dir >& /dev/null
exit
