#!/usr/bin/env perl 
#----------------------------------------------------------------------------
#
# Name: ingest_hst_non_cal.pl
#
# This perl script is used interactively.  This tool is run from a default
# directory that contains non-CAL and non-OMS HST datasets to be ingested.  
# This directory must contain only one type of data.  The tool is designed to 
# make it easy to ingest multiple datasets out of the same directory.  
# The file name format may match either the pattern "*_ext.*" or 
# "*.ext".  The user must know the OPUS data_id used to process the data.  
# If an invalid data_id is supplied, the script displays the list of valid
# data_ids with the archive class that is assigned to it.  The script will
# always modify the data_id value to upcase the first character.  The script
# will use the null.path to find the Ingest path name and it will assure that
# this is an Ingest path that has the keyword OVERRIDE_REQUIRED=Y value.  The
# script will use the Ingest path file to find the INGEST_TRIGGER_ROOT value
# and the INGEST_SOURCE_DIR.  The script will use the genreq process to copy and
# possibly rename the dataset files and create the trigger file, for each 
# dataset found in the default directory.  
#   
# Account requirements:
#
# The account using this progrom must have used the opus_login.csh setup
# to set the proper ENV variables, OPUS_DEFINITIONS_DIR,  $DSQUERY and $OPUS_DB.
# The $PATH file must include the OPUS bin directory containing Perl modules.  
#
# Command Usage: (see $usage definition below)
#
#  Return values:
#     0 - success
#     1 - failure
#
# ENV variables:
#     DSQUERY is the database server name
#     OPUS_DB is the OPUS database name
#    
# History:
# Date     OPR    Who        Reason
# -------- ------ ---------- ---------------------------------------------
# 08/11/04 51848  Baum       Initial code
# 03/01/10 59963  Sherbert   Changed input parameters 
# 08/24/10 63986  Sherbert   Convert SMS FITS to ascii for cataloging
#----------------------------------------------------------------------------

# set up external routines
unshift @INC,(split /:/, $ENV{PATH});
require 'ingest_tools_pkg.pl'; # shared subroutines for this tool 
require 'printmsg.pl' ;       # PrintMsg
use strict;

my $nm = "ingest_hst_non_cal.pl" ;
######################################
#$ENV{STDB_REPORT_LEVEL} = "STDB_INFO" ;   ## rm when testing done
#$ENV{MSG_REPORT_LEVEL}  = "MSG_ALL"   ;   ## rm when testing done
#PrintMsg( "D", "## Attempted to set STDB And MSG reporting HIGH", $nm );
######################################
PrintMsg( "D", "Running $0 " );  ## $0 == which

# begin 
    #specify exit status values
    my $EXIT_FAILURE =      1;   # exit status for errors
    my $EXIT_SUCCESS =      0;   # exit status for success
    my $CAL_OMS_FLAG =      0;   # this is not CAL or OMS d

# define command usage
    my $numRequired = 2 ;
    my $usage = <<"EOM";    
Usage: 
>$nm -i <input_dir> -d <data_id> -o <data_source>
   
   The order of [-flag value] pairs does not matter.  
   Tool now responds to MSG_REPORT_LEVEL settings*

   <input_dir>     is the location of the data to be archived.  
                   Only data from one type of archive_class should be here.
                   This is the optional parameter.  Defaults to current dir,
                   if omitted.
     
   <data_id>       is the OSF data_id used in normal processing.  It is
                   generally not equal to the archive class.  If it is
                   set to an invalid value, such as "xxx", then a list
                   of valid values with the name of the related archive
                   class is printed.  (You should also provide any random
                   -o param for this to work now.)  This tool supports all HST
                   data_ids except for classes CAL and OMS which have
                   associations.  For ingesting DIA archive class data 
                   from multiple instruments in the same input directory,
                   use the data_id dia.  The same goes for EDT class data.

   <data_source>   is the four character data source override value to be
                   set in the regression pipeline.

   * MSG_REPORT_LEVEL and STDB_REPORT_LEVEL settings are turned off before 
     calls to genreq. 

