#!/usr/bin/env perl
#----------------------------------------------------------------------------
#
# Name: check_fof
#
# This perl script verifies that the input environment variables are OK, then
# it builds the FGS and AST file specs from the environment variables. If either
# an AST or an FGS engineering file is present it renames the file so that it
# can be detected by the polling process of the external pipeline. It logs
# its findings and actions.
#
# Interactive Usage:
#	none - users can rename files without this tool
#
# History:
# Date     OPR      Who         Reason
# -------- ------ ---------- ---------------------------------------------
# 11/07/01 44742  Baum       Initial code
# 04/29/02 45738  Baum       Use external subroutines.
# 02/13/03 47570  Baum       Change exit status values
# 07/20/06 55869  Sherbert   Place subroutine prototypes before calls
#----------------------------------------------------------------------------
# set up external routines
unshift @INC,(split /:/, $ENV{PATH});
require 'printmsg.pl';       # prints a formatted status message

# subroutine prototypes
sub RenameEngFile($$);

#specify exit status values

    $PROCESS_FAILURE = 7;   # exit status for XPOLL
    $PROCESS_SUCCESS = 9;   # exit status for XPOLL
    $PROCESS_ABORT   = 102; # kill XPOLL - bad resource file

# verify pipeline mode

    $eng_root = $ENV{"OSF_DATASET"};
    if ( !defined($eng_root)) {
       PrintMsg ("F","This process only used in pipeline.");
       exit( $PROCESS_ABORT);
    } 

# get the required external variables

    $fgs_inpath     = $ENV{"FGS_INPATH"};     # directory for fgs eng files
    $ast_inpath     = $ENV{"AST_INPATH"};     # directory for ast eng files
    $fgs_ext_input  = $ENV{"FGS_EXT_INPUT"};  # input ext. for fgs eng files
    $ast_ext_input  = $ENV{"AST_EXT_INPUT"};  # input ext. for ast eng files
    $fgs_ext_output = $ENV{"FGS_EXT_OUTPUT"}; # output ext. for fgs eng files
    $ast_ext_output = $ENV{"AST_EXT_OUTPUT"}; # output ext. for ast eng files

# verify ENV variables

    if ((!defined($fgs_inpath))     || (!defined($ast_inpath))   ||
        (!defined($fgs_ext_input))  || (!defined($ast_ext_input)) ||
	(!defined($fgs_ext_output)) || (!defined($ast_ext_output)) )
    {
        PrintMsg("F","Missing ENV variable. Define all the following:");
	PrintMsg("F","FGS_INPATH,FGS_EXT_INPUT,FGS_EXT_OUTPUT and");
	PrintMsg("F","AST_INPATH,AST_EXT_INPUT,AST_EXT_OUTPUT.");
        exit( $PROCESS_ABORT);	
    }
        
# verify directories

    if (!(-d $fgs_inpath)) {
        PrintMsg("F","FGS_INPATH = $fgs_inpath is not a directory.");
	exit( $PROCESS_ABORT);
    }	
    if (!(-d $ast_inpath)) {
        PrintMsg("F","AST_INPATH = $ast_inpath is not a directory.");
	exit( $PROCESS_ABORT);
    }

    $rename_count = 0;   # initialize counter    

# rename fgs file
    	
    $fgs_in  = $fgs_inpath.$eng_root.$fgs_ext_input;
    $fgs_out = $fgs_inpath.$eng_root.$fgs_ext_output;
    RenameEngFile($fgs_in,$fgs_out);

# rename ast file
    	
    $ast_in  = $ast_inpath.$eng_root.$ast_ext_input;
    $ast_out = $ast_inpath.$eng_root.$ast_ext_output;
    RenameEngFile($ast_in,$ast_out);

# log if no files found

    if ($rename_count == 0) {
        PrintMsg("I","No FGS or AST engineering file found for $eng_root.");
    }

# end of processing    

    exit( $PROCESS_SUCCESS);     
    
#------------------------------------------------------------------------
# subroutine to check existence then rename file with error checking
sub RenameEngFile($$)
{
    my ($in_name,$out_name) = @_;
    if (-e $in_name) {
        if (rename( $in_name, $out_name)) {
	    PrintMsg("I","Renamed $in_name");
	    PrintMsg("I","-----To $out_name");
            $rename_count++;
	} else {
	    PrintMsg("E","Rename failure for $in_name");
	    exit( $PROCESS_FAILURE);     
	}
    }
}
