#!/usr/bin/env perl 
#----------------------------------------------------------------------------
#
# Name: catalog_hst_non_cal.pl
#
# This perl script is used interactively.  This tool is run from a default
# directory that contains non-CAL/non-OMS HST datasets to be cataloged.  The 
# tool is designed to make it easy to catalog 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 run the catalog or catalog_ascii_main.pl.  Then it will run the 
# update_db_tool to generate housekeeping database updates.  These programs use
# the null path to define the regression Ingest pipeline.  The program searches
# resoures files to find the correct resource file and catalog program.  
#
# 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 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/17/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 = "catalog_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 = 1 ;
    my $usage = <<"EOM";    
Usage: 
>$nm -i <input_dir> -d <data_id>  
   
   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.  This tool supports all HST
                   data_ids except for classes CAL and OMS which have
                   associations.

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, $lc_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 );
        } 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 . " -d " . $lc_data_id ;
 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, 'n/a' ) } ;  
    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 );























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

    my ( $cat_resource, $cat_task ) = get_cat_resource( 
                                         "OPUS_DEFINITIONS_DIR", $lc_data_id );
    if  (defined( $cat_resource ) ) {
        my $msg = "cat_resource is $cat_resource " ;
        PrintMsg( "I", $msg, $nm ) ;
        $msg =  "cat_task    is $cat_task " ;
        PrintMsg( "I", $msg, $nm ) ;
        $msg = "Calling: catalog_datasets(\@datasets, $cat_task, ";
        $msg = $msg . "$cat_resource, $lc_data_id)" ;
        PrintMsg( "D", $msg, $nm ) ;
        eval { catalog_datasets( \@datasets, $cat_task, $cat_resource, $lc_data_id ) } ;
        if ( $@ ) {
            PrintMsg( "E", $@ ) ;  # no nm because die will mention this script
            exit( $EXIT_FAILURE );
        }
    } else {
        my $msg = "No catalog updates required for data_id $lc_data_id" ;
        PrintMsg( "I", $msg, $nm ) ;
    }
    my $hkp_resource ;
    eval { $hkp_resource = get_hkp_resource( \@def_dirs, $lc_data_id ) } ;
    if ( $@ ) {
        PrintMsg( "E", $@ ) ;  # no nm because die will mention this script
        exit( $EXIT_FAILURE );
    }
    
    if (defined($hkp_resource)) {
        my $msg = "hkp_resource is $hkp_resource " ;
        PrintMsg( "I", $msg, $nm ) ;
        $msg = "Calling: hkp_datasets(\@datasets, $hkp_resource, $lc_data_id)" ;
        PrintMsg( "D", $msg, $nm ) ;
        eval { hkp_datasets(\@datasets, $hkp_resource, $lc_data_id) } ;
        if ( $@ ) {
            PrintMsg( "E", $@ ) ;  # no nm because die will mention this script
            exit( $EXIT_FAILURE );
        }
    } else {
        my $msg = "No housekeeping updates required for data_id: $lc_data_id";
        PrintMsg( "I", $msg, $nm ) ;
    } 
########################################################
 ## Tell user that we are done
 $msg = "Completed..." ;
 $msg = $msg . $nm . " -i " . $inDir . " -d " . $lc_data_id ;
 PrintMsg( "I", $msg ) ; ## msg contains nm
########################################################
    exit( $EXIT_SUCCESS );  