EOM

 # check ENV variables used for queries
 my $DSQUERY         = $ENV{"DSQUERY"};
 my $OPUS_DB         = $ENV{"OPUS_DB"};
        
 ## Be certain these ENV vars area available
 if (!defined($DSQUERY) || !defined($OPUS_DB) ) {
    my $msg = "Missing ENV variables: DSQUERY or OPUS_DB " ;
    PrintMsg( "E", $msg, $nm ) ;
    exit( $EXIT_FAILURE );
 }

    # start argument checks
    my $num_args = scalar @ARGV;
    PrintMsg( "D", "number of arguments is $num_args.  We fail if < 4. ", $nm ) ;

    if ($num_args < 4) {    
        PrintMsg( "E", "Too few arguments" ); ## Usage will provide nm
        print $usage;
        exit( $EXIT_FAILURE ) ;
    }
    my ( $option, $inDir, $data_source, $lc_data_id, $mc_data_id, $opusConn, $msg ) ;

    # verify the required option(s)
    my $requiredFound = 0 ;
    my $optionalFound = 0 ;
    while (scalar @ARGV) {
      $option = shift @ARGV;
      if ($option eq "-d") {
         $requiredFound = $requiredFound + 1 ;
         $lc_data_id = lc(shift @ARGV);
         eval { $opusConn = open_opus_db( $DSQUERY, $OPUS_DB ) } ;
         if ( $@ ) {
            PrintMsg( "E", $@ ) ;  # no nm because die will mention this script
            exit( $EXIT_FAILURE );
         }
         # validate argument value
         eval { check_hst_data_id( $opusConn, $lc_data_id, $CAL_OMS_FLAG ) } ;
         if ( $@ ) {
            PrintMsg( "E", $@ ) ;  # no nm because die will mention this script
            exit( $EXIT_FAILURE );
         }
         close_opus_db( $opusConn );
         $mc_data_id = uc(substr($lc_data_id,0,1)).substr($lc_data_id,1);
      } elsif ($option eq "-i") {
         $optionalFound = $optionalFound + 1 ;
         $inDir = shift @ARGV ;
      } elsif ($option eq "-o") {
         $requiredFound = $requiredFound + 1 ;
         $data_source = uc(shift @ARGV);
      } else {
         PrintMsg( "E", "Invalid option found: $option" ) ; ## Usage will provide nm
         print $usage;
         exit( $EXIT_FAILURE ) ;
      }
    }
    PrintMsg( "D", "requiredFound is $requiredFound.  I think it should be 2 out of 3.", $nm ) ;
    if ( $requiredFound < $numRequired ) {
        # Either -d or -o NOT found
        PrintMsg( "E",  "Too few arguments" ) ; ## Usage will provide nm
        print $usage;
        exit( $EXIT_FAILURE ) ;
    } elsif ( $optionalFound == 0 ) {
        # Default input dir to current dir
        $inDir = "./" ;
    }
########################################################
 ## Tell user what the command line is...
 $msg = "Starting... " ;
 $msg = $msg . $nm . " -i " . $inDir . " -d " . $lc_data_id . " -o " . $data_source ;
 PrintMsg( "I", $msg ) ; ## msg contains nm
