#!/usr/bin/env perl 
#-w
##########################################################################
#
# routine: reqcst.pl
#
# purpose: Request collector for SIs that do not support multi-file ASNs
#          (e.g. STIS, WF2).
#          Checks the data reduction blackboard to see if the requested product
#          for an OTFR request has
#          completed calibration.  If so, the files are copied to the OTFR
#          request-specific directory to prepare for their return
#          to DADS.
#
#          A timeout facility exists so that requests will not wait
#          indefinitely at the collector stage.
#
# modification history:
#
#   date    opr       who     reason
# -------- -----    --------  --------------------------------------
# 09/09/00 43165    MSwam     first version
# 04/11/01 43165_17 MSwam     differentiate timeout cases
# 11/26/01 44598_01 MSwam     parameterize calibration stage name
# 04/04/03 43915    WMiller   DADS 10.0 support
# 05/29/03 43915    Sherbert  Comment out -w switch 
# 05/29/03 43915    Sherbert  Add calstage and value to vague log message
#                             printed when collection is done
# 08/21/03 48370    Sherbert  Add missing /bin/ qualifier to cp
# 06/30/10 44712    MSwam     Remove runsql include
# 
##########################################################################
#
# prepend the Perl include variable using all components of PATH
# (this allows us to reuse other OTFR Perl code)
#
$PATH  = $ENV{"PATH"};
@parts = split /:/, $PATH;
while (defined($val = pop(@parts))) {
   if (substr($val,0,1) eq "/") { unshift(@INC, $val)}
}

#
# include these modules
#
require 'printmsg.pl';       # prints a formatted status message
require 'resolve.pl';        # resolve stretches 
require 'otfr_setup.pl';     # OTFR pipeline set-up for processing a request
require 'db_otfr_stats_dc.pl'; # fill otfr statistics in db

OTFR_setup();

PrintMsg("I","Processing $p_dir"); 

#
# define exit code for "results not ready yet" and "timeout expired"
$try_again_later = 3;
$flushit = 4;

#
# set-up 
$path = $ENV{"DATA_REDUCTION_PATH"};
$TIMEOUT_INTERVAL = $ENV{"TIMEOUT_INTERVAL"};
$CAL_STAGE_NAME = $ENV{"CAL_STAGE_NAME"};
$data_dir = Resolve("CAL_DIR");
$ipppssoo = substr($p_dset, 0, 8);
$rootname = uc($p_dset);

# test for collector timeout file
#
$timeout_file = "$p_dir.timeout";
if (-e $timeout_file) {

   # get file modification time
   ($dontcare_dev,$dontcare_ino,$dontcare_mode,$dontcare_nlink,$dontcare_uid,$dontcare_gid,
   $dontcare_rdev,$dontcare_size,$dontcare_atime,$mtime,$dontcare_ctime,$dontcare_blksize,
               $dontcare_blocks) = stat($timeout_file);
            
   # convert to days and compare
   $age_secs = (time - $mtime);
   $age_mins = $age_secs / 60.0;
   $age_hours = $age_mins / 60.0;
   $age_days = $age_hours / 24.0;
   if ($age_days > $TIMEOUT_INTERVAL) {
       #
       # try file copy for the product (failure is possible if the data are stuck)
       PrintMsg("W","TIMEOUT occurred: flushing files as is for $ipppssoo from $data_dir");
       `/bin/cp $data_dir/$ipppssoo* .`;

       #
       # save time request was flushed
       use POSIX qw(strftime);
       $flush_time = strftime "%d-%b-%Y %T", localtime;

       # update OTFR stats in database
       DB_otfr_stats_dc($rootname,$p_requestid,$flush_time);

       # the request is now considered flushed
       #
       exit $flushit;
   }
}
else {
   # create an empty timeout file
   if (!open(TIMEOUT,">$timeout_file")) {
      PrintMsg("E","ERROR: Failed to create $timeout_file file\n");
      exit $quit_this_dataset;
   }
   close(TIMEOUT);
}

# check the exposure in the request name for completion in the data reduction path
# (the product, if an ASN)
@out = `osf_test -p $path -f '$ipppssoo?' -pr $CAL_STAGE_NAME`;
if ($? != 0) {
      PrintMsg("E","ERROR: osf_test failed for $path, $ipppssoo\n");
      exit $quit_this_dataset;
}

if (scalar(@out) == 0) {
      # no output means exposure is not ready
      $ca_value = " ";
}
else {
      # parse first OSF output
      $ca_value = $out[0];
      # remove newline and extra space
      chomp($ca_value);
      chop($ca_value);
}

if ($ca_value =~ /^[cnf]/ ) {
   PrintMsg("I","$ipppssoo: calibration COMPLETE, NOT PERFORMED, or FLUSHED ($CAL_STAGE_NAME=$ca_value).");
}
else {
   # if product is not ready, notify
   PrintMsg("I","$ipppssoo: calibration not ready; $CAL_STAGE_NAME =$ca_value");
   exit $try_again_later;
}

#
# copy product files from the pipeline directory
#
PrintMsg("I","copying files for $ipppssoo from $data_dir");
`/bin/cp $data_dir/$ipppssoo* .`;
if ($? != 0) {
   PrintMsg("E","ERROR: failed to copy $ipppssoo files from $data_dir\n");
   exit $quit_this_dataset;
}

#
# save time science processing was found to be complete
use POSIX qw(strftime);
$stop_time = strftime "%d-%b-%Y %T", localtime;

# update OTFR stats in database
DB_otfr_stats_dc($rootname,$p_requestid,$stop_time);

exit $all_is_well;
