# +-======-+ 
#  Copyright (c) 2003-2007 United States Government as represented by 
#  the Admistrator of the National Aeronautics and Space Administration.  
#  All Rights Reserved.
#  
#  THIS OPEN  SOURCE  AGREEMENT  ("AGREEMENT") DEFINES  THE  RIGHTS  OF USE,
#  REPRODUCTION,  DISTRIBUTION,  MODIFICATION AND REDISTRIBUTION OF CERTAIN 
#  COMPUTER SOFTWARE ORIGINALLY RELEASED BY THE UNITED STATES GOVERNMENT AS 
#  REPRESENTED BY THE GOVERNMENT AGENCY LISTED BELOW ("GOVERNMENT AGENCY").  
#  THE UNITED STATES GOVERNMENT, AS REPRESENTED BY GOVERNMENT AGENCY, IS AN 
#  INTENDED  THIRD-PARTY  BENEFICIARY  OF  ALL  SUBSEQUENT DISTRIBUTIONS OR 
#  REDISTRIBUTIONS  OF THE  SUBJECT  SOFTWARE.  ANYONE WHO USES, REPRODUCES, 
#  DISTRIBUTES, MODIFIES  OR REDISTRIBUTES THE SUBJECT SOFTWARE, AS DEFINED 
#  HEREIN, OR ANY PART THEREOF,  IS,  BY THAT ACTION, ACCEPTING IN FULL THE 
#  RESPONSIBILITIES AND OBLIGATIONS CONTAINED IN THIS AGREEMENT.
#  
#  Government Agency: National Aeronautics and Space Administration
#  Government Agency Original Software Designation: GSC-15354-1
#  Government Agency Original Software Title:  GEOS-5 GCM Modeling Software
#  User Registration Requested.  Please Visit http://opensource.gsfc.nasa.gov
#  Government Agency Point of Contact for Original Software:  
#  			Dale Hithon, SRA Assistant, (301) 286-2691
#  
# +-======-+ 
package GMAO_utils;
#########################################################################
#
# !Revision History
#
# 28Jul2006  Stassi    Package added to CVS repository.
# 09Aug2006  Stassi    Added option to suppress zeit output in System()
#
#########################################################################
use strict;
require Exporter;
our @ISA = "Exporter";
our @EXPORT_OK = qw( Assignfn System );

#-----------------------------------------------------------------------
#  name - Assignfn
#  purpose - Assigns fn to given file name fname.
#
#  Notes:
#    fname = old file
#       fn = new file (links to old)
#-----------------------------------------------------------------------
sub Assignfn {
    my ( $fname, $fn ) = @_;
    unlink($fn) if ( -e $fn ) ;
    symlink("$fname","$fn");
}

#-----------------------------------------------------------------------
#  name - System
#  purpose -
#
#-----------------------------------------------------------------------
sub System {
    use FindBin qw($Script);
    my ($rc,$rc1,$rc2,$exit_code);
    my ($error_code);
    my ($trace,$fvwork);
    my @zname;

    # calling parameters
    #-------------------
    my ( $cmd, $logfile, $xname, $quietzeit ) = @_;

    # set values
    #-----------
    $error_code = 6;
    $trace = $Script . "->GMAO_utils::System()";
    $fvwork = $ENV{"FVWORK"};
    unless ($fvwork) {
	warn "${trace}: >>> ERROR <<< \$FVWORK must be defined "
	    . "before calling this subroutine.\n";
	return $error_code;
    }

    # save STDOUT and STDERR before redirecting
    #------------------------------------------
    open SAVEOUT, ">&STDOUT";  # save stdout
    open SAVEERR, ">&STDERR";  # save stderr

    # DUMMY print statements to suppress WARNING messages
    #----------------------------------------------------
    print SAVEOUT "";
    print SAVEERR "";

    # redirect STDOUT and STDERR to $logfile
    #---------------------------------------
    unless (open STDOUT, ">>$logfile") {
        warn "${trace}: >>> ERROR <<< while redirecting STDOUT\n";
	return $error_code;
    }
    unless (open STDERR, ">>$logfile") {
        warn "${trace}: >>> ERROR <<< while redirecting STDERR\n";
	return $error_code;
    }

    select STDERR; $| = 1;     # make it unbuffered
    select STDOUT; $| = 1;     # make it unbuffered

    @zname = split(" ", $cmd);
    unless ($quietzeit) {
	if ( $xname ) {
	    $rc1 = system( "zeit_ci.x -r $fvwork/.zeit $xname");
	} else {
	    $rc1 = system( "zeit_ci.x -r $fvwork/.zeit $zname[0]");
	}
    }

    $rc = system ( $cmd );     # run the shell command

    unless ($quietzeit) {
	if ( $xname ) {
	    $rc2 = system( "zeit_co.x -r $fvwork/.zeit $xname");
	} else {
	    $rc2 = system( "zeit_co.x -r $fvwork/.zeit $zname[0]");
	}
    }

    # Bitwise shift returns actual UNIX return code
    #----------------------------------------------
    $exit_code = $rc >> 8;

    close STDOUT;
    close STDERR;

    open STDOUT, ">&SAVEOUT" ;  # restore stdout
    open STDERR, ">&SAVEERR" ;  # restore stderr

    return $exit_code;
}

1;
