#!/usr/bin/env perl
#----------------------------------------------------------------------------
#
# Name: jitter_ast_osf_update
#
# This perl script is designed to be invoked as a subprocess of the JITTER
# process. It is used as an interface to the osf_test and osf_update routines.
#
# Interactive Usage:
#   jitter_ast_osf_update.pl pathname rootname column hold wait
#
#   All arguments are required. 
#       pathname must exist in the OPUS_DEFINITIONS_DIR
#       rootname is the 9 character IPPPSSOOT
#       column is the stage title, e.g. "AK"
#       hold is AK status for holding e.g. "h"
#       wait is the AK status for waiting to be processed e.g. "w"
#
#  Exit status:
#       11 = good: no path name or OSF not found or not in hold state
#       13 = good: OSF in hold state updated to wait state
#       15 = bad: error return from osf_test
#       17 = bad: error return from osf_update
#       19 = bad: too few or invalid arguments
#
# History:
# Date     OPR      Who       Reason
# -------- -------- --------- ---------------------------------------------
# 08/07/01 43973    Baum      Initial version
# 06/20/02 46012    Baum      Fix test of osf_test return value
# 05/08/06 55509    Sherbert  Remove inconsistencies with path defaults
#----------------------------------------------------------------------------

#specify exit status values
#---------------------------------------------------------------------

    $GOOD_NO_HOLD = 11;   # exit status for JITTER
    $GOOD_UPDATED = 13;   # exit status for JITTER
    $BAD_OSF_TEST = 15;   # exit status for JITTER
    $BAD_OSF_UPDT = 17;   # exit status for JITTER
    $BAD_ARGUMENT = 19;   # exit status for JITTER

#---------------------------------------------------------------------
#check arguments
#---------------------------------------------------------------------

    $num_arg = scalar @ARGV;

    if ($num_arg != 5) {
       exit( $BAD_ARGUMENT);
    }
    $ast_path_name    = $ARGV[0];
    $ast_rootname     = $ARGV[1];
    $ast_column       = uc($ARGV[2]);
    $ast_hold_status  = lc($ARGV[3]);
    $ast_wait_status  = lc($ARGV[4]);

#---------------------------------------------------------------------
# check for NONE path
#---------------------------------------------------------------------
    if ($ast_path_name eq "NONE" ) {
       exit( $GOOD_NO_HOLD);
    }
#---------------------------------------------------------------------
# restart holding ASTKWD process if it is in a hold status
#---------------------------------------------------------------------
    $osf_test = "osf_test -p $ast_path_name -t ast -f $ast_rootname ".
                "-pr $ast_column";
    $osf_stat = `$osf_test`;
    
    if ($?) {  # checking non-zero return status of osf_test
       exit( $BAD_OSF_TEST);
    }
    $osf_stat= lc(substr( $osf_stat, 0, 1));
    if ($ast_path_name eq "NONE" || $ast_hold_status ne $osf_stat) {
       exit( $GOOD_NO_HOLD);
    }
    $upd_command="osf_update -p $ast_path_name -f $ast_rootname".
      " -t ast -c $ast_column -s $ast_wait_status";
    if (system($upd_command)) {
       exit( $BAD_OSF_UPDT);
    } else {
       exit( $GOOD_UPDATED);
    }
