#/bin/csh -f
# net_batch C Shell script written by BJ MacGowan - Alias Assist January 1992
# This program is provided free of charge and unsupported by Alias Research Inc.

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

if ($#argv < "1") then
	set program = `basename $0`
cat << EOF
Usage: $program -dfms

The $program shell script is used for rendering many sdl files over a 
network of machines. It is possible to assign sdl files to specific machine as 
well as assigning whether an sdl is to be raytraced or raycasted.

All sdl filenames should be relative from the current directory. $program will
verify if the render engines are on a local or remote network. If the files are
on a local network, rendering will happen in either the default Alias Project 
as specified in the .AliasVariables, or in the user defined project. If the 
rendering will happen on a remote network, $program will check first to see if
there is an sdl and pix directory in your home directory of the remote machine.
If the sdl or pix directory does not exist, the directory will be
automatically created for the rendering process and when the renderings are 
complete these directories will be removed.

The output of the sdl will be sent to the sdl directory and placed in a file 
with the name of out.<render engine>.<sdl>. If the sdl exists abnormally
the error message will be placed at the end of the outfile. A log file named
$program.log is also maintained. This file will contain the machine and render
type that the sdl file was rendered with. The log file will also contain the
name of any sdls that did not render properly.

-d default directory. The project directory to use other than the default
   ALIAS_PROJECT directory.
-f batch file name. An ASCII file that can contain host, sdl, and assignment
   variables. Type '$program example' for an example of the batch file
-m machine list. If not specified in the batch file, this option must be used
   for each machine to be used.
-s sdl list. If not specified in the batch file, this option must be used for
   for each sdl to be rendered.

Examples:
$program -d render -m thor -m athena -s sdl/test1 -s r sdl/test2 -s sdl/test3
	In the above example, the default directory has been changed to render, 
	2 machines will be used to render 3 sdls the r placed in front of the 
	second sdl will tell $program to raytrace the file.

$program -f batch_file
	$program will parse through the file named batch_file to get the 
	required rendering variables.

$program -f batch_file -s sdl/test -s sdl/test2
	$program will render sdl/test and sdl/test2 using the parameters 
	found in the file batch_file.
EOF
	exit
endif

# The following explains the ASCII batch file

if ("$argv" == example) then
cat << EOF
                         Sample ASCII batch file:

hosts:                   Required to indicate that the following are the 
                            machines that are to do the rendering.
host_name_1
host_name_2              List of all machine that are to be used for rendering
host_name_3
host_name_4 X 2          To have more than one sdl rendering at a time on a
                            certain host,place an 'X' or 'x' after the host
                            name followed by the number indicating the number
                            of sdls that can render at any one time.

sdl:                     Required to indicate the sdls that are to be rendered.
sdl/tires                If there are more sdl's than render engines, then the
sdl/lights                  engines will be cycled through the render process 
sdl/hood                    as each engine becomes available.
R sdl/car                Placing an 'R' or an 'r' in front of an sdl filename
                            indicates that this sdl is to be raytraced.

assign:                  Required if a specific sdl filename must be rendered 
                            on a specific machine.
host_name_2 sdl/car      These two files will be rendered only by their 
host_name_4 sdl/lights      assigned machines.

directory:               Required to specify a new default directory
car                      The valid Alias project that the rendering will take
                            place in.
EOF
	exit
endif

# Whole bunch of variables that are required for the shell
set log = "net_batch.log"
set check_type = ""
set check_engines = ""
set check_sdl = ""
set render_engines = ""
set render_sdl = ""
set renderer_to_use = ""
set check_renderer = ""
set renderer = ""
set temp_dir = "/usr/tmp/net_batch"
set created_pix = ""
set created_sdl = ""
set sdl_rendered = ""
set sdl_to_render = ""
set engines_free = ""
set engines_rendering = ""
set default_dir = $ALIAS_PROJECT
set do_not_copy = ""
set do_copy = ""
set copy_texture_path = ""
set copy_texture_name = ""
set render_dir = ""
set localhost = `hostname`
set skip_check = ""
set assign_sdl = ""
set sdl_queue = ""
set engine_queue = ""
set render_queue = ""

# Create a directory to store a variety of files that the program will write

/bin/mkdir $temp_dir >& /dev/null
/bin/mkdir $temp_dir/rendering >& /dev/null
/bin/mkdir $temp_dir/remote >& /dev/null
/bin/mkdir $temp_dir/cp_pix >& /dev/null

# Parse through the command line variables 

if ($#argv > 1) then

	set p_count = 1
	set p_count2 = 2

	while ($p_count < $#argv)

		switch ($argv[$p_count])


			case "-d":
				set default_dir = $argv[$p_count2]
			breaksw

			case "-m":
				set check = $argv[$p_count2]
				set file_check = `file $check`

				# Test to see if the -m option is an ascii file if it is then 
				#   use that file to determine the render engines

				if ($file_check[2] == "ascii" || $file_check[2] == "commands") then
					set temp = `cat $check`
					set check_engines = `echo "$check_engines $temp"`
				else 
					set check_engines = `echo "$check_engines $check"`
				endif
			breaksw

			case "-f":
				# This option will read in the batch file and will determine
				#   the host, sdl and assign options.
				set batch = `cat $argv[$p_count2]`
				set count = 1

				while ($count <= $#batch)
					if ($batch[$count] == "hosts:") then
						set check_type = "H"
						@ count ++
						if ($count > $#batch) breaksw
					endif

					if ($batch[$count] == "sdl:") then
						set check_type = "S"
						@ count ++
						if ($count > $#batch) breaksw
					endif

					if ($batch[$count] == "assign:") then 
						set check_type = "A"
						@ count ++
						if ($count > $#batch) breaksw
					endif

					if ($batch[$count] == "directory:") then 
						set check_type = "D"
						@ count ++
						if ($count > $#batch) breaksw
					endif

					if ($check_type == "D" && $count <= $#batch) then
						if ($batch[$count] != "hosts:" && $batch[$count] != "sdl:" && $batch[$count] != "assign:") then
							set default_dir = $batch[$count]
						else 
							set check_type = ""
							@ count --
						endif
					endif

					if ($check_type == "H" && $count <= $#batch) then
					# This will determine the hosts from the batch file
						if ($batch[$count] != "directory:" && $batch[$count] != "sdl:" && $batch[$count] != "assign:") then
							if ($batch[$count] == "x" || $batch[$count] == "X") then
								set temp = `expr $count + 1`
								set times = $batch[$temp]
								set count2 = 1
								set temp = `expr $count - 1`
								while ($count2 < $times)
									set check_engines = `echo "$check_engines $batch[$temp]"`
									@ count2 ++
								end		
								set skip_check = `echo "$skip_check $batch[$temp] $times"`
								@ count ++ 
							else
								set check_engines = `echo "$check_engines $batch[$count]"`
							endif
						else
							set check_engines = "$check_engines"
							@ count --
						endif
					endif

					if ($check_type == "S" && $count <= $#batch) then
					# This will determine the sdls from the batch file
						if ($batch[$count] != "directory:" && $batch[$count] != "hosts:" && $batch[$count] != "assign:") then
							if ($batch[$count] == "r" || $batch[$count] == "R") then
								set r_type = "raytracer"
								@ count ++
							else
								set r_type = "renderer"
							endif
							set check_sdl = `echo "$check_sdl $batch[$count]"`
							set check_renderer = `echo "$check_renderer $r_type"`
						else
							set check_sdl = `echo "$check_sdl"`
							set check_renderer = `echo "$check_renderer"`
						endif
					endif

					if ($check_type == "A" && $count <= $#batch) then 
					# sdl assignments from the batch file
						if ($batch[$count] != "directory:" && $batch[$count] != "hosts:" && $batch[$count] != "sdl:") then
							set count2 = `expr $count + 1`
							set assign_sdl = `echo "$assign_sdl $batch[$count] $batch[$count2]"`
							@ count ++ 
						else
							set assign_sdl = `echo "$assign_sdl"`
							@ count -- 
						endif
					endif

					@ count ++
				end
			breaksw

			case "-s":
			#	set check_sdl = `echo "$check_sdl $argv[$p_count2]"`
				if ($argv[$p_count2] == "r" || $argv[$p_count2] == "R") then
					set r_type = "raytracer"
					@ p_count2 ++
					@ p_count ++
				else
					set r_type = "renderer"
				endif
				set check_sdl = `echo "$check_sdl $argv[$p_count2]"`
				set check_renderer = `echo "$check_renderer $r_type"`
			breaksw
	
		endsw 

		@ p_count = $p_count + 2
		@ p_count2 = $p_count2 + 2
	end
endif

# Verify sdl file
set count = 1

while ($count <= $#check_sdl) 
	if (! -r $check_sdl[$count]) then
		echo "\07\nCannot read $check_sdl[$count]. File $check_sdl[$count] will not be rendered.\n"
	else		
		set render_sdl = `echo "$render_sdl $check_sdl[$count]"`
		set renderer_to_use = `echo "$renderer_to_use $check_renderer[$count]"`
	endif

	@ count ++
end

# Verify the render engines to see if they are accessible

if ("$check_engines" != "") then
	set count = 1
	echo -n "Validating Hosts. "
	while ($count <= $#check_engines)
		rsh $check_engines[$count] date >& /dev/null
		if ($status != 0) then
			echo "\n\07Host $check_engines[$count] not validated. $check_engines[$count] will not be used.\n"
		else
			set render_engines = `echo "$render_engines $check_engines[$count]"`
		endif
		set temp = 1
		while ($temp < $#skip_check)
			if ($check_engines[$count] == $skip_check[$temp]) then
				set temp = `expr $temp + 1`
				set times = `expr $skip_check[$temp] - 1`
				@ count = $count + $times
				set temp2 = 1
				while ($temp2 <= $times)
					set render_engines = `echo "$render_engines $check_engines[$count]"`
					@ temp2 ++
				end
			endif
			@ temp ++
		end
		@ count ++
	end
endif

# If no valid render engines were found then use the local host

if ("$render_engines" == "") then
	set render_engines = `hostname`
	echo "No render engines were found defaulting to $render_engines"
endif

echo "Done"

# Check to see if remote directory is the current directory, if it is then
#   there will be no need to copy over the various files for rendering.

echo -n "Verifying paths. "
# A probe file is written to the current directory, if the probe file can be
#   found on the remote machine then the machine is on the local nfs network.

date > .probe
set count = 1

while ($count <= $#render_engines)
	set stat = `rsh $render_engines[$count] "if (-r $ALIAS_WORKENV/user_data/$default_dir/.probe) echo '$status'"`
	# if stat does not equal 0 then the probe file could not be found.
	if ($stat != "0") then 
		echo $render_engines[$count] >> $temp_dir/copy
		set do_copy = `echo "$do_copy $render_engines[$count]"`
	endif
	@ count ++
end
echo "Done"

# Determine the output pix file name for all of the sdl's. This is required so
#   that in the case the sdl is rendered on a remote network the output pix
#   file can be copied back to the local pix directory.
echo -n "Determining output pix file name(s). "
set count = 1

while ($count <= $#render_sdl)
	grep "pix =" $render_sdl[$count] > $temp_dir/grep
	nawk '{FS = "[\"]"; print $2}' $temp_dir/grep > $temp_dir/grep2
	set pix = `nawk '{FS = "/";print $(NF-1)"/"$NF}' $temp_dir/grep2`
	echo "$render_sdl[$count] $pix" >> $temp_dir/pix

# Determine texture/background pix names that may be required for the rendering

	grep 'image =' $render_sdl[$count] > $temp_dir/grep
	grep 'floor_texture =' $render_sdl[$count] >> $temp_dir/grep
	grep 'cloud_texture =' $render_sdl[$count] >> $temp_dir/grep
	grep 'right = "' $render_sdl[$count] >> $temp_dir/grep
	grep 'left  = "' $render_sdl[$count] >> $temp_dir/grep
	grep 'top   =' $render_sdl[$count] >> $temp_dir/grep
	grep 'bottom =' $render_sdl[$count] >> $temp_dir/grep
	grep 'back =' $render_sdl[$count] >> $temp_dir/grep
	grep 'front =' $render_sdl[$count] >> $temp_dir/grep
	grep 'background = "' $render_sdl[$count] >> $temp_dir/grep
	nawk '{FS = "[\"]"; print $2}' $temp_dir/grep > $temp_dir/grep2

	# Determine the path name and then strip off all but the last 2 directory
	#   names so that the files can be rcp'ed properly.
	set texture_path = `cat $temp_dir/grep2`
	set texture = `nawk '{FS = "/";print $(NF-1)"/"$NF}' $temp_dir/grep2`

	set edit_texture_path = `sed -e 's/\//\\\//g' $temp_dir/grep2`
	nawk '{FS = "/";print $(NF-1)"/"$NF}' $temp_dir/grep2 > $temp_dir/grep3
	set edit_texture_name = `sed -e 's/\//\\\//g' $temp_dir/grep3`

	echo "$render_sdl[$count] $texture" >> $temp_dir/texture
	echo "$render_sdl[$count] $texture_path" >> $temp_dir/texture_path
	echo "$render_sdl[$count] $edit_texture_path\n" >> $temp_dir/edit_texture_path
	echo "$render_sdl[$count] $edit_texture_name\n" >> $temp_dir/edit_texture_name

@ count ++
end
echo "Done\n"

# Test to see if there is a writeable pix and sdl directory on the remote
#   network Host, if not create temporary directories

if ("$do_copy" != "") then
set count = 1

while ($count <= $#do_copy)
	set host_test = $do_copy[$count]

	set test_pix = `rsh $host_test "if (-d pix && -w pix) echo $status"`
	set test_sdl = `rsh $host_test "if (-d sdl && -w sdl) echo $status"`

	if ($test_pix != "0") then
		echo "Creating temporary pix directory on Host $host_test"
		rsh $host_test mkdir pix >& /dev/null
		if ($status == 0) then
			set created_pix = `echo "$created_pix $host_test"`
		else
			echo "Could not create a temporary pix directory on $hosts_test. Aborting render process"
			exit
		endif
	endif

	if ($test_sdl != "0") then
		echo "Creating temporary sdl directory on Host $host_test"
		rsh $host_test mkdir sdl >& /dev/null
		if ($status == 0) then
			set created_sdl = `echo "$created_sdl $host_test"`
		else
			echo "Could not create a temporary sdl directory on $host_test. Aborting render process"
			exit
		endif
	endif
	@ count ++
end
endif

# Start of the Render Section
set sdl_to_render = ($render_sdl)
set engines_free = ($render_engines)
set engine_count = $#render_engines
set r_count = 1
set e_count = 1
set a_count = 1 

echo "Starting Render Process\n"
echo "\n`date`" >> $log
Render_Loop:

while ($e_count <= $engine_count)
	# If there are no more sdls to render go back to the Search section
	if ($r_count > $#sdl_to_render) goto Start_of_Search
	# If there are no more engine to render on go back to the Search section
	if ("$engines_free" == "") goto Start_of_Search

	# If there are any sdls assigned to specific machines render them first
	if ("$assign_sdl" != "") then
		set engine = $assign_sdl[1]
		set to_render = $assign_sdl[2]
		set a_temp = 1
		while ($a_temp <= $#sdl_to_render) 
			if ($to_render == $sdl_to_render[$a_temp]) then
				set renderer = $renderer_to_use[$a_temp]
				set renderer_to_use[$a_temp] = ""
				set sdl_to_render[$a_temp] = ""
				set assign_sdl[1] = ""
				set assign_sdl[2] = ""
				set temp2 = 1
				set as = ""
				while ($temp2 <= $#assign_sdl)
					if ($assign_sdl[$temp2] != "") then
						set as = `echo "$as $assign_sdl[$temp2]"`
					endif
					@ temp2 ++
				end
				set r = ""
				set s = ""
				set temp2 = 1
				while ($temp2 <= $#sdl_to_render)
					if ($sdl_to_render[$temp2] != "") then
						set s = `echo "$s $sdl_to_render[$temp2]"`
						set r = `echo "$r $renderer_to_use[$temp2]"`
					endif
					@ temp2 ++
				end
				set checked_once = "FALSE"
				set temp2 = 1
				while ($temp2 <= $#engines_free)
					if ($engine == $engines_free[$temp2] && $checked_once == "FALSE") then
						set checked_once = "TRUE"
						set engines_free[$temp2] = ""
					endif				
					@ temp2 ++
				end
				set assign_sdl = ($as)
				set sdl_to_render = ($s)
				set renderer_to_use = ($r)
				set r_count = `expr $r_count - 1`
			endif
			@ a_temp ++
		end
	else
		# Once all of the assigned sdl have been rendered, render the
		#   remaining sdls
		set engine = $engines_free
		set engines_free[1] = ""
		set renderer = $renderer_to_use[$r_count]
		set to_render = $sdl_to_render[$r_count]
	endif

	# Determine what the outfile name should be called

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

	# Determine if the machine to be rendered on is on a local or remote network
	grep $engine $temp_dir/copy >& /dev/null
	if ($status == 0) then
		set render_dir = $to_render

		# Start of Remote Render Section
		# Some editing may have to be done to the sdl, so make a copy of the sdl
		/bin/cp $to_render $to_render.temp

		set copy_texture_path = `grep $to_render $temp_dir/texture_path`
		set copy_texture_name = `grep $to_render $temp_dir/texture`
		set edit_texture_path = `grep $to_render $temp_dir/edit_texture_path`
		set edit_texture_name = `grep $to_render $temp_dir/edit_texture_name`

		if ("$copy_texture_path" != "") then
			set count2 = 2
			while ($count2 <= $#copy_texture_path)

			# Before copying over and pix files test the remote directory to see
			#   if the file exists or not, if it exists then there is no need to
			#   copy the pix files.For each pix file that is to be copied the 
			#   machine name and pix filename are copied into a file so that the
			#   name can be referenced for later removal
				set test_exist = `rsh $engine "if (-r $copy_texture_name[$count2]) echo $status"`
				if ($test_exist != "0") then
					rcp $copy_texture_path[$count2] $engine":"$copy_texture_name[$count2] >& /dev/null
					echo $copy_texture_name[$count2] >> $temp_dir/cp_pix/$engine
				endif
			@ count2 ++
			end
		endif	

		# Edit the sdl to change the support pix file paths so that the sdl will
		#   render properly.
		cp $to_render $to_render.temp
		# If there are no textures in the sdl then there is no need to edit it.
		if ($#edit_texture_path < 2) goto SKIP_OVER
			set count2 = 2
			while ($count2 <= $#copy_texture_path)
				set orig = $edit_texture_path[$count2]
				set new = $edit_texture_name[$count2]
				set replace = `echo "/$edit_texture_path[$count2]/s//$edit_texture_name[$count2]"`
				ex -s $to_render.temp << EOF >& /dev/null
   				$replace
				w $to_render.temp
				q
EOF
				@ count2 ++
			end
SKIP_OVER:
		rcp $to_render".temp" $engine":"$to_render
		/bin/rm $to_render".temp"

		echo $to_render >> $temp_dir/remote/$engine

		# When a c-shell is invoked as a background process the process ID is
		#   sent to the screen, as well as a message when the process is 
		#   finished. To get around this the renderer will be invoked under 
		#   bourne shell. However we have to write out required variables to 
		#   disk and then cat them back in

		echo $render_dir > .render_dir
		echo $engine > .engine
		echo $to_render > .to_render
		echo $renderer > .renderer
		echo $out_file > .out_file
		echo $temp_dir > .temp_dir

		# sh -cf invokes the bourne shell

		# First read in the required variables for the Bourne shell and then
		#   start up the renderers

		sh -cf 'cat="/bin/cat";to_render=`$cat .to_render`;\
		renderer=`$cat .renderer`; out_file=`$cat .out_file`;\
		engine=`$cat .engine`; render_dir=`cat .render_dir`;\
		temp_dir=`$cat .temp_dir`;echo `date` >sdl/out.$engine.$out_file;\
		rsh $engine $renderer $to_render >>sdl/out.$engine.$out_file 2>&1 &'

		echo "Rendering $to_render on Engine $engine using $renderer" | tee -a $log
		echo $to_render >> $temp_dir/rendering/$engine
		set engine_queue = `echo "$engine_queue $engine"`
		set render_queue = `echo "$render_queue $renderer"`
		set sdl_queue = `echo "$sdl_queue $to_render"`

	else 
	# Start of Local Render Section

		set render_dir = "$ALIAS_WORKENV/user_data/$default_dir/"
		echo $render_dir > .render_dir
		echo $engine > .engine
		echo $to_render > .to_render
		echo $renderer > .renderer
		echo $out_file > .out_file
		echo $temp_dir > .temp_dir

		# sh -cf invokes the bourne shell

		# First read in the required variables for the Bourne shell and then 
		#   start up the renderers
		sh -cf 'cat="/bin/cat";to_render=`$cat .to_render`;\
		renderer=`$cat .renderer`; out_file=`$cat .out_file`;\
		engine=`$cat .engine`; render_dir=`cat .render_dir`;\
		temp_dir=`$cat .temp_dir`;echo `date` >sdl/out.$engine.$out_file;\
		rsh $engine "cd $render_dir;$renderer $to_render" >>sdl/out.$engine.$out_file 2>&1 &'

		echo "Rendering $to_render on Engine $engine using $renderer" | tee -a $log
		echo $to_render >> $temp_dir/rendering/$engine
		set engine_queue = `echo "$engine_queue $engine"`
		set render_queue = `echo "$render_queue $renderer"`
		set sdl_queue = `echo "$sdl_queue $to_render"`
	endif

	# When a render engine has started the name is taken off of the list of
	#    available engines.

		set count3 = 1
		set ef = ""

		echo "" > $temp_dir/engines_free
		while ($count3 <= $#engines_free)
			if ($engines_free[$count3] != "") then
				set ef = `echo "$ef $engines_free[$count3]"`		
			endif
			@ count3 ++
		end
		set engines_free = ($ef)
	@ r_count ++
	@ e_count ++
end

Start_of_Search:
# Run through the list of render engines to see if they are still running

set c_count = 1

while ($c_count <= $#engine_queue)
	set check_render = $sdl_queue[$c_count]
	set engine = $engine_queue[$c_count]
	set renderer = $render_queue[$c_count]

	if ($engine != $localhost) then
		rsh $engine ps -ef | grep "$renderer $check_render" >& /dev/null
		set Status = $status
	else
		ps -ef | grep "$renderer $check_render" | grep -v "grep">& /dev/null
		set Status = $status
	endif

	# When a render engine has finished the name is taken off of the list of
	#   engines to check.

	if ($Status != "0") then 
		echo "$engine has finished rendering"
		set out_name = `expr //$check_render : '.*/\(.*\)'`

		set Done = `fgrep -s done sdl/out.$engine.$out_name`
		if ($status == 0) then
			echo `date` >> sdl/out.$engine.$out_name
		else
			echo $engine $to_render out.$engine.$out_name >>$temp_dir/abnormal
		endif

		set engines_free = `echo "$engines_free $engine"`
		set engine_queue[$c_count] = ""
		set sdl_queue[$c_count] = ""
		set c_temp = 1
		set temp_rq = ""
		set temp_eq = ""
		set temp_sq = ""

		while ($c_temp <= $#engine_queue)
			if ($engine_queue[$c_temp] != "") then
				set temp_eq =  `echo "$temp_eq $engine_queue[$c_temp]"`
				set temp_rq =  `echo "$temp_rq $render_queue[$c_temp]"`
				set temp_sq =  `echo "$temp_sq $sdl_queue[$c_temp]"`
  			endif
			@ c_temp ++
		end
		set engine_queue = ($temp_eq)
		set render_queue = ($temp_rq)
		set sdl_queue = ($temp_sq)
	endif
	@ c_count ++
end

# If there are no more sdls to render either continue to search to see of the
#   render engines are still running, or go to the Clean_Up section.
if ($r_count > $#sdl_to_render) then 
	if ("$render_queue" != "") then
		goto Start_of_Search
	else
		goto Clean_Up
	endif	
else	
	# If there are more sdls to render first see if there are any render
	#   engines that are free that can do the job, if not go back to the search
	#   section to see if any have become free. If there are free engines then
	#   go up to the Render Section
	if ("$engines_free" == "") then
		goto Start_of_Search
	else
		set e_count = 1
		goto Render_Loop
	endif
endif

Clean_Up:

# Once every thing has been rendered copy back any files on the remote network
#   machines, and remove any files or directories that were created.

echo -n "\nCopying remote files back to the default directory. "

# See if anything was rendered on a remote machine
set remote = `/bin/ls $temp_dir/remote` >& /dev/null

if ("$remote" != "") then
	set count = 1
	while ($count <= $#remote)
		set remote_copies = `cat $temp_dir/remote/$remote[$count]`
		set count2 = 1
		while ($count2 <= $#remote_copies)
			# Determine what files have to be copied back, do it and then
			#   remove the files from the remote machine
			set copy_pix = `fgrep $remote_copies[$count2] $temp_dir/pix`
			rcp $remote[$count]":"$copy_pix[2] $copy_pix[2] >& /dev/null
			rsh remote /bin/rm $copy_pix[2] >& /dev/null
			@ count2 ++
		end
	@ count ++
	end

	# Remove any support files that may have been placed on the remote machines
	set to_rm = `/bin/ls $temp_dir/cp_pix` >& /dev/null
	if ("$to_rm" != "") then
		set count = 1
		while ($count <= $#to_rm)
			set rm_this = `cat $temp_dir/cp_pix/$to_rm[$count]`
			set count2 = 1
			while ($count2 <= $#rm_this)
				rsh $to_rm /bin/rm $rm_this[$count2] >& /dev/null
				@ count2 ++
			end
			@ count ++
		end	
	endif

endif
# All of the files have been rendered and have been copied back to the local
#   machine
echo "Done\07"

# Check to see if any of the sdls had exited abnormally
if (-r $temp_dir/abnormal) then
	set abnormal = `cat $temp_dir/abnormal`

	if ("$abnormal" != "") then
		set count1 = 1
		set count2 = 2
		set count3 = 3
		set msg = ""
		if ($#abnormal > 3) set msg = "s"
			echo "\nThe following file$msg exited abnormally.\n"| tee  -a $log 
			echo "Render engine\t\tSDL filename\t\tout filename\n" | tee -a $log
		endif
		while ($count1 <= $#abnormal)
			echo "$abnormal[$count1]\t\t\t$abnormal[$count2]\t\t$abnormal[$count3]"|tee -a $log
		@ count1 = $count1 + 3
		@ count2 = $count2 + 3
		@ count3 = $count3 + 3
		end
		echo "\nVerify the sdl$msg before restarting the render process." | tee -a $log
	endif
endif

interrupt:
# If a temporary sdl directory was created on the remote machine then 
#   remove it

if ("$created_sdl" != "") then
	set count = 1
	while ($count <= $#created_sdl)
		rsh $created_sdl[$count] /bin/rm -fr sdl >& /dev/null
	@ count ++
	end
endif

# If a temporary pix directory was created on the remote machine then 
#   remove it

if ("$created_pix" != "") then
	set count = 1
	while ($count <= $#created_pix)
		rsh $created_pix[$count] /bin/rm -fr pix >& /dev/null
	@ count ++
	end
endif

# Clean up any files/directories that have been created on the local machine.

/bin/rm .engine >& /dev/null
/bin/rm .out_file >& /dev/null
/bin/rm .probe >& /dev/null
/bin/rm .render_dir >& /dev/null
/bin/rm .renderer >& /dev/null
/bin/rm .to_render >& /dev/null
/bin/rm .temp_dir >& /dev/null
/bin/rm -fr $temp_dir >& /dev/null

exit
