#!/usr/local/bin/perl -w
#
# copymon
#
# Purpose:
#    Script to copy new images into the current directory.
#
# Usage:
#    copymon <instrument_name>
# where:
#    <instrument_name> = "lris" or "hires"
#
# Modification history:
#    GDW  1998-Jul-28  original version, adapted from "archmon"
#    GDW  1998-Nov-11  added check on return value of "waitfor" to
#                      enable keyboard interrupts      
#-----------------------------------------------------------------------

{

 # declare variables...
 my( $maxhours) = 18;		# maximum number of hours to run
 my( $quitting_time) = 10;	# stop after 10am is started before 10am
 my( @buf);
 my( $outdir);
 my( $outfile);
 my( $frameno);
 my( $dform);
 my( $todisk);
 my( $rootname);
 my( $filename);
 my( $nightpath);
 my( $space);
 my( $i);
 my( $size);
 my( $machine);
 my( $disk);
 my( $hh, $hh_start);
 my( $dd, $dd_start);
 
 # verify presence of argument...
 if ( $#ARGV < 0 ){ die "Usage:\n\tcopymon <instrument_name>\nwhere:\n\t<instrument_name> = \"hires\" or \"lris\"\007\n" }
 
 # verify validity of argument...
 my( $instrument) = $ARGV[0];
 unless ( $instrument eq "hires" || $instrument eq "lris" ){
   die ("copymon: invalid instrument name \"", $instrument, "\"\007\n")
 }
 
 # get the current working directory...
 my( $dir) = `pwd`; 
 chop( $dir);
 
 print <<END
 
  copymon is now running!  Incoming $instrument files will be copied
  into the current directory ($dir).  
  This program will terminate automatically in $maxhours hours or at $quitting_time a.m.,
  whichever comes first.

END
 ;
 
 # get the nightly directory...
 $nightpath = `nightpath`;
 chop( $nightpath);		# remove trailing backslash
 
 # check the starting time...
 ($hh_start, $dd_start) =(localtime)[2,3];
 
 # start infinite loop...
 {
   
   # wait for an image to appear on the disk...
   system( "waitfor -s $instrument wdisk=t") == 0
     or die "copymon interrupted";
   system( "waitfor -s $instrument wdisk=f") == 0
     or die "copymon interrupted";
   
   # get the file name of the image just written to disk...
   $_ = `show -s $instrument outdir outfile frameno dform todisk`;
   @buf = split;
   $outdir  = $buf[2];
   $outfile = $buf[8];
   $frameno = $buf[14];
   $dform   = $buf[17];
   $todisk  = $buf[24];
   
   # skip scratch files...
   $frameno -= 1;
   redo if $todisk eq "false";
   
   # skip malign files...
   redo if $outdir eq $nightpath;
   
   # build the output file name...
   $rootname = sprintf( "%s%04d.%s", $outfile, $frameno, $dform);
   $filename = $outdir . "/" . $rootname;
   
   # verify the existence and non-emptiness of the file...
   if( ! -e $filename || -z $filename){
     print STDERR 
       "copymon: $filename is empty or non-existent -- skipping.\007\n";
     redo
   }
   
   # check the size of the input file...
   $size = 0 + ( stat( $filename))[7];
   
   # make sure there is sufficient space in the local directory...
   $_ = `df .`;
   @buf = split;
   if( @buf < 11 ){
     print STDERR "copymon: bad return from df.\007\n";
     redo
   }
   
   # if the disk is remote, we may not have received the right
   # information from "df".  So if a colon is present in the
   # diskname go to that machine and check "df"...
   $disk = $buf[7];
   $i = index( $disk, ":");
   if( $i ge 1){
     $machine = substr( $disk, 0, $i);
     $disk = substr( $disk, $i+1);
     
     # re-do the check...
     $_ = `rsh $machine df $disk`;
     @buf = split;
     if( @buf < 11 ){
       print STDERR "copymon: bad return from df.\007\n";
       redo
     }
   }
   
   # inform user about disk space...
   $space = 0 + $buf[10];
   
   # compare filesize (in KB) with available space; 
   # give error if insufficient...
   if( $space < $size/1000. ){
     print STDERR 
       "copymon: Insufficient space in $dir\ncopymon: $filename not copied\007\n";
     redo
   }
   
   # copy the file...
   print STDERR "copymon: copying $rootname...";
   system( "cp $filename $dir") == 0
     or print STDERR "\ncopymon: copy of file $filename failed.\007\n";
   print STDERR "done. You have " . int($space/1000.+0.5) . " MB left.\n";
   
   # check the time...
   ($hh, $dd) =(localtime)[2,3];
   
   # quit if it's after 10a.m. AND and the job was either started
   # before 10a.m. or sometime yesterday...
   if( $hh > $quitting_time && 
       ( $hh_start < $quitting_time || $dd != $dd_start )){
     print "copymon: it's after $quitting_time o'clock --- normal exit.\007\n";
     last;
   }
   
   # quit if we've been running more than 18 hours...
   if((time-$^T) > 3600*$maxhours){
     print "copymon: program running more than $maxhours hours --- normal exit.\007\n";
     last;
   }
   
   # continue iteration of the loop...
   redo;
 }
}
