#!/usr/bin/env perl
#
# name : check_cal_state.pl
#
# purpose : Given a 9-character dataset name, determine if this
#		dataset should be calibrated, as dictated by the
#		otfr_special_processing table.
#
#           Accesses the otfr_special_processing relation in the
#           ARCH_SERVER:ARCH_DB database (ARCH_SERVER and ARCH_DB are
#           environment variables).
#
# usage:    check_cal_state.pl u2440101t
#
# returns:  exit status = 0 if OK to calibrate 
#                              (either osp_processing_steps does NOT contain
#                               the string "BYPASS_CAL"
#                               OR failed to read database)
#           exit status = 1 if calibration should NOT occur 
#                              (osp_processing_steps contains "BYPASS_CAL") 
# 
# modification history:
#
#   date    opr     who     reason
# -------- -----  --------  --------------------------------------
# 04/03/00 43165  MSwam     first version
# 
##########################################################################
#
# 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)}
}

    # include these modules
    #
    require 'printmsg.pl';       # prints a formatted status message
    require 'do_dbi_pkg.pl';
    require 'db_osp_processing_steps.pl'; # reads processing code from DB relation

    # exit codes
    #
    $all_is_well = 0;
    $bypass_calibration = 1;

    #
    # dataset name to query for is the lone runtime argument
    $dataset = uc($ARGV[0]);
    chomp($dataset);

    #
    # query the database for the processing code
    $code = DB_osp_processing_steps($dataset);

    #
    # if the calibration-bypass string appears anywhere in the code,
    # then skip calibration
    #
    if ($code) {
      if ($code =~ /BYPASS_CAL/) {
        PrintMsg("W","$code indicated for $dataset");
        exit $bypass_calibration;
      }
    }

    # no db entry or an entry indicating calibration can be attempted
    exit $all_is_well;
