#!/usr/bin/env perl
#----------------------------------------------------------------------------
#
# Name: ingest_file_poll.pl
#
# This perl script creates the INGEST group OSFs for processing
# datasets without interpipeline communications. It is not recommended to use
# this interface unless catalog fields do NOT need to be updated. The DADS
# daily log datasets (archive class DLG) are one appropriate use of this
# interface. It is designed to handle multiple missions using the trigger
# subdirctory name and the resource variables named SET_<mission>.
#
# Interactive Usage:
#	None
#
#
# History:
# Date     OPR      Who       Reason
# -------- -------- --------- ---------------------------------------------
# 03/17/04 50521    J.Baum    Initial version
# 01/06/05 53769    MSwam     fix bug in parsing for double slash filespec
#----------------------------------------------------------------------------
# set up external routines
unshift @INC,(split /:/, $ENV{PATH});
require 'printmsg.pl';       # prints a formatted status message

    #specify exit status values and othe constants
    $FILE_SUCCESS     = 11; # exit status for XPOLL
    $FILE_FAILURE     = 13; # exit status for XPOLL

    #check for all required ENV variables
    $event          = $ENV{"EVENT_NAME"};
    $path           = $ENV{"PATH_FILE"};
    $set_processing = $ENV{"SET_PROCESSING"};
    $set_error      = $ENV{"SET_ERROR"};

    if (!defined($event) || !defined($path) || !defined($set_processing) ||
        !defined($set_error)) 
    {
       PrintMsg("E", "Missing ENV variable from XPOLL --");
       PrintMsg("E", "Must have EVENT_NAME, PATH_FILE, SET_PROCESSING ".
          "and SET_ERROR defined.");
       exit( $FILE_FAILURE);
    }
    # remove directory from path name
    $file_offset = rindex( $path, "/") + 1;
    if ($file_offset > 0) {
       $path = substr($path, $file_offset);
    } 
    # replace double slashes with singles
    $event =~s/\/\//\//g;

    # sign on
    PrintMsg("I","Processing file event: $event");

    # determine dataset name request by examining event extension
    $dot_offset = rindex( $event, ".");
    $slash_offset = rindex( $event, "/");
    
    if ($dot_offset < 0 || $slash_offset < 0 || $dot_offset < $slash_offset) {
       PrintMsg("E","Invalid event name. Expecting ".
          "/<trigger_path>/<mission>/<dataset>.<archclass>_proc" );
       exit( $FILE_FAILURE);
    } 
    $dataset_start  = $slash_offset + 1;
    $dataset_length = $dot_offset - $dataset_start;
    $dataset        = substr($event, $dataset_start, $dataset_length);

    # make first char of data_id uppercase and the rest lowercase
    $first_class_char = $dot_offset + 1;
    $second_class_char = $dot_offset + 2;
    $data_id = uc( substr($event, $first_class_char, 1)).
               lc( substr($event, $second_class_char, 2));
    $cmd = "osf_create -p $path -f $dataset -t $data_id  -s $set_processing";
    
    #submit command to create pipeline OSF in processing status
    if (system("$cmd")) {
       PrintMsg("E","Failed to execute command: $cmd");
       exit ( FILE_FAILURE);
    } else {
       PrintMsg("I","Created OSF for dataset $dataset $data_id");
    }
    # get mission from last subdirectory of path
    $next_slash_offset = rindex( $event, "/", $slash_offset-1);
    $mission_start = $next_slash_offset + 1;
    $mission_length = $slash_offset - $mission_start;
    $mission        = uc(substr($event, $mission_start, $mission_length));
    
    $set_mission = $ENV{"SET_".$mission};  # mission completion status
    
    if (!defined($set_mission)) {
       PrintMsg("E","Cannot get ENV variable for SET_".$mission);
       $cmd = "osf_update -p $path -f $dataset -t $data_id  -s $set_error";
    
       #submit command to update pipeline OSF in error status
       if (system("$cmd")) {
          PrintMsg("E","Failed to execute command: $cmd");
       } else {
          PrintMsg("I","Updated OSF to error status for dataset ".
             "$dataset $data_id");
       }
       exit ( $FILE_FAILURE);
    }
    $cmd = "osf_update -p $path -f $dataset -t $data_id  -s $set_mission";
    
    #submit command to update pipeline OSF in mission completion status
    if (system("$cmd")) {
       PrintMsg("E","Failed to execute command: $cmd");
    } else {
       PrintMsg("I","Updated OSF to completion status for $mission dataset ".
             "$dataset $data_id");
    }
    exit( $FILE_SUCCESS);
