#!/usr/bin/env perl 
#----------------------------------------------------------------------------
#
# Name: catalog_hst_cal_oms.pl
#
# This perl script is used interactively.  This tool is run from a default
# directory that contains either CAL or OMS HST datasets to be cataloged.  
# CAL and OMS files cannot be mixed in the same directory.  The 
# tool is designed to make it easy to catalog multiple datasets out of the same
# directory.  The user must specify on the command line whether the default
# directory contains CAL or OMS data.  
#
# The program assumes that the ingest_hst_cal_oms.pl tool was used to ingest
# the datasets.  Since that tool does OMS renaming and ASN regeneration, this
# tool does not have those features.  
#
# To handle associations properly, the script will process the associations
# first.  For every association, it will use the asn.fits file the datasets for
# the association.  Any dataset assigned to an association will be eliminated 
# from the list of datasets found in the default directory.  After all 
# associations have been processed, the datasets remaining in the list will be
# ingested as singletons.   
#
# Account requirements:
#
# The account using this progrom must have used the opus_login.csh setup
# to set the proper ENV variables, $DSQUERY and $OPUS_DB.  The $PATH variable
# 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
# -------- -------- ----------  ---------------------------------------------
# 04/11/05 53339    Baum        Initial code
# 03/01/10 59963    Sherbert    Add input parameter
# 09/07/10 59963    Sherbert    Need to call cat_cos_copies (lil change 9/24)
#----------------------------------------------------------------------------
# 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 = "catalog_hst_cal_oms.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

# define command usage
    my $numRequired = 1 ;
    my $usage = <<"EOM";    
Usage: 
>$nm -i <input_dir> -c <archive_class>  

   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.

   <archive_class> is either CAL or OMS.

   * MSG_REPORT_LEVEL and STDB_REPORT_LEVEL settings are turned off before 
     calls to ingrsp and update_db_tool (housekeeping).
   
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 < 2. ", $nm ) ;

    if ($num_args < 2) {    
        PrintMsg( "E", "Too few arguments" ) ; ## Usage will provide nm
        print $usage;
        exit( $EXIT_FAILURE ) ;
    }
    my ( $option, $inDir, $archive_class, $msg ) ;
    
    # verify the required option(s)
    my $requiredFound = 0 ;
    my $optionalFound = 0 ;
    while (scalar @ARGV) {
        $option = shift @ARGV;
        if ($option eq "-c") {
            $requiredFound = $requiredFound + 1 ;
            $archive_class = uc(shift @ARGV);
        } elsif ($option eq "-i") {
            $optionalFound = $optionalFound + 1 ;
            $inDir = 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 . " -c " . $archive_class ;
 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_cal_oms_datasets($inDir, $archive_class) } ; 
    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 list of associations 
    my @asn_datasets ;
    eval { @asn_datasets = get_asn_datasets($inDir, \@datasets,$archive_class) } ;
    if ( $@ ) {
        PrintMsg( "E", $@ ) ;  # no nm because die will mention this script
        exit( $EXIT_FAILURE );
    }
    $msg = '@asn_datasets are ' ;
    $msg = $msg . "(" . join(", ", @asn_datasets) . ")";
    PrintMsg( "D", $msg, $nm );
    # get list of all request datasets - eliminating association members
    my @request_dsets ;
    eval { @request_dsets = get_request_datasets($inDir, \@datasets,\@asn_datasets, $archive_class) } ;
    if ( $@ ) {
        PrintMsg( "E", $@ ) ;  # no nm because die will mention this script
        exit( $EXIT_FAILURE );
    }
    $msg = '@request_dsets are ' ;
    $msg = $msg . "(" . join(", ", @request_dsets) . ")";
    PrintMsg( "D", $msg, $nm );

    # get sublists of datasets that use the same data_ids
    my @cat_sublists = request_sublists(\@request_dsets, $archive_class);
    $msg = '@cat_sublists are ' ;
    $msg = $msg . "(" . join(", ", @cat_sublists) . ")";
    PrintMsg( "D", $msg, $nm );




    my $opusConn ;
    eval { $opusConn = open_opus_db( $DSQUERY, $OPUS_DB ) } ;
    if ( $@ ) {
        PrintMsg( "E", $@ ) ;  # no nm because die will mention this script
        exit( $EXIT_FAILURE );
    }
    
    foreach my $sublist_ref (@cat_sublists) {
       if ($archive_class eq "CAL") {
          # copy virtual files  
          eval { ingrsp_sublist( $opusConn, $sublist_ref ) } ;
          if ( $@ ) {
              PrintMsg( "E", $@ ) ;  # no nm because die will mention this script
              exit( $EXIT_FAILURE );
          }
          # Create COS files copies if necessary
          # The cat_cos_copies.py script will weed out non-COS data  
          eval { copy_cos_sublist( $inDir, $sublist_ref ) } ;
          if ( $@ ) {
              PrintMsg( "E", $@ ) ;  # no nm because die will mention this script
              exit( $EXIT_FAILURE );
          }
       }
       # insert catalog records
       eval { catalog_sublist($inDir, $sublist_ref, $archive_class) } ;
       if ( $@ ) {
          PrintMsg( "E", $@ ) ;  # no nm because die will mention this script
          exit( $EXIT_FAILURE );
       }
       # update housekeeping records
       eval { hkp_sublist($sublist_ref, $archive_class) } ;
       if ( $@ ) {
          PrintMsg( "E", $@ ) ;  # no nm because die will mention this script
          exit( $EXIT_FAILURE );
      }
    }
    close_opus_db( $opusConn );
########################################################
 ## Tell user that we are done
 $msg = "Completed..." ;
 $msg = $msg . $nm . " -i " . $inDir . " -c " . $archive_class ;
 PrintMsg( "I", $msg ) ; ## msg contains nm
########################################################
    exit( $EXIT_SUCCESS );  

