#!/usr/bin/env perl
#----------------------------------------------------------------------------
#
# Name: override_clean_delay.pl
#
# This perl script is used interactively.This tool resets the 
# ids_clean_delay_days field in the ingest_data_set_info table for any dataset
# that has reached the CL stage of the pipeline. These are the datasets that
# are waiting to be cleaned up after the expiration of the clean delay time. 
# The tool only resets clean delay values for the pipeline designated by either
# the pipeline path file name or the four character pipe id value which is also
# defined in the pipeline path file keyword INGEST_PIPE_ID. The path file name
# is the same as that used for the PMG and the pipe id is the value seen in the
# second through fifth characters of the NSA request ids for ingest datasets. 
#
# Account requirements:
#
# The account using this progrom must have used the opus_login.csh setup
# to set the proper ENV variables for either this script or the clean_ingest.pl
# script.
#
# Command Usage: (see $usage definition below)
#
#  Return values:
#     0 - success
#     1 - failure
#
# ENV variables:
#     ARCH_SERVER is the database server name
#     ARCH_DB is the DADS database name used by the Ingest pipeline
#    
# History:
# Date     OPR      Who        Reason
# -------- -------- ---------- ---------------------------------------------
# 04/27/04 50937    Baum       Initial code
# 03/24/10 64274    MSwam      Replace ST_DBlib with DBI
# 06/30/10 64432    MSwam      Use single quotes for SQLServer
# 09/18/12 72255    Sherbert   get rid of DSQUERY
#----------------------------------------------------------------------------
# set up external routines
unshift @INC,(split /:/, $ENV{PATH});
require 'do_dbi_pkg.pl';         # run query returning only record count

# begin 
    #specify exit status values
    $EXIT_FAILURE =      1;   # exit status for errors
    $EXIT_SUCCESS =      0;   # exit status for success

# check ENV variables used for queries
    $ARCH_SERVER     = $ENV{"ARCH_SERVER"};
    $ARCH_DB         = $ENV{"ARCH_DB"};
    if (!defined($ARCH_SERVER) || !defined($ARCH_DB)) {
       print "Missing ENV variables: ARCH_SERVER or ARCH_DB.\n";
       exit( $EXIT_FAILURE);
    }
# define command usage
    $usage = <<"EOM";    
Usage: 
>override_clean_delay.pl <delay_days> (-p <pipeline_path_file> | -i <pipe_id>) 
   ( -s <science_inst_codes> 
     | all 
     | (<archive_class-1> [<archive_class-2 ...])
   )
   
   where <delay_days> is a required integer value that may be 0; 
   either the -i or -p values, but not both, must be provided. 
   At the end of this command, one of the following options is required:
   
   1) "-s <science_inst_codes>",  where <science_inst_codes> is a set of 
     letters without spaces from the set "ijlnou" for the instruments 
     (wf3,acs,cos,nicmos,stis,wfpc2), respectively, and the archive class
     CAL is assumed. The case of these letters does not matter.
     
   2) "all" to specify all archive classes.
   
   3) A list of archive classes separated by blanks. The case of the class 
   name does not matter.
   
    
  Examples:
    To reset the clean delay to 7 days for POD and EDT datasets in the 
    "ingest" pipeline, use:    
        >override_clean_delay.pl 7 -p ingest pod edt

    To reset the clean delay to 12 days for ACS and STIS datasets for the
    pipeline id DADS, use: 
        >override_clean_delay.pl 12 -i DADS -s jo

    To reset the clean delay to 30 days for all datasets in the "ingest"
    pipline, use:
        >override_clean_delay.pl 7 -p ingest all 
       
