#!/bin/csh -f
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
#  This is a csh script that can be used to transfer chunks of images
#  while those very images are being rendered elsewhere.
#
#
#  Author:     Wesley C. Barris
#              AHPCRC
#              Minnesota Supercomputer Center, Inc.
#  Date:       Oct. 11, 1991
#
#  Copyright @ 1991, 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.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
if (-e /net/i1/usr/tmp/abekas) then
   set person = `cat /net/i1/usr/tmp/abekas`
   if ($person != `whoami`) then
      echo "$person is already using the Abekas A60"
      echo "If this is not correct, delete the file i1:/usr/tmp/abekas"
      exit
   endif
endif
#
#  Here are some default thingies...
#
@ nframe	= 1		# start sending this file
@ last_frame	= 30		# stop sending files after this one
set base_name	= 'fade'	# use this base name
set dest_node	= 'abekas'	# this is where they are going
set extension	= 'undefined'	# extension of files to look for
@ needinit	= 1		# do we need to initialize Video Framer?
@ aframe	= 0		# first file goes here on the abekas
@ delete	= 1		# delete the files after being sent
@ sentfiles	= 0		# init the sentfiles accumulator
@ begmin	= `date +%M`	# init begmin to reflect the current minute
@ begsec	= `date +%S`	# init begmin to reflect the current second
@ totmin	= 0		# init totmin to zero
@ totsec	= 0		# init totsec to zero
@ secperfile	= 1		# init to something sufficiently large?
@ bhold		= 0		# no hold at the beginning
@ ehold		= 0		# no hold at the end
#
#  Read command line arguments.
#
@ n = 1
while ($n <= $#argv)
   if ("$argv[$n]" == "-fs") then
      @ n++
      @ nframe = $argv[$n]
   else if ("$argv[$n]" == "-fe") then
      @ n++
      @ last_frame = $argv[$n]
   else if ("$argv[$n]" == "-base") then
      @ n++
      set base_name = $argv[$n]
   else if ("$argv[$n]" == "-ext") then
      @ n++
      set extension = $argv[$n]
   else if ("$argv[$n]" == "-a") then
      @ n++
      @ aframe = $argv[$n]
   else if ("$argv[$n]" == "-bh") then
      @ n++
      @ bhold = $argv[$n]
   else if ("$argv[$n]" == "-eh") then
      @ n++
      @ ehold = $argv[$n]
   else if ("$argv[$n]" == "-nd") then
      @ delete = 0
   else
      echo "Usage: smart_vfr [-fs start] [-fe end] [-base basename]"
      echo "                 [-dir remote_dir] [-a abekas_start_frame]"
      echo "                 [-nd]"
      echo "If options are omitted, the following defaults will be used."
      echo "smart_vfr -fs 1 -fe 30 -base fade -a 0"
      exit
   endif
   @ n++
end
#if ($nframe == $last_frame) then
#   @ single = 1
#else
#   @ single = 0
#endif
#
#  What kind of files do we see here?
#
if ($extension == undefined) then
   set file_name = `echo $base_name $nframe | awk '{printf "%s.%04d",$1,$2}'`
   if (-e $file_name.rle) then
      set extension = 'rle'
   else if (-e $file_name.sgi) then
      set extension = 'sgi'
   else if (-e $file_name.rgb) then
      set extension = 'rgb'
   else if (-e $file_name.yuv) then
      set extension = 'yuv'
   else if (-e $file_name.qtl) then
      set extension = 'qtl'
   else if (-e $file_name.rla) then
      set extension = 'rla'
   else
      echo "I can't find any files to send???"
      exit
   endif
endif
#
#  Disable Abekas control panel.
#
echo "Disabling Abekas control panel..."
rsh abekas -n disable
whoami >/net/i1/usr/tmp/abekas
chmod 666 /net/i1/usr/tmp/abekas
#
#  Transfer these frames when they become available.
#
while ($nframe <= $last_frame)
   @ test = $nframe + 1
#   if ($single == 1) then
#      @ test = $last_frame
#      @ single = 0
#   else if ($test > $last_frame) then
   if ($test > $last_frame) then
      @ test = $last_frame
      if ($secperfile != 0) then
         echo "waiting $secperfile seconds for last file..."
         sleep $secperfile
      endif
   endif
   set file_name = `echo $base_name $test $extension | awk '{printf "%s.%04d.%s",$1,$2,$3}'`
   if (-e $file_name) then
#
#  Produce the text string for this file number.
#
      set this_file = `echo $base_name $nframe $extension | awk '{printf "%s.%04d.%s",$1,$2,$3}'`
      @ sentfiles++
      echo "Sending $this_file to frame $aframe on the $dest_node..."
      if ($extension == rle) then
         if (`hostname` == i1) then
            if ($needinit) then
               vfr_rletoD1 $this_file
               @ needinit = 0
            else
               vfr_rletoD1 -z $this_file
            endif
            rsh abekas -n record input $aframe
         else
            rletoabA60 $this_file >junk.yuv
            rcp junk.yuv $dest_node\:$aframe
         endif
      else if ($extension == sgi || $extension == rgb) then
         if (`hostname` == i1) then
            if ($needinit) then
               /usr/video/vfr/bin/display -b vfr_d1_525 -n $this_file
               @ needinit = 0
            else
               /usr/video/vfr/bin/display -b vfr_d1_525 -n -z $this_file
            endif
            rsh abekas -n record input $aframe
         else
            iristorle $this_file | rletoabA60 >junk.yuv
#           toyuv $this_file junk.yuv	#this causes a one pixel shift
            rcp junk.yuv $dest_node\:$aframe
         endif
      else if ($extension == rla) then
         if (`hostname` == i1) then
            if ($needinit) then
               echo "Initializing Video Framer buffer with color bars..."
               /usr/video/vfr/bin/display -b vfr_d1_525 -c
               @ needinit = 0
            endif
#           /usr/wave/CONTRIB1.03/bin/fb_dspl $this_file
            imf_dspl -fb VideoFramer $this_file >& /dev/null
            rsh abekas -n record input $aframe
         else
            rlatorle $this_file | rletoabA60 >junk.yuv
            rcp junk.yuv $dest_node\:$aframe
         endif
      else if ($extension == yuv) then
         rcp $this_file $dest_node\:$aframe
      else if ($extension == qtl) then
         rcp $this_file $dest_node\:$aframe
      endif
      if ($delete == 1) then
         echo "Deleting $this_file"
         rm $this_file
      endif
      @ nframe++
      @ aframe++
   else
#
#  Let's see how many files were sent in that last chunck.
#
      if ($sentfiles > 0) then
         @ curmin = `date +%M`
         @ cursec = `date +%S`
         if ($curmin >= $begmin) then
            @ totmin = $curmin - $begmin
         else
            @ totmin = 60 + $curmin - $begmin
         endif
         if ($cursec >= $begsec) then
            @ totsec = $cursec - $begsec
         else
            @ totsec = 60 + $cursec - $begsec
            @ totmin--
         endif
         date
         echo "We created/sent $sentfiles files in $totmin minutes $totsec seconds."
         @ temp = `echo $totmin | awk '{print $1*60}'`
         @ totsec += $temp
         echo "total seconds: $totsec"
         set secperfile = `echo $totsec $sentfiles | nawk '{print int(.5+$1/$2)}'`
         echo "$secperfile seconds per file (converted and sent)."
         @ sentfiles = 0
         @ totmin = 0
         @ totsec = 0
         @ begmin = `date +%M`
         @ begsec = `date +%S`
      endif
      echo "Waiting for $file_name..."
      sleep $secperfile
   endif
end
#
#  Clean up.  (this gets rid of that little period after the frame number)
#
if ($aframe == 750) then
   rsh abekas -n goto 749
endif
#
#  Enable Abekas control panel.
#
echo "Enabling Abekas control panel..."
rsh abekas -n enable
rm -f /net/i1/usr/tmp/abekas
