#!/usr/bin/env perl
#----------------------------------------------------------------------------
#
# Name: rawjit_check
#
# This perl script verifies the database to determine what level of processing
# is required and whether the RAWJIT process can be started. It uses the
# following input tables from OPUS_DB. 
#
#     qolink_sms
#     dataset_link
#     jitter_evt_map
#     product_status
#
# It uses the process exit status to control the actions of XPOLL which 
# provides the necessary OSF changes in the pipeline. The interactive usage
# is only used for testing where the value of the return status can be
# examined as well as the log message. 
#
# Interactive Usage:
#	>rawjit_check.pl jitter_rootname
#
#	Note that the following environment variables (logicals) are 
#       always required: DSQUERY and OPUS_DB.
#
# If DBI encounters a failure, this program is terminated.  Don't like that.
# This is bad for calculating run times because End message is NOT printed.
#
#
# History:
# Date     OPR      Who         Reason
# -------- -------- ----------  ---------------------------------------------
# 06/29/01 43969    Baum        Initial code
# 04/29/02 45738    Baum        Use external subroutines.
# 10/16/02 46785    Baum        Support lowercase OSF rootnames
# 12/04/02 47074    Baum        Put hold check before internal check
# 03/06/08 44712    Sherbert    Move from ST_DBlib to DBI interface
# 06/30/10 64432    MSwam     Use single quotes for SQLServer
# 
#----------------------------------------------------------------------------

 # set up external routines
 unshift @INC,(split /:/, $ENV{PATH});
 require 'printmsg.pl';       # prints a formatted status message
 require 'do_dbi_pkg.pl';     # run queries that return records
 #----------------------------------------------------------------------------

 #specify exit status values

 $PROCESS_FAILURE  = 99; # exit status for XPOLL
 $NO_SCIENCE       = 9; 
 $HOLD_FOR_SCIENCE = 11;
 $NO_GSA_REQUIRED  = 15;
 $GSA_WAIT         = 17;
 $GSA_READY        = 19;

 #check for arguments

 $num_arg = scalar @ARGV;

 if ( $num_arg > 0 && substr( $ARGV[0], 0, 1 ) ne "-" ) {
    $osf_root = $ARGV[0];
    $rootname = uc($ARGV[0]);
 } else {
    $osf_root = $ENV{"OSF_DATASET"};
    $rootname = uc($osf_root);
 }
 if ( ! defined( $rootname ) ) {
    PrintMsg( "E", "Missing rootname name in arg or OSF_DATASET" );
    exit( $PROCESS_FAILURE );
 } 

 # get the required external variables

 $DSQUERY=           $ENV{"DSQUERY"};
 $OPUS_DB=           lc($ENV{"OPUS_DB"});
 my $continue = 1;

 # begin processing

 PrintMsg( "I", "--- start --- Check RAWJIT $osf_root readiness ----------" );
 #---------------------------------------------------------------------
 # open database for queries
 #---------------------------------------------------------------------
 $db = DoDBIopen( $DSQUERY, $OPUS_DB, $PROCESS_FAILURE );

 #---------------------------------------------------------------------
 #  Query event data for this observation
 #---------------------------------------------------------------------
 $query = <<"EOQ";
SELECT j.event_start_time, j.event_type, j.internal_flag
FROM jitter_evt_map j, dataset_link l
WHERE l.dataset_rootname= '$rootname' 
  AND l.dataset_type = 'FGS' 
  AND j.program_id = l.program_id 
  AND j.obset_id = l.obset_id 
  AND j.ob_number = l.ob_number
EOQ

 @in_record  = DoDBIselect( $db, $query );
 $event_time = $in_record[0];
 $event_type = $in_record[1];
 $internal   = $in_record[2];
 ## If query fails, DBI kills this program.  exit status is $PROCESS_FAILURE.
 ## If query succeeds, program continues.  

 #---------------------------------------------------------------------
 #   Get time_type and status for this jitter file. Check conditions.
 #---------------------------------------------------------------------
 $query = <<"EOQ";
SELECT o.status, o.time_type
FROM qolink_sms o, dataset_link l
WHERE l.dataset_rootname= '$rootname' AND l.dataset_type = 'FGS' AND
  o.program_id = l.program_id AND o.obset_id = l.obset_id AND
  o.ob_number = l.ob_number
EOQ

 @in_record = DoDBIselect( $db, $query );
 $status    = $in_record[0];
 $time_type = $in_record[1];
 if ( $status eq "N" ) {
    PrintMsg( "I", "Science data not available. No jitter products needed." );
    $exit_status = $NO_SCIENCE;
    $continue = 0;
 } elsif ( $time_type ne "A" ) {
    PrintMsg( "I",
           "qolink_sms actual times not updated. Hold for science update." );
    $exit_status = $HOLD_FOR_SCIENCE;
    $continue = 0;
 } elsif ( $event_type eq "SMS" || $internal eq "Y" ) {
    PrintMsg( "I", "No GSA data needed for this observation." );
    $exit_status = $NO_GSA_REQUIRED;
    $continue = 0;  
 } # else - continue

 #---------------------------------------------------------------------
 #  Determine GSA product name for this observation
 #---------------------------------------------------------------------
 if ( $continue ) {
    $gsa_rootname =  $event_time;
    $gsa_rootname =~ s/^(....)\.(...):(..):(..):(..)/G$1$2$3$4$5/;

    $query = <<"EOQ";
SELECT complete_flag 
FROM product_status
WHERE product_rootname= '$gsa_rootname' AND product_type = 'GSA'
EOQ

    @complete = DoDBIselect( $db, $query );
    if ( $complete[0] eq "N" ) {
        PrintMsg( "I", "Wait for GSA data $gsa_rootname to be processed." );
        $exit_status = $GSA_WAIT;
    } else {
        PrintMsg( "I", "GSA data $gsa_rootname is ready." );
        $exit_status = $GSA_READY;
    }
 }

 #---------------------------------------------------------------------
 # end of all queries
 #---------------------------------------------------------------------
 DoDBIclose( $db );
 PrintMsg( "I", "---  end  --- Check RAWJIT $osf_root readiness ----------" );
 exit( $exit_status );  
