#!/bin/csh -f
#
#  JEEVES
#
#  This script will drive movie making programs for
#  3-D Hydro code simulations.
#
#  USAGE: jeeves

  echo "START JEEVES"

  # ---------------
  # SET ENVIRONMENT
  # ---------------

    set BINDIR="/home/user/bin"
    set READDIR="/home/user/junk"
    set DUMPDIR="/home/user/junk"
    set INFODIR="/home/user/info"
    set REMSITE="ozma.sdsc.edu"
    set REMSCR="/u/home/user/imaging/bin/hlmrun.sh"
    set SHELL=/bin/csh
    set NAME=disk1000
    set path = ( /usr/etc $BINDIR /usr/local/bin \
                 /usr/openwin/bin /usr/ucb /bin /usr/bin )

  # ------------------
  # START MAIN PROGRAM
  # ------------------


    #Setup lock for ".flag" file 
    set lock = "$BINDIR/.lock"

  
    # Check for lock before modifying ".flag" file
    while (-e $lock)
      echo " " > /dev/null
    end

    # Lock out ".flag" file
    touch $lock

    # Update the flag number 
    # This number tells the script whether it is already running
    # and how many more files it has to process
    set nu = `cat $BINDIR/.flag`
    set nu=`expr $nu + 1`
    echo $nu >! $BINDIR/.flag
    echo ".flag =" $nu 

    # Remove lock on ".flag" file
    rm -f $lock


  
    # Exit if another "holmes" process is already running
    # (if .flag > 1 after being updated)
    if($nu > 1) then
      exit
    endif

    # Start the main loop
    # 
    # Remains in the loop as long as there are data files
    # to process
  
    while ($nu > 0)


  
    # Get the current filename
      set FILENM=`$BINDIR/getbase disk`
      echo $FILENM


  
    # FTP the density file to the SGI
      echo "start FTP of $FILENM"
      $BINDIR/ftpfile.sh $READDIR/$FILENM
      echo "FTP done"


  
    # Start remote shell script holmes on ozma
      echo "start remote holmes"
      /usr/ucb/remsh -l user $REMSITE $REMSCR 

    # Remove density file
      echo "remove binary"
      /bin/rm -f $READDIR/$FILENM 


  
    # Check for lock before modifying ".flag" file
      while (-e $lock)
        echo " " > /dev/null
      end

    # Lock out ".flag" file
      touch $lock

    # Decrement the flag number 
      set nu = `cat $BINDIR/.flag`
      set nu=`expr $nu - 1`
      echo $nu >! $BINDIR/.flag

    # Remove lock on ".flag" file
      /bin/rm -f $BINDIR/.lock 

  
  end

  # ----------------
  # END MAIN PROGRAM
  # ----------------

  echo "END JEEVES"