#!/usr/bin/env perl
##########################################################################
#
# routine: pushit.pl
#
# purpose: Triggers when a merge OSF is detected in data partitioning.
#          The number of entries for this ipppssoot is checked in the "podnames"
#          relation.  If only a single entry is found, then there are no more
#          pod files expected for this observation, and it can be pushed down
#          the pipeline.  If zero or more than one "podnames" entries are found,
#          then something is amiss, and the observation must be looked into.
#
# input:   ENV.PATH - shell path variable for prepending to Perl Include dir
#          ENV.DSQUERY - database server for OPUS database
#          ENV.OPUS_DB - name of OPUS database (must contain "podnames" relation)
#
# modification history:
#
#   date    opr     who     reason
# -------- -----  --------  --------------------------------------
# 04/03/00 43165  MSwam     first version
# 03/25/10 64274    MSwam     Replace ST_DBlib with DBI
# 
##########################################################################
#
# prepend the Perl include variable using all components of PATH
# (this allows us to reuse other OTFR Perl code)
#
$PATH  = $ENV{"PATH"};
@parts = split /:/, $PATH;
while (defined($val = pop(@parts))) {
   if (substr($val,0,1) eq "/") { unshift(@INC, $val)}
}

# define exit status values
$quit_this_dataset = 0;
$all_is_well = 1;

#
# include these modules
#
require 'printmsg.pl';       # prints a formatted status message
require 'do_dbi_pkg.pl';       # prints a formatted status message

    # set up DB connection
    $db = DoDBIopen ($ENV{"DSQUERY"}, $ENV{"OPUS_DB"}, $quit_this_dataset);

    # add a wildcard to the input
    $wild = $ENV{"OSF_DATASET"} . '%';

    $query = <<"EOQ";
SELECT podname,ipppssoot FROM podnames WHERE ipppssoot like '$wild'
EOQ
    $count = 0;
    $sth = DoDBIexecute( $db, $query);
    while ( ($podname, $ipppssoot) = DoDBIfetch( $db, $query, $sth) ) {
        print "$podname $ipppssoot\n";
        $count++;
    }
    DoDBIclose($db);
    
    if ($count == 1) {
        exit 1;  # only a single pod found, OK to push past DP
    }
    else {
        exit 0;  # either 0 pods or multiple pods found, cannot push past DP
    }
