#!/bin/csh -f
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
#  This is a csh script used to create a movie intro using Wavefront.
#
#
#  Author:     Wesley C. Barris
#              AHPCRC
#              Minnesota Supercomputer Center, Inc.
#  Date:       Sept. 20, 1990
#
#  Copyright @ 1990, Minnesota Supercomputer Center, Inc.
#
#  RESTRICTED RIGHTS LEGEND
#
#  Use, duplication, or disclosure of this software and its documentation
#  by the Government is subject to restrictions as set forth in subdivision
#  { (b) (3) (ii) } of the Rights in Technical Data and Computer Software
#  clause at 52.227-7013.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
#  Some defaults...
#
set format	= "qtl_ntsc"
set image	= "rla"
set job_name	= "movie"
set mode	= "fade"
set dest	= "rla"	#[rla, qtl, abekas n, rle, sgi]
set data_location = "undefined"
set cmap	= "undefined"
set background	= "undefined"
set font	= "news-bold"
set intro_data  = "undefined"
set rflmap	= "chrome.txc"
set rflext	= ".txc"
set scale	= 1.0
@ betacam	= 0
@ delete	= 1
@ fperscn	= 60	#frames per scene -- 60 is probably good
@ nfades	= 30	#number of frames per fade -- 30 is probably good
@ aframe	= -1    #starting location for record on Abekas
#
#  Check for the intro data file.
#
if ($#argv == 0) then
   echo "Usage: intro [-j job_name] [-f|r|z|s] [-nd] [-dest format[n]] intro_data"
   echo " "
   echo "	-nd: no delete (do not delete files when finished)"
   echo "Modes:"
   echo "	-f: fade mode (fade from one scene to the next)"
   echo "	-r: roll mode (roll scenes from bottom to top of screen)"
   echo "	-z: zoom mode (scenes zoom from behind camera then fade)"
   echo "	-s: starwars mode (scenes roll from bottom of screen to infinity)"
   echo "Formats:"
   echo "	rla: normal Wavefront format (default)"
   echo "	qtl: yuv format suitable for Abekas A60"
   echo "	abekas n: transfer directly to Abekas A60 starting at n"
   echo "	betacam n t: transfer to Betacam via Abekas A60"
   echo "                    n is starting location on Abekas A60"
   echo "                    t is starting location on Betacam"
   exit
endif
#
#  Read command line arguments.
#
@ n = 1
while ($n <= $#argv)
   if ("$argv[$n]" == "-j") then
      @ n++
      set job_name = $argv[$n]
   else if ("$argv[$n]" == "-f") then
      set mode = "fade"
   else if ("$argv[$n]" == "-r") then
      set mode = "roll"
   else if ("$argv[$n]" == "-s") then
      set mode = "star"
   else if ("$argv[$n]" == "-z") then
      set mode = "zoom"
   else if ("$argv[$n]" == "-p") then
      set mode = "spin"
   else if ("$argv[$n]" == "-nd") then
      @ delete = 0
   else if ("$argv[$n]" == "-fs") then
      @ n++
      set scale = $argv[$n]
   else if ("$argv[$n]" == "-dest") then
      @ n++
      set dest = $argv[$n]
      if ($dest == qtl || $dest == yuv) then
         set image = "qtl"
      else if ($dest == rla || $dest == rle || $dest == sgi) then
         set image = "rla"
      else if ($dest == sgi) then
         set image = "sgi"
      else if ($dest == abekas) then
         set image = "rla"
         @ n++
         @ aframe = $argv[$n]
         if ($aframe < 0 || $aframe > 749) then
            echo "Invalid Abekas start frame number: $aframe"
            exit
         endif
      else if ($dest == betacam) then
         set dest = "abekas"
         @ betacam = 1
         set image = "rla"
         @ n++
         @ aframe = $argv[$n]
         @ astart = $aframe
         if ($aframe < 0 || $aframe > 749) then
            echo "Invalid Abekas start frame number: $aframe"
            exit
         endif
         @ n++
         set timecode = $argv[$n]
      else
         echo "Unknown destination format: $dest"
         exit
      endif
   else
      set intro_data = $argv[$n]
   endif
   @ n++
end
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-testing
#
#  Let's make sure all stuff is here before continuing.
#
if ($intro_data == undefined) then
   echo "You must include your intro datafile."
   exit
else
   if (-e $intro_data) then
   else
      echo "$intro_data does not exist\!"
      exit
   endif
endif
if (-e $WF_SCMP_DIR/$job_name.pv) then
   echo "Woah\!  $WF_SCMP_DIR/$job_name.pv already exists."
   echo "Rename this job or delete the preview file before continuing."
   exit
endif
if (-e $job_name.log) then
   mv $job_name.log $job_name.log.bak
endif
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-modeling
#
#  Read the information in the intro data file and create obj files.
#  The "status" variable will contain the number of groups of objects or scenes.
#
set lines = `wc -l $intro_data`
@ nscenes = -1
@ newobj = 1
@ i = 1
while ($i <= $lines[1])
   set line = `sed -n "${i}p;${i}q" $intro_data`
   set keyword = `echo $line | awk -F= '{print $1}'`
   set data = `echo $line | awk -F= '{print $2}'`
   set junk = `echo $keyword | awk '{print $1}'`
   switch ($junk)
      case DATA_FILES
      case DATAFILES
      case DATA
         set data_location = $data
         if ($data_location == NONE) then
            echo "OK, no data."
         else
            echo "Checking the data location..."
            if (-d $data_location:h) then
               echo "The $data_location:h directory exists."
               echo "Hmmm.  Do those $data_location:t files exist..."
               set test = `ls $data_location*`
               if ($#test == 0) then
                  echo "$data_location:t* files don't exist in the $data_location:h directory\!"
                  exit
               else
                  echo "Yup\!"
                  set ndata = `/bin/ls $data_location* | wc -l`
                  echo "There are $ndata image files."
               endif
            else
               echo "$data_location:h -- invalid directory\!"
               exit
            endif
         endif
         breaksw
      case COLOR_MAP
      case COLORMAP
      case CMAP
      case MAP
         set cmap = $data
         if (-r $cmap) then
            echo "Hmmm.  Is $cmap:t a Winkler style color map..."
            @ size = `ls -l $cmap | awk '{print $5}'`
            if ($size == 4736) then
               echo "Yup.  Convert it..."
#               mkcmap <$cmap >$cmap:t.asc
               ~ken/proj/basset/eatmap -a <$cmap >$cmap:t.asc
               set cmap = $cmap:t.asc
            else if (`wc -l $cmap | awk '{print $1}'` == 256) then
               echo "Nope."
            else
               echo "I can't figure out what kind of color map this is."
               exit
            endif
         else
            echo "$cmap does not exist\!"
            exit
         endif
         breaksw
      case BACKGROUND
      case BACKGND
      case BACK
         set background = $data
         breaksw
      case TEXT_COLOR
      case TEXTCOLOR
      case COLOR
         set rflmap = $data$rflext
         breaksw
      case FONT
      case TEXT
         set font = $data
         breaksw
      default
         if ($nscenes >= 0) then
            if ($#line == 0) then
               @ nscenes++
               echo "Creating o$nscenes.obj:"
#              bit -C -V -s -f linejunk -q 3 dut_bld
               texttoobj linejunk o$nscenes $font >>& $job_name.log
               @ newobj = 1
               rm linejunk
               breaksw
            else
               echo $line >>linejunk
            endif
         else
            if ($#line != 0) then
               echo $line >linejunk
               @ nscenes = 0
            endif
         endif
         breaksw
   endsw
   @ i++
end
#
#  Determine just how many frames we'll have.
#
if ($mode == fade) then
   @ npvobjs = 6 + $nscenes
   @ nframes = $nscenes
   @ total = `echo $nfades $nfades $nscenes | awk '{print $1+$2*$3}'`
else if ($mode == roll) then
   @ npvobjs = 7 + $nscenes
   @ nframes = $nscenes * $fperscn * 2
   @ total = $nfades + $nframes + $nfades
else if ($mode == star) then
   @ npvobjs = 7 + $nscenes
   @ nframes = $nscenes * $fperscn * 2
#  this should really be a constant
   @ nframes = $fperscn * 5
   @ total = $nfades + $nframes + $nfades
else if ($mode == zoom) then
   @ npvobjs = 6 + $nscenes
   @ nframes = `echo $nscenes $fperscn | awk '{print $1*$2}'`
   @ total = $nfades + $nframes + $nframes + $nfades
else if ($mode == spin) then
   @ npvobjs = 6 + $nscenes
   @ nframes = `echo $nscenes $fperscn | awk '{print $1*$2}'`
   @ total = $nfades + $nframes + $nframes + $nfades
endif
#
#  Just how much space is this going to take?
#
if ($?ndata) then
   @ total = $total + $ndata + $nfades
endif
if ($dest == abekas && ($aframe + $total > 750)) then
   echo "Sorry, your animation will not fit onto the Abekas from this starting point."
   @ junk = 750 - $aframe
   echo "Total frames required: $total;   Available frames from this point: $junk"
   exit
else
   echo "Your animation will occupy $total frames on the Abekas A60."
endif
#
#  Make the 3-D text objects.
#
@ nobjs = $nscenes
#
#  Create each of the textual face outline object files.
#  NOTES on bit:
#	-C	Center the output
#	-V	Verbose messages
#	-s	Make each string of text an object
#	-f n	Use text in file "n" for input
#	-q n	Use quality index of "n"
#	-a n	Use arc resolution "n" (10 is default)
#	dut_bld (dutch bold) similar to times bold
#
#@ nobjs = 0
#foreach file ($WF_OBJ_DIR/o?.obj)
#   echo "Created object $file"
#   @ nobjs++
#end
#
#  Extrude and bevel each of the textual objects.
#  Then assign material names.
#
@ n = 1
while ($n <= $nscenes)
   echo "Extruding and beveling o$n.obj"
   model o$n.obj >>& $job_name.log <<!
      extbvlout
      g all
      setr -me mirror
      wo O$n.obj
      exit
!
   @ n++
end
if ($delete == 1) then
   rm *modhist
endif
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-preview
#
#  Create a new .pv file which has enough space for each of these objects.
#  NOTE: If using "roll" or "starwars" mode, a dummy object must be included.
#
echo "Creating new .pv file with $npvobjs objects and $nframes frames..."
new_pv -n $job_name -o $npvobjs -f $nframes >>& $job_name.log
#
#  Generate a .pv script that will add each of these objects to
#  there own line in the .pv file.
#  NOTES:
#	Starting with channel 7, object names are loaded stating with obj1
#	Channel 7 of each new object is changed from scale (default) to display
#	The size of the .pv file in frames needs to be only $nscenes long
#	Each o$nobj is turned on for only one frame
#
echo "Generating a preview script to properly add all objects..."
@ n = 7
@ m = 1
echo "o 6"			>setobj.cmd
echo "seto -f $rflmap"		>>setobj.cmd
echo "seto -d on"		>>setobj.cmd
echo "po -a 6"			>>setobj.cmd
if ($mode == fade) then
#
#  Position the camera.
#
   echo "o 1/3"		>>setobj.cmd
   echo "m = 70"	>>setobj.cmd
   echo "o 1/4"		>>setobj.cmd
   echo "m = 0"		>>setobj.cmd
   echo "o 1/5"		>>setobj.cmd
   echo "m = 0"		>>setobj.cmd
#
#  Turn off the light.
#
   echo "o 4"		>>setobj.cmd
   echo "seto -d off"	>>setobj.cmd
#
#  Add each object and set both "display" and "scale".
#
   while ($n <= $npvobjs)
      echo "o $n"		>>setobj.cmd
      echo "seto -f O$m.obj"	>>setobj.cmd
      echo "seto -d on"		>>setobj.cmd
      echo "po -a $n"		>>setobj.cmd
      echo "o $n/7"		>>setobj.cmd
      echo "f 1 $nframes"	>>setobj.cmd
      echo "setc -l display"	>>setobj.cmd
      echo "m = 0"		>>setobj.cmd
      echo "cr -l"		>>setobj.cmd
      echo "f $m"		>>setobj.cmd
      echo "m = 1"		>>setobj.cmd
      echo "o $n/6"		>>setobj.cmd
      echo "setc -l scale"	>>setobj.cmd
      echo "f 1 $nframes"	>>setobj.cmd
      echo "m = $scale"		>>setobj.cmd
      echo "cr -l"		>>setobj.cmd
      @ n++
      @ m++
   end
else if ($mode == roll) then
   @ offset = 0
   @ total = 0
   while ($n < $npvobjs)
      echo "o $n"		>>setobj.cmd
      echo "seto -f O$m.obj"	>>setobj.cmd
      echo "seto -d on"		>>setobj.cmd
      echo "seto -p $npvobjs"	>>setobj.cmd
      echo "po -a $n"		>>setobj.cmd
      echo "o $n/2"		>>setobj.cmd
      echo "m = $offset"	>>setobj.cmd
      echo "o $n/7"		>>setobj.cmd
      echo "m = $scale"		>>setobj.cmd
      @ n++
      @ m++
#      @ offset -= 10
      @ offset -= 15
#      @ total += 9
      @ total += 16
   end
#
#  Position the camera.
#
   echo "o 1/3"		>>setobj.cmd
   echo "m = 60"	>>setobj.cmd
   echo "o 1/4"		>>setobj.cmd
   echo "m = 0"		>>setobj.cmd
   echo "o 1/5"		>>setobj.cmd
   echo "m = 0"		>>setobj.cmd
#
#  Turn off the light.
#
   echo "o 4"		>>setobj.cmd
   echo "seto -d off"	>>setobj.cmd
#
#  Spin the reflection map.
#
   echo "o 6/6"		>>setobj.cmd
   echo "f 1"		>>setobj.cmd
   echo "m = 0"		>>setobj.cmd
   echo "f $nframes"	>>setobj.cmd
   echo "m = 120"	>>setobj.cmd
   echo "f 1 $nframes"	>>setobj.cmd
   echo "cr -l"		>>setobj.cmd
#
#  Make a dummy object to move all others.
#
   echo "o $npvobjs"	>>setobj.cmd
   echo "seto -t d"	>>setobj.cmd
   echo "seto -n dummy"	>>setobj.cmd
   echo "seto -d on"	>>setobj.cmd
   echo "po -a $npvobjs">>setobj.cmd
   echo "o $npvobjs/2"	>>setobj.cmd
   echo "f 1"		>>setobj.cmd
   echo "m = -14"	>>setobj.cmd
   echo "f $nframes"	>>setobj.cmd
   echo "m = $total"	>>setobj.cmd
   echo "f 1 $nframes"	>>setobj.cmd
   echo "cr -l"		>>setobj.cmd
else if ($mode == zoom) then
#
#  Position the camera.
#
   echo "o 1/3"		>>setobj.cmd
   echo "m = 50"	>>setobj.cmd
   echo "o 1/4,5"	>>setobj.cmd
   echo "m = 0"		>>setobj.cmd
#
#  Turn off the light.
#
   echo "o 4"		>>setobj.cmd
   echo "seto -d off"	>>setobj.cmd
##
##  Position the light
##
#   echo "o 4/1"		>>setobj.cmd
#   echo "m = -35"	>>setobj.cmd
   @ frame1 = 1
   @ frame2 = $frame1 + $fperscn - 1
   @ frame2p = $frame2 - 1
   while ($n <= $npvobjs)
      echo "o $n"		>>setobj.cmd
      echo "seto -f O$m.obj"	>>setobj.cmd
      echo "seto -d on"		>>setobj.cmd
      echo "po -a $n"		>>setobj.cmd
      echo "o $n/6"		>>setobj.cmd
      echo "setc -l scale"	>>setobj.cmd
      echo "f $frame1"		>>setobj.cmd
      echo "o $n/3"		>>setobj.cmd
      echo "m = 50.05"		>>setobj.cmd
      echo "o $n/6"		>>setobj.cmd
      echo "m = $scale"		>>setobj.cmd
      echo "f $frame2p $frame2"	>>setobj.cmd
      echo "o $n/3"		>>setobj.cmd
      echo "m = 0"		>>setobj.cmd
      echo "o $n/6"		>>setobj.cmd
      echo "m = $scale"		>>setobj.cmd
      echo "f $frame1 $frame2p $frame2"	>>setobj.cmd
      echo "o $n/3,6"		>>setobj.cmd
      echo "cr -s"		>>setobj.cmd
      echo "o $n/7"		>>setobj.cmd
      echo "setc -l display"	>>setobj.cmd
      echo "f 1 $nframes"	>>setobj.cmd
      echo "m = 0"		>>setobj.cmd
      echo "f $frame1 $frame2"	>>setobj.cmd
      echo "m = 1"		>>setobj.cmd
      @ n++
      @ m++
      @ frame1 += $fperscn
      @ frame2 += $fperscn
      @ frame2p += $fperscn
   end
#
#  Spin the reflection map.
#
   echo "o 6/6"		>>setobj.cmd
   echo "f 1"		>>setobj.cmd
   echo "m = 0"		>>setobj.cmd
   echo "f $nframes"	>>setobj.cmd
   echo "m = 360"	>>setobj.cmd
   echo "f 1 $nframes"	>>setobj.cmd
   echo "cr -l"		>>setobj.cmd
else if ($mode == star) then
   @ offset = 0
   while ($n < $npvobjs)
      echo "o $n"		>>setobj.cmd
      echo "seto -f O$m.obj"	>>setobj.cmd
      echo "seto -d on"		>>setobj.cmd
      echo "seto -p $npvobjs"	>>setobj.cmd
      echo "po -a $n"		>>setobj.cmd
      echo "o $n/2"		>>setobj.cmd
      echo "m = $offset"	>>setobj.cmd
      echo "o $n/7"		>>setobj.cmd
      echo "m = $scale"		>>setobj.cmd
      @ n++
      @ m++
      @ offset -= 10
   end
#
#  Position the camera.
#
   echo "o 1/4,5"	>>setobj.cmd
   echo "m = 0"		>>setobj.cmd
   echo "o 1/3"		>>setobj.cmd
   echo "m = 30"	>>setobj.cmd
   echo "cam -ap 2.0"	>>setobj.cmd
#
#  Turn off the light.
#
   echo "o 4"		>>setobj.cmd
   echo "seto -d off"	>>setobj.cmd
#
#  Spin the reflection map.
#
   echo "o 6/6"		>>setobj.cmd
   echo "f 1"		>>setobj.cmd
   echo "m = 0"		>>setobj.cmd
   echo "f $nframes"	>>setobj.cmd
   echo "m = 360"	>>setobj.cmd
   echo "f 1 $nframes"	>>setobj.cmd
   echo "cr -l"		>>setobj.cmd
#
#  Make a dummy object to move all others.
#
   echo "o $npvobjs"	>>setobj.cmd
   echo "seto -t d"	>>setobj.cmd
   echo "seto -n dummy"	>>setobj.cmd
   echo "seto -d on"	>>setobj.cmd
   echo "po -a $npvobjs">>setobj.cmd
   echo "o $npvobjs/2"	>>setobj.cmd
   echo "f 1"		>>setobj.cmd
#   echo "m = -31.8"	>>setobj.cmd
   echo "m = -17.0"	>>setobj.cmd
#   @ junk = $nframes - 1
#   echo "f $junk"	>>setobj.cmd
#   echo "m = 60"	>>setobj.cmd
   echo "f $nframes"	>>setobj.cmd
   echo "m = 60"	>>setobj.cmd
   echo "f 1 $nframes"	>>setobj.cmd
   echo "cr -l"		>>setobj.cmd
   echo "o $npvobjs/4"	>>setobj.cmd
   echo "m = -70"	>>setobj.cmd
   echo "seto -tp rts"	>>setobj.cmd
   echo "seto -rp yxz"	>>setobj.cmd
else if ($mode == spin) then
#
#  Position the camera.
#
   echo "o 1/3"		>>setobj.cmd
   echo "m = 50"	>>setobj.cmd
   echo "o 1/4,5"	>>setobj.cmd
   echo "m = 0"		>>setobj.cmd
#
#  Turn off the light -- the party's over.
#
   echo "o 4"		>>setobj.cmd
   echo "seto -d off"	>>setobj.cmd
   @ frame1 = 1
   @ frame1p = $frame1 + 1
   @ framem = `echo $fperscn | awk '{print $1/2}'`
   @ framemp = $framem + 1
   @ frame2 = $frame1 + $fperscn - 1
   @ frame2p = $frame2 - 1
   while ($n <= $npvobjs)
      echo "o $n"		>>setobj.cmd
      echo "seto -f O$m.obj"	>>setobj.cmd
      echo "seto -d on"		>>setobj.cmd
      echo "po -a $n"		>>setobj.cmd
      echo "o $n/6"		>>setobj.cmd
      echo "setc -l scale"	>>setobj.cmd
      echo "f $frame1 $frame2"	>>setobj.cmd
      echo "m = $scale"		>>setobj.cmd
      echo "o $n/4"		>>setobj.cmd
      echo "f $frame1 $frame1p"	>>setobj.cmd
      echo "m = -180"		>>setobj.cmd
      echo "f $framem $framemp"	>>setobj.cmd
      echo "m = 0"		>>setobj.cmd
      echo "f $frame2p $frame2"	>>setobj.cmd
      echo "m = 180"		>>setobj.cmd
      echo "f $frame1 $frame1p $framem $framemp $frame2p $frame2">>setobj.cmd
      echo "o $n"		>>setobj.cmd
      echo "cr -s"		>>setobj.cmd
      echo "o $n/7"		>>setobj.cmd
      echo "setc -l display"	>>setobj.cmd
      echo "f 1 $nframes"	>>setobj.cmd
      echo "m = 0"		>>setobj.cmd
      echo "f $frame1 $frame2"	>>setobj.cmd
      echo "m = 1"		>>setobj.cmd
      @ n++
      @ m++
      @ frame1 += $fperscn
      @ frame1p += $fperscn
      @ framem += $fperscn
      @ framemp += $fperscn
      @ frame2 += $fperscn
      @ frame2p += $fperscn
   end
#
#  Spin the reflection map.
#
   echo "o 6/6"		>>setobj.cmd
   echo "f 1"		>>setobj.cmd
   echo "m = 0"		>>setobj.cmd
   echo "f $nframes"	>>setobj.cmd
   echo "m = 360"	>>setobj.cmd
   echo "f 1 $nframes"	>>setobj.cmd
   echo "cr -l"		>>setobj.cmd
endif
echo "exit save $job_name" >>setobj.cmd
#
#  Do the preview thing...
#
echo "Adding all objects to the new preview file..."
pv $job_name <setobj.cmd >>& $job_name.log
if ($delete == 1) then
   rm setobj.cmd
   rm *pvhist
endif
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-Background
#
#  Make a fancy background for all scenes using the proper format.
#
if ($format == ntsc_4d) then
   @ xsize = 646
   @ ysize = 485
else if ($format == qtl_ntsc) then
   @ xsize = 720
   @ ysize = 486
endif
echo "Making a $format $background background..."
if ($background == undefined) then
   rlebg -s $xsize $ysize -v 0.1 0.9 240 240 240 | rletorla -o backgnd.rla
else if ($background == red) then
   rlebg -s $xsize $ysize -v 0.1 0.9 255 100 100 | rletorla -o backgnd.rla
else if ($background == green) then
   rlebg -s $xsize $ysize -v 0.1 0.9 100 240 150 | rletorla -o backgnd.rla
else if ($background == blue) then
#   rlebg -s $xsize $ysize -v 0.1 0.9 100 080 255 | rletorla -o backgnd.rla
   rlebg -s $xsize $ysize -v 0.5 0.1 030 030 255 | rletorla -o backgnd.rla
else if ($background == cyan) then
   rlebg -s $xsize $ysize -v 0.1 0.9 100 255 255 | rletorla -o backgnd.rla
else if ($background == yellow) then
   rlebg -s $xsize $ysize -v 0.1 0.9 255 240 100 | rletorla -o backgnd.rla
else if ($background == magenta) then
   rlebg -s $xsize $ysize -v 0.1 0.9 255 080 255 | rletorla -o backgnd.rla
else if ($background == pattern) then
   ppmpat -poles $xsize $ysize | ppmtorle | rletorla -o backgnd.rla
else if ($background == none) then
else
   if (-e $background) then
      tontsc -imf rla -o backgnd.rla $background
   else
      echo "Can't find $background"
      exit
   endif
endif
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-Imaging
#
#  All frames are ready to be rendered.
#
echo "Render all $nframes of the scenes using $format format..."
if ($mode == spin) then
   @ start = 0030
else
   @ start = 0001
endif
image -fs $start -fe $nframes -os 3 -i text -fmt $format $job_name
if ($delete == 1) then
   @ n = 1
   while ($n <= $nobjs)
      rm $WF_OBJ_DIR/*o$n.obj
      rm $WF_OBJ_DIR/*O$n.obj
      @ n++
   end
   rm $WF_SCMP_DIR/*$job_name.pv
endif
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-Initial Fade
#
#  Before doing anything else, create the fade "into".
#
if ($background != none) then
   composite -fs $start -base text -back backgnd.rla -fmt $format
endif
if ($dest == abekas) then
   smart_vfr -fs 1 -fe $nfades -base fadein -a $aframe -ext rla &
   @ aframe += $nfades
endif
echo "Creating the intro fade..."
if ($background == none) then
   fade -l text.$start.rla -n $nfades -base fadein -fmt $format -imf $image
else
   fade -l comp.$start.rla -n $nfades -base fadein -fmt $format -imf $image
   rm comp.0001.rla
endif
if ($dest == qtl) then
   echo "#\!/bin/csh -f" >$job_name.sh
   echo "@ i = 0" >>$job_name.sh
   echo "foreach file (fadein.*.qtl)" >>$job_name.sh
   echo '   rcp $file abekas:$i' >>$job_name.sh
   echo '   @ i++' >>$job_name.sh
   echo "end" >>$job_name.sh
   chmod 755 $job_name.sh
else if ($dest == rle) then
   echo "Converting intro fade rla files to rle..."
   foreach file (fadein.*.rla)
      echo "Converting $file to $file:r.rle..."
      rlatorle -o $file:r.rle $file
      rm $file
   end
#else if ($dest == sgi) then
#   echo "Converting intro fade rla files to sgi..."
#   foreach file (fadein.*.rla)
#      echo "Converting $file to $file:r.sgi..."
#      fromrla $file $file:r.sgi
#      rm $file
#   end
endif
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-Fading for fade or zoom modes
#
#  OK, we have all of the scenes.  Lets generate fades between them (maybe).
#
if ($mode == fade) then
   echo "Goody Goody.  We're doing fade mode.  Everything rendered properly"
   echo "Now I will create $nfades fades between each of the $nscenes scenes..."
   @ n = 1
   @ nplus1 = 2
   while ($n < $nscenes)
      set f1 = `echo $n      | awk '{printf "text.%04d.rla", $1}'`
      set f2 = `echo $nplus1 | awk '{printf "text.%04d.rla", $1}'`
      fade -n $nfades -f $f1 -l $f2 -base fade$n -fmt $format
      if ($delete == 1) then
         rm text.000$n.rla
      endif
      @ n++
      @ nplus1++
   end
   if ($delete == 1) then
      rm text.000$n.rla
   endif
#
#  Now it would be nice to rename these fader images more orderly.
#
   @ nframes = 1
   @ n = 1
   while ($n < $nscenes)
      @ number = 1
      while ($number <= $nfades)
         set f1 = `echo $n $number | awk '{printf "fade%d.%04d.rla", $1, $2}'`
         set f2 = `echo $nframes| awk '{printf "text.%04d.rla", $1}'`
         echo "Renaming $f1 to $f2"
         mv $f1 $f2
         @ number++
         @ nframes++
      end
      @ n++
   end
   @ nframes--
else if ($mode == zoom) then
   echo "OK, now we've got to do some fading between the scenes..."
   @ n = 1
   @ b = $fperscn
   @ e = $b + 1
   while ($n < $nscenes)
      set f1 = `echo $b | awk '{printf "text.%04d.rla", $1}'`
      set f2 = `echo $e | awk '{printf "text.%04d.rla", $1}'`
      fade -n $nfades -f $f1 -l $f2 -base fade$n -fmt $format
      @ n++
      @ b += $fperscn
      @ e += $fperscn
   end
#
#  Renumber the rendered scenes so that we can stick in the fades.
#
   @ n = $nscenes
   while ($n > 1)
      @ ostart = ($n - 1) * $fperscn + 1
      @ oend = $ostart + $fperscn - 1
      @ nstart = ($n - 1) * $nfades + (($n - 1) * $fperscn) + 1
      @ nend = $nstart + $fperscn - 1
      while ($oend >= $ostart)
         set f1 = `echo $oend | awk '{printf "text.%04d.rla", $1}'`
         set f2 = `echo $nend | awk '{printf "text.%04d.rla", $1}'`
         echo "rename $f1 to $f2"
         mv $f1 $f2
         @ oend--
         @ nend--
      end
      @ n--
   end
#
#  Insert the fades amongst the rendered images.
#
   @ n = 1
   while ($n < $nscenes)
      @ i = 1
      while ($i <= $nfades)
         set f1 = `echo $n $i | awk '{printf "fade%d.%04d.rla", $1, $2}'`
         @ nstart = $n * $fperscn + ($n - 1) * $nfades + $i
         set f2 = `echo $nstart | awk '{printf "text.%04d.rla", $1}'`
         echo "rename $f1 to $f2"
         mv $f1 $f2
         @ i++
      end
      @ n++
   end
else if ($mode == spin) then
#
#  Create some moving fades.
#
   @ n = 1
   @ j = 1
   @ first = 30
   @ last  = 61
   while ($n < $nscenes)
      movingfade -f text $first -l text $last
      @ i = 1
      while ($i <= 30)
         set f1 = `echo $i | awk '{printf "fade.%04d.rla", $1}'`
         set f2 = `echo $j | awk '{printf "text.%04d.rla", $1}'`
         mf $f1 $f2
         @ j++
      end
      @ first += `echo $nfades | awk '{print $1*2}'`
      @ last  += `echo $nfades | awk '{print $1*2}'`
      @ n++
   end
exit
endif
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-Composite main body of movie
#
#  Composite intro text over background.
#
echo "Compositing all images over the static background..."
if ($mode == zoom) then
   @ nframes = $nscenes * $fperscn + ($nscenes - 1) * $nfades
endif
#
#  First check to see if we're going directly to the Abekas.
#
if ($dest == abekas) then
   wait
   if ($background == none) then
      smart_vfr -fs 1 -fe $nframes -base text -a $aframe -ext rla &
   else
      smart_vfr -fs 1 -fe $nframes -base comp -a $aframe -ext rla &
   endif
   @ aframe += $nframes
endif
if ($background != none) then
  composite -fs 1 -fe $nframes -base text -back backgnd -imf $image -fmt $format
endif
#
#  Composite the last image into an "rla" file so that we can
#  easily fade to the raw data.
#
if ($dest == qtl || $dest == yuv) then
   composite -fs $nframes -base $text -back backgnd -fmt $format
endif
if ($background == none) then
   set last_file = `echo $nframes | awk '{printf "text.%04d.rla", $1}'`
else
   set last_file = `echo $nframes | awk '{printf "comp.%04d.rla", $1}'`
endif
cp $last_file last.rla
if ($delete == 1 && $background != none) then
   rm backgnd.rla text.*.rla
endif
#
#  Now we have a bunch of files named comp.xxxx.qtl.
#  What do we do with these files?
#
if ($dest == rle) then
   echo "Converting all rla files to rle files..."
   foreach file (comp.*.rla)
      echo "Converting $file to $file:r.rle..."
      rlatorle -o $file:r.rle $file
      rm $file
   end
#else if ($dest == sgi) then
#   echo "Converting all rla files to sgi files..."
#   foreach file (comp.*.rla)
#      echo "Converting $file to $file:r.sgi..."
#      fromrla $file $file:r.sgi
#      rm $file
#   end
else if ($dest == qtl) then
   echo "foreach file (comp.*.qtl)" >>$job_name.sh
   echo '   rcp $file abekas:$i' >>$job_name.sh
   echo '   @ i++' >>$job_name.sh
   echo "end" >>$job_name.sh
endif
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-Fade to raw data
#
#  If we're going to the Abekas, get ready to transfer the files that
#  Will be created in the next step.
#
if ($dest == abekas) then
   wait
   smart_vfr -fs 1 -fe $nfades -base fade -a $aframe -ext rla &
   @ aframe += $nfades
   @ astop = $aframe - 1
endif
#
#  Get the name of the first of the data images.
#
if ($data_location == undefined || $data_location == NONE) then
   echo "There is no raw data -- we will fade to black..."
else
   ls $data_location* > datajunk
   set first_data = `sed -n "1p;1q" datajunk`
   @ nrawdata = `wc -l datajunk | awk '{print $1}'`
   rm datajunk
   echo "$first_data:t is the first of $nrawdata raw data files."
   echo "Fade from last.rla to $first_data..."
endif
#
#  We're done with the intro part.  Let's create a fade between the last
#  scene and the first piece of data.
#
if ($data_location == undefined || $data_location == NONE) then
   fade -f last.rla -n $nfades -base fade -imf $image -fmt $format
   rm last.rla
   if ($dest == rle) then
      echo "Converting final fade rla files to rle..."
      foreach file (fade.*.rla)
         echo "Converting $file to $file:r.rle..."
         rlatorle -o $file:r.rle $file
         rm $file
      end
#   else if ($dest == sgi) then
#      echo "Converting final fade rla files to sgi..."
#      foreach file (fade.*.rla)
#         echo "Converting $file to $file:r.sgi..."
#         fromrla $file $file:r.sgi
#         rm $file
#      end
   endif
else
   if ($cmap == undefined) then
      rawtorla $first_data -o junk.rla -fmt $format
   else
      rawtorla $first_data -o junk.rla -cmap $cmap -fmt $format
   endif
   fade -f last.rla -l junk.rla -n $nfades -base fade -imf $image -fmt $format
   rm last.rla junk.rla
endif
if ($dest == qtl) then
   echo "foreach file (fade.*.qtl)" >>$job_name.sh
   echo '   rcp $file abekas:$i' >>$job_name.sh
   echo '   @ i++' >>$job_name.sh
   echo "end" >>$job_name.sh
endif
#
#  If there is no raw data, we can stop here.
#
if ($data_location == undefined || $data_location == NONE) then
   wait
   if ($betacam == 1) then
      echo "Transfering to betacam..."
      vfr_record -in $timecode -st $astart -end $astop
   else
      echo "Done."
   endif
   exit
endif
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-Transfer of raw data
#
#  Ok.  The transistion is generated and laid down.  Now let's get ready
#  to do the raw conversion and transfer.
#
if ($dest == abekas) then
   wait
#   smart_vfr -fs 1 -fe $nrawdata -base $data_location:t -ext yuv -a $aframe &
   smart_vfr -fs 1 -fe $nrawdata -base $data_location:t -a $aframe -ext rla &
   @ aframe += $nrawdata
endif
#
#  Now convert the raw images to qtl_ntsc format rla (or qtl) files.
#
if ($dest == rla) then
   @ n = 1
   foreach file ($data_location*)
      if ($n < 10) then
         set f2 = $data_location:t.000$n.rla
      else if ($n < 100) then
         set f2 = $data_location:t.00$n.rla
      else if ($n < 1000) then
         set f2 = $data_location:t.0$n.rla
      else
         set f2 = $data_location:t.$n.rla
      endif
      echo "Converting $file to $f2..."
      if ($cmap == undefined) then
         rawtorla $file -o $f2 -fmt $format
      else
         rawtorla $file -o $f2 -cmap $cmap -fmt $format
      endif
      @ n++
   end
else if ($dest == abekas) then
   @ n = 1
   foreach file ($data_location*)
      set f2 = `echo $data_location:t $n | awk '{printf "%s.%04d.rla", $1, $2}'`
      echo "Converting $file to $f2..."
      if ($cmap == undefined) then
#         rawtoyuv $file $f2
         rawtorla $file -o $f2 -fmt $format
      else
#         rawtoyuv $file $f2 $cmap
         rawtorla $file -o $f2 -cmap $cmap -fmt $format
      endif
      @ n++
   end
else if ($dest == qtl) then
   echo "foreach file ($data_location:t.*.yuv)" >>$job_name.sh
   echo '   rcp $file abekas:$i' >>$job_name.sh
   echo '   @ i++' >>$job_name.sh
   echo "end" >>$job_name.sh
endif
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=Fade from raw data to black
#
#  Now we will fade from the last image to black.
#
if ($dest == abekas) then
   wait
   smart_vfr -fs 1 -fe $nfades -base fadeout -a $aframe -ext rla &
   @ aframe += $nfades
   @ astop = $aframe - 1
endif
echo "Creating a fade from $file to black..."
if ($cmap == undefined) then
   rawtorla $file -o junk.rla -fmt $format
else
   rawtorla $file -o junk.rla -cmap $cmap -fmt $format
endif
fade -f junk.rla -n $nfades -base fadeout -imf $image -fmt $format
rm junk.rla
if ($dest == qtl) then
   echo "foreach file (fadeout.*.qtl)" >>$job_name.sh
   echo '   rcp $file abekas:$i' >>$job_name.sh
   echo '   @ i++' >>$job_name.sh
   echo "end" >>$job_name.sh
endif
wait
if ($betacam == 1) then
   echo "Transfering to betacam..."
   vfr_record -in $timecode -st $astart -end $astop
endif
echo "Done."