EOM
    # start argument checks
    $num_args = scalar @ARGV;

    if ($num_args < 4) {    
        print $usage;
        exit ($EXIT_FAILURE);
    }
    # verify first argument is an integer
    $delay_days = shift @ARGV;
    
    if ( !($delay_days =~m/^\d+$/)) {
        print "Error - first argument must be an integer.\n $usage";
        exit ($EXIT_FAILURE);        
    }    
    #verify the second argument
    $option = shift @ARGV;
    
    if ($option eq "-p") {
        # Verify ENV variable set by opus_login.csh
        $sogs_disk = $ENV{"SOGS_DISK"};

        if ( !defined($sogs_disk)) {
            print "Error - Cannot use -p option without opus_login.csh.\n";
            exit ($EXIT_FAILURE);
        }
        $pathfilename = (shift @ARGV).".path";
        $pathfile = `osfile_stretch_file "OPUS_DEFINITIONS_DIR:$pathfilename"`;
        if ($pathfile =~m/^OPUS_DEFINITIONS_DIR/) {
            print "Error - Invalid path name: $pathfilename\n";
            exit ($EXIT_FAILURE);
        } else {    
            print "Obtained full path file name: $pathfile\n";
        }
        if (!open( PATHFILE, "<$pathfile") ) {
            print "Error - cannot open $pathfile\n";
            exit ($EXIT_FAILURE);
        }
        while (<PATHFILE>) {
           $record = $_;
           if ($record =~m/^INGEST_PIPE_ID/) {
              # parse line to extract value after the equal sign
              ($pipe_id) = ($record =~m/^INGEST_PIPE_ID\s*=\s*(\S+)/);
              last;
           }
        }
        close PATHFILE; 
        if (!defined($pipe_id)) {
            print "Error - could not find INGEST_PIPE_ID in path file.\n";
            exit ($EXIT_FAILURE);
        }
        print "Found INGEST_PIPE_ID: $pipe_id\n";
    } elsif ($option eq "-i") {
       $pipe_id = shift @ARGV;
    } else {
        print "Error - must have option -p or -i after integer\n $usage";
        exit ($EXIT_FAILURE);
    }
    # verify pipe_id which must have only alphanumeric characters
    if ( !($pipe_id =~m/^\w+$/)) {
        print "Error - pipe_id must be alphanumeric.\n $usage";
        exit ($EXIT_FAILURE);        
    }
    # open database for queries
    $db = DoDBIopen( $ARCH_SERVER, $ARCH_DB, $EXIT_FAILURE);

    # from here on, use ErrorExit to close $db with error message
    #    
    # check next arg
    $next_arg = $ARGV[0];
    
    if ($next_arg eq "-s") {
       shift @ARGV;
       $num_args = scalar @ARGV; 
       if ($num_args == 0) {
          ErrorExit("Failed to supply instrument code.\n $usage");
       } elsif ($num_args > 1) {       
          ErrorExit("Extra argument after instrument codes.\n $usage");
       } else {
          reset_by_sci_code_list();
       }
    } elsif (uc($next_arg) eq "ALL") {
       reset_all_classes();
    } else {
       reset_by_class_list();
    }
    DoDBIclose($db);
    undef $db;     

    # run cleanup tool to process any expired datasets
    `clean_ingest.pl $pipe_id`;
     
    exit( $EXIT_SUCCESS);  
#----------------------------------------------------------------------------
sub ErrorExit {   
   # one argument - the error message without newline
   # exit the script with the error condition after closing the $db database
   # objest and writing the error message to the log file.
   
   my ($msg) = @_;
   if (defined($db)) {
      DoDBIclose($db);
   }
   print "Error - ".$msg."\n";
   exit ( $EXIT_FAILURE);
}
#-----------------------------------------------------------------------------
sub reset_by_sci_code_list {   # no args
   # use the values remaining in @ARGV as science dataset prefixes
   my $sci_codes = uc(shift @ARGV);
   my $dataset_mask;
   if (length($sci_codes) == 1) {
      $dataset_mask = "'".$sci_codes."%'";
   } else {
      $dataset_mask = "'[".$sci_codes."]%'";
   }
   my $query = <<"EOQ";
UPDATE ingest_data_set_info 
SET ids_clean_delay_days = $delay_days
WHERE ids_clean_delay_days != 999 and ids_archive_class = 'CAL' and 
   ids_ins_request_id like 'I$pipe_id%' and 
   ids_data_set_name like $dataset_mask
EOQ

   my $rec_count = DoDBI( $db, $query);
   
   if ($rec_count == 0) {
      print "Warning - no records updated for query:\n$query";
   } else {
      print "$rec_count records have ids_clean_delay_days set to ".
         "$delay_days\n";   
   }             
} 
#-----------------------------------------------------------------------------
sub reset_all_classes {   # no args
   # reset clean delay for all classes
   
   my $query = <<"EOQ";
UPDATE ingest_data_set_info 
SET ids_clean_delay_days = $delay_days
WHERE ids_clean_delay_days != 999 and 
   ids_ins_request_id like 'I$pipe_id%'
EOQ

   my $rec_count = DoDBI( $db, $query);
   
   if ($rec_count == 0) {
      print "Warning - no records updated for query:\n$query";
   } else {
      print "$rec_count records have ids_clean_delay_days set to ".
         "$delay_days\n";   
   }             
}
#-----------------------------------------------------------------------------
sub reset_by_class_list {   # no args
   # use the values remaining in @ARGV as archive classes
   
   my $class_set;
   if (scalar @ARGV == 1) {
      $class_set = "='".uc(shift @ARGV)."'";
   } else {
      $class_set = "IN ('".uc(shift @ARGV)."'";
      while (scalar @ARGV) {
         $class_set .= ",'".uc(shift @ARGV)."'";
      }
      $class_set .= ')';
   }
   my $query = <<"EOQ";
UPDATE ingest_data_set_info 
SET ids_clean_delay_days = $delay_days
WHERE ids_clean_delay_days != 999 and ids_archive_class $class_set and 
   ids_ins_request_id like 'I$pipe_id%'
EOQ

   my $rec_count = DoDBI( $db, $query);
   
   if ($rec_count == 0) {
      print "Warning - no records updated for query:\n$query";
   } else {
      print "$rec_count records have ids_clean_delay_days set to ".
         "$delay_days\n";   
   }             
} 