########################################################

    my $pathname ;
    eval { $pathname = get_regr_path() } ;
    if ( $@ ) {
        PrintMsg( "E", $@ ) ;  # no nm because die will mention this script
        exit( $EXIT_FAILURE );
    }
    PrintMsg( "D", "pathname     is $pathname", $nm ) ;
    eval { check_pipeline( $pathname ) } ;
    if ( $@ ) {
        PrintMsg( "E", $@ ) ;  # no nm because die will mention this script
        exit( $EXIT_FAILURE );
    }

    # need dir to be a full path name
    my @pwdOut =`pwd` ;
   #print "#0: pwd sez we are sitting in: @pwdOut " ;
    use Cwd ;
    my $cwd = getcwd() ;
   #print "#1: my cwd is $cwd \n" ;
    my $size = length( $cwd ) ;
   #print "#2: length of cwd is $size \n" ;
    my $lastchar = substr( $cwd, $size-1, 1 ) ;
    if ( $lastchar ne "/" ) {
       #print "#   We need to add a slash to cwd \n" ;
        $cwd = $cwd . '/' ;
    }
   #print "#3: my cwd is $cwd \n" ;
   #print "#4: my inDir is $inDir \n" ;

    if ( substr($inDir, 0, 1) ne "/" ) {
       #print "#   This is NOT an absolute path, make it one. \n" ;
        $inDir = $cwd . $inDir ;
    }
   #print "#5: my inDir is $inDir \n" ;

    chdir $inDir ;
    PrintMsg( "I", "chdir'd to $inDir ", $nm ) ;
    @pwdOut =`pwd` ;
   #$msg = "pwd sez we are sitting in: @pwdOut " ;
   #PrintMsg( "I", $msg, $nm ) ;
   #my @lsOut = `ls` ;
   #PrintMsg( "I", "ls sez these files are here: ", $nm ) ;
   #print @lsOut, "\n" ;

    # get datasets in directory    
    my @datasets ;
    eval { @datasets = get_datasets( $inDir, $lc_data_id ) } ;
    if ( $@ ) {
        PrintMsg( "E", $@ ) ;  # no nm because die will mention this script
        exit( $EXIT_FAILURE );
    }
    $msg = '@datasets are ' ;
    $msg = $msg . "(" . join(", ", @datasets) . ")";
    PrintMsg( "D", $msg, $nm );

    # get parameters from path    
    my ($trig_dir, $source_dir) ;
    eval { ($trig_dir, $source_dir) = get_path_dirs( $pathname ) }; 
    if ( $@ ) {
        PrintMsg( "E", $@ ) ;  # no nm because die will mention this script
        exit( $EXIT_FAILURE );
    }
#   PrintMsg( "D", "trig_dir     is $trig_dir", $nm ) ;
#   PrintMsg( "D", "source_dir   is $source_dir", $nm ) ;


    my $hst_trig_dir = $trig_dir."hst/";
#   PrintMsg( "D", "hst_trig_dir is $hst_trig_dir", $nm ) ;
    $msg = "eval { set_data_source($source_dir,$data_source) } ";
    PrintMsg( "D", $msg, $nm ) ; 
    eval { set_data_source($source_dir,$data_source) } ;
    if ( $@ ) {
        PrintMsg( "E", $@ ) ; ## Usage will provide nm
        print $usage ;
        exit( $EXIT_FAILURE );
    }

    my @def_dirs = expand( "OPUS_DEFINITIONS_DIR" );
    $msg = '@def_dirs are ' ;
    $msg = $msg . "(" . join(", ", @def_dirs) . ")";
    PrintMsg( "D", $msg, $nm );

    my $resource_name ;
    eval { $resource_name = get_req_resource( \@def_dirs, $lc_data_id) } ;
    if ( $@ ) {
        PrintMsg( "E", $@ ) ;  # no nm because die will mention this script
        exit( $EXIT_FAILURE );
    }
    eval { request_datasets(\@datasets, $mc_data_id, $resource_name, $hst_trig_dir) } ;
    if ( $@ ) {
        PrintMsg( "E", $@ ) ;  # no nm because die will mention this script
        exit( $EXIT_FAILURE );
    }
########################################################
 ## Tell user that we are done
 $msg = "Completed..." ;
 $msg = $msg . $nm . " -i " . $inDir . " -d " . $lc_data_id . " -o " . $data_source ;
 PrintMsg( "I", $msg ) ; ## msg contains nm
########################################################
    exit( $EXIT_SUCCESS );  

