##########################################################################
#
# routine: otfr_setup.pl
#
# purpose: Performs set-up common to many OTFR Perl pipeline tasks.
#          - Prints a banner and date to stdout.
#          - Sets exit status constants.
#          - Splits the triggering OSF_DATASET value into a directory
#             and dataset name.
#          - Verifies that the request processing directory exists and
#             moves there.
#          - Opens the trailer file for append access
# 
# Input:   ENV.OSF_DATASET - name of OTFR request being processed
#          ENV.OUTPATH - base directory for processing OTFR requests
#
# Globals: go_absent - exit status to signal FATAL XPOLL error
#          quit_this_dataset - exit status to cause this request to fail
#          all_is_well - exit status for normal completion
#          p_dir - the full name of the OTFR request (timestamp_exposure)
#          p_dset - the exposure part of the OTFR request name
#          p_requestid - the unique pseudo timestamp portion of the OTFR request name
#          TRL - file handle for trailer file
#
#          Current working directory is set to the directory where this
#             request is being processed.
#
# Returns: none
#
# Errors:  Will exit with go_absent if unable to locate the request directory
#
# modification history:
#
#   date    opr     who     reason
# -------- -----  --------  --------------------------------------
# 09/03/00 43165  MSwam     first version
# 
##########################################################################
sub OTFR_setup
{

print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n";
`/bin/date`;

# set exit status codes
#
$go_absent=102;          #this exit status is in the FATAL range for XPOLL
$quit_this_dataset=1;    #this exit status is mapped and will allow
                         #  the process to try another dataset
$all_is_well=0;

require 'resolve.pl';    # resolves filespec in an env var

# parse dataset,timestamp from OSF field
#
$p_dir          = $ENV{"OSF_DATASET"};
($p_requestid,$p_dset) = split("_",$p_dir);

$caldir = Resolve("OUTPATH");

# verify directories and move there
if (! -e $caldir || ! chdir($caldir) ) {
   PrintMsg("E","ERROR: Failed to set directory to $caldir\n");
   exit $go_absent;
}
if (! -e $p_dir || ! chdir($p_dir) ) {
   PrintMsg("E","ERROR: Failed to set directory to $p_dir\n");
   exit $go_absent;
}
$localdir=`pwd`;
chomp($localdir);

# open the trailer file and save the filehandle
if (! open TRL, ">>$p_dir.trl") {
   PrintMsg("E","ERROR: Failed to open trailer $p_dir.trl");
}
$trl = *TRL;

}
1;
