#!/usr/bin/env perl
#----------------------------------------------------------------------------
#
# Name: override_data_source.pl
#
# This perl script is used either in scripts supporting regression tests, or
# interactively for any reason. This script sets or resets the override file
# used by the UPDATE task of the Ingest pipeline to override the
# ads_data_source field of teh achive_data_set_all table. Normally
# this value is set by the ingest_rm_control table based on the pipeline
# that creates the Ingest OSF. When an empty override file with the name
# "override_source.xxxx" is found in the INGEST_SOURCE_DIR, the UPDATE task will
# uppercase the "xxxx" value and use it in place of the value found in the
# ingest_data_set_info table when it updates the archive_data_set_all table.
#
# The script requires either the file path name of the INGEST_SOURCE_DIR or the
# path-file name when this parameter is defined. The path-files are located in
# the OPUS_DEFINITIONS_DIR directory. OPUS_DEFINITIONS_DIR is an ENV variable
# set in the opus_login.csh script. If the opus_login.csh is not being used for
# this account, then the user must know the value used for the path-file
# parameter INGEST_SOURCE_DIR. When using the path-file, the opus_login.csh or
# the user account must include the SHARE application in it $PATH to access
# the program osfile_stretch_file that finds the full specification of the 
# path-file.
#
# Command Usage: (see $usage definition below)
#
#  Return values:
#     0 - success
#     1 - failure
#
# History:
# Date     OPR      Who         Reason
# -------- -------- ----------  ---------------------------------------------
# 04/15/04 50933    Baum        Initial code
#----------------------------------------------------------------------------
# begin 
    #specify exit status values
    $EXIT_FAILURE =      1;   # exit status for errors
    $EXIT_SUCCESS =      0;   # exit status for success
    $override_name = "source_override";

    $usage = <<"EOM";    
Usage: 
  override_data_source.pl (-p <pathfile> | -d <ingest_source_dir>) [<value>]

  where:
     <pathfile> is the filename without extension of the path definitions
     file found in OPUS_DEFINITIONS_DIR for the Ingest pipeline. It is a
     case-sensitive value.

     <ingest_source_dir> is the full path name (starting and ending in a /)
     of the directory that contains the override file. Always lowercase.

     If <value> is not present, the old $override_name file is deleted, so
     that the override is eliminated. Otherwise, <value> is the data source
     name, whose uppercased value will be used to set the ads_data_source
     field of the archive_data_set_all table. This value is not case
     sensitive.
     
     Either the -p or -d options, but not both, must be present.
     
  Examples:
    override_data_source.pl -p ingest regr   
         or   
    override_data_source.pl -d /store/sthubbins/ingest/trigger/source/ regr            
EOM

    $num_args = scalar @ARGV;

    if ($num_args < 2 || $num_args > 3) {    
        print $usage;
        exit ($EXIT_FAILURE);
    }

    if ($ARGV[0] 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 = $ARGV[1].".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_SOURCE_DIR/) {
              # parse line to extract value after the equal sign
              ($override_dir) = ($record =~m/^INGEST_SOURCE_DIR\s*=\s*(\S+)/);
              last;
           }
        }
        close PATHFILE; 
        if (!defined($override_dir)) {
            print "Error - could not find INGEST_SOURCE_DIR in path file.\n";
            exit ($EXIT_FAILURE);
        }
        print "Found INGEST_SOURCE_DIR: $override_dir\n";
    } elsif ($ARGV[0] eq "-d") {
       $override_dir = $ARGV[1];
    } else {
        print "Error = invalid first argument.\n $usage";
        exit ($EXIT_FAILURE);
    }
    if (!(-d $override_dir)) {
        print "Error - $override_dir is not a valid directory.\n"; 
        exit ($EXIT_FAILURE);
    }
    # delete any old files
    $unlink_count = unlink glob( $override_dir.$override_name.".*");
    if ($unlink_count) {
        print "Deleted old file from $override_dir.\n";
    }  else {
        print "No old files found in $override_dir.\n";
    }  
    if ($num_args == 3) {
       $extension = lc($ARGV[2]);
       $ext_length = length( $extension);
       if ($ext_length < 4 || $ext_length > 6) {
           print "Error - data source names must be 4 to 6 characters.\n";
           exit ($EXIT_FAILURE);
       }
       $override = $override_dir.$override_name.".".$extension;
       
       if (!open (OVERRIDE, ">$override")) {
           print "Error - cannot create new file - protection issue?\n";
           exit ($EXIT_FAILURE);           
       }
       close OVERRIDE;
       print "Created: $override\n";
    } 
    exit( $EXIT_SUCCESS);  
#----------------------------------------------------------------------------
