# +-======-+ 
#  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 Extract_config;
#
# 20020114 TOwens added FVHOME to Run_Config search path
#

require Exporter;
require 5.000;
@ISA = qw(Exporter);
@EXPORT = qw(extract_config);

sub extract_config {

# This module contains the dirname() subroutine.

use File::Basename;

# If not specified, 4th argument is the same directory from which the calling
# script is invoked.

   if ( $#_ < 3 ) { $_[3] = dirname( $0 ); }

# If not specified, 5th argument is the default name of the run_config_file,
# "Run_Config".

   if ( $#_ < 4 ) { $_[4] = "Run_Config"; }

   my $requested_param = $_[0];
   my $run_config_file = $_[1];
   my $default_value   = $_[2];
   my $bin_dir         = $_[3];
   my $run_config_base = $_[4];

# If run_config_file given (i.e., not "DEFAULT"), use that if it
# exists.  Otherwise, find another one.

   if ( $run_config_file ne "DEFAULT" ) {

      if ( !( -e $run_config_file ) ) {
         print STDERR "(extract_config) Run_Config file '", $run_config_file, "' does not exist.\n";
         return( $default_value );
      }

      $run_config_file_use = $run_config_file;

   } else {

# See if a config file exists in $FVHOME/run if set otherwise check $HOME
      if ($ENV{'FVHOME'}){
          $run_config_file = join( '', $ENV{'FVHOME'}, "/", "run", "/", $run_config_base );
     } else {
        $run_config_file = join( '', $ENV{'HOME'}, "/", $run_config_base );
     }

     if ( -e $run_config_file ) {
        $run_config_file_use = $run_config_file;
      
# See if a config file exists in user's home directory in case $FVHOME/run came up dry.
     } else {
        $run_config_file = join( '', $ENV{'HOME'}, "/", $run_config_base );

        if ( -e $run_config_file ) {
           $run_config_file_use = $run_config_file;

# See if a config file exists in bin directory that this script resides in.

        } else {
           $run_config_file = join( '', $bin_dir, "/", $run_config_base );

           if ( -e $run_config_file ) {
              $run_config_file_use = $run_config_file;
# No config file found; the default value will be used for the parameter.

           } else {
              $run_config_file_use = "DEFAULT";
           }
        }
     } 
   }

   if ( $run_config_file_use eq "DEFAULT" ) {

      $param_value = $default_value;

   } else {

      unless ( open( CFILE, $run_config_file_use ) ) {
         print STDERR "(extract_config) Run_Config file '", $run_config_file_use, "' can not be opened.\n";
         return( $default_value );
      }

      $param_value = "";
      while ( <CFILE> ) {
         chop;
         if ( m/$requested_param/ ) { $param_value = ( split( /=/ ) )[1]; }
      }

      close( CFILE );

   }

   if ( not $param_value ) { 
         print STDERR "(extract_config) Did not find parameter '", $requested_param, "' in Run_Config file '", $run_config_file_use, "'\n";
         $param_value = $default_value;
   }

# Strip any leading or trailing blanks from the value.

   $param_value =~ s/^ *(.*) *$/$1/;

   return( $param_value );

}

1;
