#!/usr/bin/env perl
#----------------------------------------------------------------------------
#
# Name: nsa_bypass.pl
#
# This perl script is used as an extenal task for the Ingest pipeline to be
# only for testing. The script will reside in the AUTO tools directory.
#
# This tool updates ingest_data_set_info table to simulate the completion of
# the RQ and RS stages of the Ingest pipeline.  
# 
# Pipeline usage only:
#
#   Requires ENV variable OSF_DATASET created by the Ingest pipline for
#   install OSFs. If this script is used for anything but the INgest install
#   OSF it will fail due to a database update error.
#
#  Return values:
#     7 - success
#     9 - failure
#
# ENV variables:
#     DSQUERY is the database server name
#     ARCH_DB is the DADS database name used by the Ingest pipeline
#    
# History:
# Date     OPR      Who         Reason
# -------- -------- ----------  ---------------------------------------------
# 04/06/05 52891    Baum        Initial code
# 08/12/10 65781    MSwam       single quotes for SQLserver
#----------------------------------------------------------------------------
# set up external routines
unshift @INC,(split /:/, $ENV{PATH});
require 'do_dbi_pkg.pl';     # DoDBI package - convenience routines
require 'printmsg.pl';       # prints a formatted status message

# begin 
    use strict;    # require explicit context for all variables (using "my")
    #specify exit status values
    my $EXIT_FAILURE =      7;   # exit status for errors
    my $EXIT_SUCCESS =      9;   # exit status for success

# check ENV variables used for queries
    my $DSQUERY         = $ENV{"DSQUERY"};
    my $ARCH_DB         = $ENV{"ARCH_DB"};
    my $install_id      = $ARGV[0];
    
    if (!defined($DSQUERY) || !defined($ARCH_DB)) {
       PrintMsg("F","Missing ENV variables: DSQUERY or ARCH_DB.");
       exit( $EXIT_FAILURE);
    }
    if (!defined($install_id)) {
       PrintMsg("F","Missing pipeline ENV OSF_DATASET.");
       exit( $EXIT_FAILURE);
    }        
    # Open database server connection for DADS, and set err exit code
    my $db = DoDBIopen( $DSQUERY, $ARCH_DB, $EXIT_FAILURE);    
    PrintMsg("I","Updating NSA status for $install_id.");

    # perform update of nsa fields in ingest_data_set_info
    my $query = <<"EOQ";
UPDATE ingest_data_set_info
SET ids_nsa_req_date = dateadd( minute, -2, getdate()),
    ids_nsa_rsp_date = getdate()
WHERE ids_ins_request_id = '$install_id'
EOQ

    my $count = DoDBI( $db, $query);
   
    if ($count != 1) {
       PrintMsg("F","Failed to update ingest_data_set_info table.");
       exit( $EXIT_FAILURE);
    }
    PrintMsg("I","Successful update of ingest_data_set_info table.");
    DoDBIclose($db);
    exit( $EXIT_SUCCESS);  

