#!/usr/bin/perl -w
package XXXXbuildenv;
{
# XXXXbuild-env.pl  wjs  Jun 04
# XXXX = "opt-" for option server setup; presumably empty else
#	(from opt-build-env November 13, 1998  C.Hammond
#	 "For use in setting a compilation environment when building
#	 an OTHER OPTIONS server" as modified by rcg 29 Dec 99
#	 Base csh source file was in use 1 Jun 04)

#	General comment: this routine is a string generator.  It does NOT
#    check for the validity of strings, existence of files when string
#    points to a file, etc.  I think this is a good architectural idea
#    because I don't think all strings can be validated, and doing some
#    but not others would be confusing
#
#	Accepts -sh or -csh switch.  If present, outputs a set of export or
#    setenv commands, respectively, to stdout.  Values for env vars are
#    output between a single set of "s, so complicated ones might produce
#    bad setenv/export statements.
#	Accepts -defnlist to control XXXXBUILD-ENV-DEFNS (see below)
#	Accepts an absolute directory spec as an argument.  If present, that 
#    directory becomes the jgofs root instead of the directory in which
#    opt-build-env is found.
#	If -defnlist specified, outputs XXXXBUILD-ENV-DEFNS, an 
#    environment variable = to a comma separated list of environment
#    variables set by XXXXbuild-env.pl.
#	Defines perl hash old_ENV for every env var that XXXXbuild-env.pl 
#    replaced.  Key is the replaced env var; value is the replaced value.
#    old-ENV{key} is undefined if there was no old value for key.

#	Sample of environment variables output - documentation is in code where
#	user modifies the site-specific value.  (Note: sample not
#	necessarily complete nor does it necessarily reflect the defaults
#	nor does it necessarily reflect the values in the body of the code.
#	It is a sample showing the kinds of things that can be done)
#	    Some variables (eg, TARPROG) are output only for option servers
#	    Some variables (eg, SHELL) are output on an OS-specific basis.
#	  AR		/usr/ccs/bin/ar srv
#	  BACKGROUND_COLOR	00FF00
#	  BUTTONIMAGESDIR	http://gb11.whoi.edu:8200/images
#	  CC		cc -DSOL -DSVR4 -lnsl -lsocket
#	  CONFIG_FILE	/export/home/wsass/opt-build-env.pl
#	  CPPROG	/usr/bin/cp
#	  DIRSERVER	gb11.whoi.edu:8200/jg/dir
#	  DISPLAY_DATA_URL	FALSE
#	  DMONAME	Option Server
#	  FC		f77 -lnsl -lsocket
#	  GNUCOMPRESS	/usr/local/bin/gzip
#	  HELPADDR	http://gb11.whoi.edu:8200
#	  HELPDIR	htdocs
#	  INFOSERVER	gb11.whoi.edu:8200/jg/info
#	  INFO_SOURCES	BCO-DMO,TRADITIONAL
#	  JGOFSDIR	/export/home/wsass
#	  JGSCRIPTDIR	/jg
#   LOCAL_SERVER_ROOTS dmoservp1_OOserver.whoi.edu|/data302/data_server/dmoservp1
#	  MAIL_SENDER	/usr/bin/rmail
#	  MYADDR	gb11.whoi.edu:8200
#	  NAWK		nawk
#	  OBJDIR	/
#	  OPT		opt
#	  OPTIONSERVER	optserv1.whoi.edu/jg/otheropt3
#	  PERL		/usr/bin/perl
#	  PORT		8200
#	  RANLIB	echo
#   SETUP_OOSERVER_BUILD_ENVIRONMENT 1
#	  SYS		Solaris
#	  TARPROG	/usr/bin/tar
#	  TEMPADDR	http://gb11.whoi.edu:8200/jgofsopt/8200
#	  TEMPDIR	/export/home/wsass/htdocs/jgofsopt/8200
#	  UNIXCOMPRESS	/usr/bin/compress
#	  ZIPPROG	/usr/local/bin/zip

  $build_env_pl_version = "build-env.pl version 1.5a  17 Mar 2016";
#  17 Mar 16.  wjs
# Get some matlab defns from build-env.conf.  Should be all, but it's a mess,
# so just take those we want to use "this time".  Amazingly enough, trying to get the
# defns from build-env.conf means yet more logic change to matlab defns
#		[begin v 1.5a]

#  18 Jan 15.  wjs
# Comment changes
#  15 Jan 15.  wjs
# Bug fix: The default setting for 3 optionserver-specifying parameters needs to be done
#   BEFORE getting possible user setting from the .conf file
# Actually implement v 1.4c bug fix.  diff between 1.4c & 1.4b just shows the comments about
#   what is to be done!  (Possible bad source control - there might be a "good" 1.4c in the
#   place where problem originated)
#  11 Jan 15.  wjs
# LOCAL_SERVER_ROOTS
#     A map of JGOFS data server names to server root directories.  Allows servers to access local
#     objects locally rather than via http
# SETUP_OOSERVER_BUILD_ENVIRONMENT
#     Allows a data server to have build-env.pl put some OOserver vars into the environment
#     Present use is to allow a data server to build "OOserver" programs like makenetcdf
#		[begin v 1.5]

#   8 Nov 14.  wjs
# Bug fix: OPTIONSERVER needs to be an option server, not gb11.whoi.edu
#	Problem probably began w/ gb6/gb11 OOserver hosting duality (v 1.4 et seq).
#		[begin v 1.4c]
#  28 Aug 14.  wjs
#	MVPROG
#		[begin v 1.4b]
#  20 Dec 12.  wjs
#	How about actually looking to see if there's a .conf file (v 1.4)
#	  instead of just looking to see if the symbol defining the name exists?
#	Newer perl objects to  defined (%hash)  as obsolete; suggests 
#	  just getting rid of  defined  ; following suggestion
#  15 May 12.  wjs
#	Don't require hash_from_file stuff if there's no .conf file
#	Give up on building matlab/netcdf env vars from only the portion
#	  that we expect to change (root location, system architecture)
#	  in favor of having the user supply the whole "right thing"
#	  matlab seems to just change too much
#	Define non-existent "things" (eg, compress utility on gb11) as
#	  empty strings.  Let software that uses them deal w/that (said
#	  software would already have to deal w/env vars that disappear
#	  (eg, UNIXCOMPRESS), so no added work.
#  25 Jan 12.  wjs
#	Default env var values suitable for gb11 optserver
#	Remove -O switch from linux fortran compilation command.  Not
#	  only don't we want optimization, specifying -O produces
#	  diagnostics about conflict w/new, unimplemented intrinsic
#	  functions like  scale .  Eventually we will presumably need to 
#	  declare our function names  external  to avoid this conflict
#		[begin v 1.4]

#  23 Oct 11.  wjs
#	Check $server_name for X.Y.Z format
#	Do a bit more checking on $port, $doc_root & $jgofs_cgi_root
#		[begin v 1.3b]
#   9 Aug 10.  wjs
#	More info controls (info 4.1)
#		[begin v 1.3a]
#  11 Mar 10.  wjs
#	Forgot to accept dmoname from .conf file
#	Need to accept temp_dir from .conf file
#   5 Mar 10.  wjs
#	Forgot to accept button_images_server from .conf file
#  27 Feb 10.  wjs
#	More info controls
#  20 Feb 10.  wjs
#	Accept values from a .conf file.  If present there,
#	  overrides value (if any) here
#	Bug fix: no FC env var defined for irix systems.  Typo.
#	Add a version.  DON'T call it $version until/unless ready
#	  to deal w/scoping issues
#   5 Feb 10.  wjs
#	By default, don't define DMONAME
#	Output INFO_USEDATABASE w/same value as USEDATABASE
#	info 4.0 control variables.  Supersedes but accepts USEDATABASE
#	  and INFO_USEDATABASE.
#	Comment to advise against changing jg defn
#		[begin v 1.3]
#
#  11 Jun 09.  wjs
#	Change otheropt2 to otheropt3
#  16 Apr 09.  wjs
#	Add USEDATABASE per rcg request
#  14 Mar 09.  wjs
#	Add compile-time LINUX defn
#  14 Feb 09.  wjs
#	Change otheropt to otheropt2
#	Get rid of "not setting up standard system OOserver" warning
#		[Earlier comments moved to build-env.revision Feb 10]

  foreach (@ARGV) {
    if ($_ eq "-sh") {
      ((defined $sh_out) || (defined $csh_out)) && 
			die ("Duplicate or conflicting -sh, -csh switches\n");
      $sh_out = 1;
    } elsif ($_ eq "-csh") {
      ((defined $sh_out) || (defined $csh_out)) && 
			die ("Duplicate or conflicting -sh, -csh switches\n");
      $csh_out = 1;
    } elsif ($_ eq "-defnlist") {
      $defnlist = "";
    } else {
      (defined $jgofs_root) && die "Too many switches/arguments\n";
      $jgofs_root = $_;
    }
  }

  (defined $sh_out) || ($sh_out = 0);
  (defined $csh_out) || ($csh_out = 0);
  if (defined $jgofs_root) {
    ($jgofs_root =~ /^\//) ||
      die "jgofs root (specified as $jgofs_root) must be an absolute file spec\n";
    (-e $jgofs_root) || die "jgofs root $jgofs_root does not exist\n";
    (-r $jgofs_root) || die "jgofs root $jgofs_root is not readable\n";
    (-d $jgofs_root) || die "jgofs root $jgofs_root is not a directory file\n";
  }

  $this_file = ($main::build_env_file_name) ? $main::build_env_file_name : $0;

#     Use cd command to get directory.  Don't know how to do it easily otherwise
#     (there is a shell env var PWD, but this can be set != to actual pwd)
  ($dir,$file) = ($this_file =~ /^(.*)\/(.+)$/);
  if ($file) {
#     Presumably $dir must be defined if $file is, so testing $dir is
#     asking whether or not it's empty
    $this_dir = ($dir) ? `(cd $dir; pwd)` : "";
  } else {
#     Presumably $dir is not defined, or we'd have a non-empty dir spec w/an
#     empty file name.  Don't think this is worth a diagnostic
    $this_dir = `pwd`;
    $file = $this_file;
  }
  chomp $this_dir;
  &out("CONFIG_FILE","$this_dir/$file");

  (defined $jgofs_root) || ($jgofs_root = $this_dir);

###
# vvvvvvvv     most only need to edit between long comment lines      vvvvvvvvvv
#######################################################################

#   Site-specific configuration configuration.  See build-env_conf.doc
#     Set next value to "" or remove line if capability not desired
$build_env_conf_file = "$jgofs_root/build-env.conf";
#     Destination file(s) for diagnostics regarding the configuration file
$build_env_conf_diagnostic_sink = "STDERR";

#   Operating system.  If not "Solaris", "Irix", or "linux"
#   editing "outside the lines" will be required
$opsys = "linux";

#   Location of perl.  It is recommended that this NOT be used
#   Some perl scripts are released as "template" files which are input
#     to make files which substitute values for placeholders in the template.
#     Except for the location of perl itself, these placeholders all come
#     from this file.  Therefore, we can get rid of both the templates and
#     their make files by
#	1) hardcoding the perl location (we recommend /usr/bin/perl and,
#	   if perl is NOT in /usr/bin, setting up a soft link defining
#	   /usr/bin/perl; eg,  ln -s wherever_perl_is /usr/bin/perl)
#	2) "require"ing this file
#	3) using $ENV{"whatever"} instead of the placeholder
$perl_location = "/usr/bin/perl";
########

#   Next group must be defined and non-empty
#   $jgofs_cgi_root is relative to the directory build-env.pl is in,
#     and must be defined in httpd server config for $server_name:$port
#   Note: much of the system does NOT honor $jgofs_cgi_root, so change
#     at own peril
$server_name = "gb11.whoi.edu";
$port = 8200;
$jgofs_cgi_root = "jg";

#   Pieces of URL pointing to directory where images for outer's
#   display buttons are located.  Empty $button_images_server means
#   $server_name.  Empty $button_images_dir means default doc root for
#   $button_images_server.  (Note that empty $button_images_port means
#   default port for $button_images_server, not $port)
#   If no images, comment out next 3 lines
$button_images_server = "globec.whoi.edu";
$button_images_port = "";
$button_images_dir = "images";

#	Next 2 values control appearance of web pages displayed by
#	outer.  If your methods do not use outer, these values have
#	no effect.
#   Controls whether outer displays the object spec as a title line 
#   in html display.  May be omitted.  Default value is TRUE
$display_data_url = TRUE;
#   Controls background color of pages displayed by outer.  May be
#   omitted.  Default value depends on browser displaying page.
#   If specified, specify as a string constant exactly 6 characters in
#   length, each character being a valid hex digit.  Case insensitive.
$background_color = "ffffff";

#
#   Name of server (appears on web pages someplace...)
#   If commented out or otherwise undefined, defaults to $server_name
#   A defined, empty name is honored.  Will override the default server
#   name, if any, implied by the value of program (below)
#		$dmoname = "Option Server";

#   The next variable controls the sources of metadata.  It assumes
#   that version 4.0 of the info utility is installed.  The variable's value
#   is a comma-separated list of sources.  The sources are searched
#   in the order specified.
#     If TRADITIONAL is in the list,
#   the metadata information comes from a plain text, with HTML tags 
#   allowed, file called <data object name>.info located in the same 
#   subdirectory in the JGOFS/GLOBEC objects subdirectory where the 
#   data object is specified as an entry in the .objects file.  
#     If BCO-DMO is in the list then the metadata information is read
#   from the BCO-DMO metadata database via a web CGI script.
#     If REMOTE_TRADITIONAL is in the list, the metadata comes from a
#   remote host's info server.  That server is expected to be configured
#   as TRADITIONAL (above).  Specification of the remote server is controlled
#   by the REMOTE_TRADITIONAL_SERVERS configuration parameter (below)
#     If the list is empty or not specified, TRADITIONAL is assumed.
#
#		$info_sources = "BCO-DMO,TRADITIONAL";
#
#   Some earlier versions of the info utility were controlled by
#   the build-env.pl  $usedatabase  variable.
#   This variable is still accepted.  If defined as true, it is a synonym for
#	$info_sources = "BCO-DMO,TRADITIONAL";
#   If false, it is a synonym for
#	$info_sources = "TRADITIONAL";
#   The next set of variables can be used to control the operation of
#   the metadata display facility.  For information about these variables
#   please see the  info.doc  file included with info version 4.0
#	$info_log_misses = "/tmp/info40-misses.log";
#	$info_log_all_sources = "/tmp/info40-logall.log";
#	$info_action_on_data_source_error = "ABORT";
#	$info_action_on_logging_failure = "ABORT";
#   The next variable can be used to specify the host and port
#   for the BCO-DMO metadata web CGI script
#	$info_BCODMO_host_port
#   The next variable can be used to specify how the BCO-DMO metadata web
#   web CGI script is called.  The choices are 
#   DATASET_URL, MANUFACTURED_DATASET_URL, and DDIR_OBJ.
#   The default is
#	$info_BCODMO_access_techniques =
#			"DATASET_URL,MANUFACTURED_DATASET_URL,DDIR_OBJ";
#   The next variable can be used to force all the named techniques to be
#   attempted even if use of a technique results in returned data.  The
#   default is
#	$info_BCODMO_attempt_all_techniques = "FALSE";
#   The next variable can be used to specify where to look if
#   INFO_SOURCES includes the keyword REMOTE_TRADITIONAL
#   The choices are DATASERVER, INFOSERVER and GLOBEC.  The default is
#	$info_remote_traditional_servers = "INFOSERVER";
#   The next variable can be used to force all the named remote traditional
#   servers to be contacted even if one of them already returned data.  The
#   default is
#	$info_attempt_all_remote_traditional_servers = "FALSE";


#	NEXT 2 VARIABLES ONLY NEEDED FOR DATA SERVER SETUP
#   If you are a node in the JGOFS or GLOBEC programs, setting
#   program to JGOFS or GLOBEC, respectively, will set your documentation
#   and directory servers to the programs' master servers, and will
#   set the default name for your data server.  If blank or undefined,
#   servers will come from your node.
#		$program = "";

# Allow otherwise-ooserver-only programs to be built in a data server if user wants them
# As of build-env.pl v 1.5, this applies only to the matlab and netcdf converters.
# To enable this capability, set the value of the next parameter to 1.  It may also be
# necessary to alter the various netcdf and matlab definitions in the "ONLY ... OPTION
# SERVER SETUP" section below, esp if the data server using this feature is not located
# on a box that already hosts an ooserver.
$setup_ooserver_build_environment = 0;


#	REST OF VARIABLES ONLY NEEDED FOR OPTION SERVER SETUP
#   Directory of DocumentRoot defined for $server_name in
#   server setup, specified as a relative directory (no initial /)
#   This directory must be an offset from the JGOFS software installation
#   directory (where this file lives).  Use links, if necessary, to
#   coordinate JGOFS installation tree and web server tree
$doc_root = "htdocs";

#   directory relative to $doc_root where help is located
$help_dir = "";

#   directory relative to $doc_root under which temp files are stored
#   Some level of uniqueness is nice, but we certainly don't do 
#   a thorough job of it with $port.  (We don't even do a
#   thorough job of it when we add the browser's ip addr as
#   another subdirectory level)
#   (NB: this is NOT /jgofsopt just because we sometimes alias 
#   things there - alias is logically irrelevant to configuration
#	The OOserver code uses jgofsopt in 2 different ways.  One way
#   is as a spec for temp files (set up here).  When used this way, the file
#   spec is arbitrary (except for accessibility issues).  The other way is 
#   as a web-server-accessibile "entity".  When used this way, jgofsopt 
#   is "the thing we define in the web server setup".  We happened to 
#   choose the same name for both uses (and, on optserv1, threw a file
#   system symbolic link into the stew for good measure)
#	Among other things, the web "entity" must be a subdir of the 
#   doc root (enforced here, creating an understood association between
#   what's done here and what's done in the web server config))
#	NB: $port, below does NOT reflect the value, if any, in build-env.conf
#   If you change the port in build-env.conf, and you want temp_dir to
#   have that changed port, you must specify temp_dir in build-env.conf as well
$temp_dir = "jgofsopt/$port";

$netcdfincdir = "/usr/local/netcdf/include";
$netcdflibdir = "/usr/local/netcdf/lib";

#   Latest matlab foolery.
#   At the moment (Feb 12), the matlab downloader uses only the "plain"
#   matlab definitions (last of bunch below; "system default").  
#   The matlab-within-download may use all the 
#   definitions, but will hopefully confine offerings only to those
#   versions that work
#	incdir - matlab .h files
#	libdir - matlab libraries
#	syslibdir - libstdc++ library required by matlab713
$matlab5incdir = "/usr/local/matlab5.3.1/extern/include";
$matlab5libdir = "/usr/local/matlab5.3.1/extern/lib/lnx86"; 
$matlab5syslibdir = "";
$matlab65incdir = "/usr/local/matlab6.5.1/extern/include";
$matlab65libdir = "/usr/local/matlab6.5.1/extern/lib/glnx86";
$matlab65syslibdir = "";
$matlab713incdir = "/usr/local/matlab7.13/extern/include";
$matlab713libdir = "/usr/local/matlab7.13/bin/glnxa64";
$matlab713syslibdir = "/usr/local/matlab7.13/sys/os/glnxa64";
#   List below ordered by preference.  In particular, first choice is
#   the "system default".  Shouldn't matter if versions 
#   on list are not defined above ... unless you want to use those versions!
#     All of the defns in this section, above & below, can be overridden
#   in a build-env.conf file (but only if this code has a use_build_env_conf_val
#   call for each variable, and it doesn't.  It has SOME ... check this code or doc)
$matlab_version_list = "matlab713,matlab65,matlab5";

#     If LOCAL_SERVER_ROOTS is defined by build.pl, build-opt-env.pl will make a local object defn
#   out of a remote object defn (when possible, and according to the LOCAL_SERVER_ROOTS mapping)
#   and put the local defn in the "primal" OBJECT env var.  Presumably all the OOserver programs 
#   that use build-opt-env.pl (again, presumably all the OOserver programs) use the translation of
#   the OBJECT env var as the object name for the data to be processed.
#     Format of this parameter is a comma-separated list, where each element of the list is an A|B
#   pair, where A is the name of the local data server and B is the JGOFS root of that data server.
#   The "name of the local data server" is what ends up in the PATH_INFO for the URL that points to the OOserver.
#   This is made by outer, and presumably is some function of dns names, httpd config parameters, and
#   JGOFS parameter names used in the data server build.... as well as somebody just typing in a string
#   Resurrecting Gertrude Stein may be of use.
#     The name of the local data server is optionally preceded by 2 slashes.  It may include a
#   trailing :port_number.  The JGOFS root is an absolute directory spec.  The leading slash is optional
$local_server_roots = "";

#   auxiliary programs required for various download functions
(-x ($mail_sender = "/usr/bin/rmail")) || ($mail_sender = "");
(-x ($zip = "/usr/bin/zip")) || ($zip = "");
(-x ($tar = "/bin/tar")) || ($tar = "");
(-x ($compress = "/usr/bin/compress")) || ($compress = "");
(-x ($gzip = "/usr/bin/gzip")) || ($gzip = "");
(-x ($mv = "/bin/mv")) || ($mv = "");

#   life is much easier if we have cp ...
(-x ($cp = "/bin/cp")) || ($cp = "");
#######################################################################
# ^^^^^^^^     most only need to edit between long comment lines     ^^^^^^^^^^^
###

if (($build_env_conf_file) && (-e $build_env_conf_file)) {
#   Some day we may want to get next routine from a standard "library 
#   place".  Issue is that if/when we do that, we make the build package
#   depend on something else.  At the moment it's stand alone (and we
#   distribute the next file w/the build package)
#     get_hash_from_file is now a perl module, so we are one step closer
#   to standard.  Regarding the location issue, above, we could find
#   things w/whatever perl search list mechanism is defined
#     Well, turns out we actually NEED that "standard library place"
#   use get_hash_from_file;  the intended syntax, looks in the @INC
#   array for the directory.  The @INC array has . in it, but we might
#   not be running in .  Attempts to alter @INC have so far resulted in
#   humiliating failure.  So, for now, something that works
########  push @INC,$jgofs_root;
  require "$jgofs_root/get_hash_from_file.pm";
  ($status,$msg,%build_env_conf) = 
      &get_hash_from_file::get_hash_from_file
		($build_env_conf_file,$build_env_conf_diagnostic_sink);
  ($status eq "OK") || 
	(die ("Bad status $status from get_hash_from_file.  Err text = $msg"));
}

###
#	OPERATING SYSTEM STUFF
###

&use_build_env_conf_val("opsys");
$opsys || die "Undefined or blank \$opsys (operating system) variable";

if ( $opsys eq 'linux' ) {
  $c_compile = "cc -DLINUX";
  $fortran_compile = "g77";
  $archive_command = "/usr/bin/ar rv";
  $ranlib = "ranlib";
  $nawk = "awk";
#		      Stop supporting SunOS.  Leave this here to explain
#		      code conditional on SYS=SunOS (for example)
#		} elsif ( $ENV{'SYS'} eq 'SunOS' ) {
#		  $ENV{'CC'} = "cc -DSUN";
#		  $ENV{'FC'} = "f77";
#		  $ENV{'AR'} = "ar rv";
#		  $ENV{'RANLIB'} = "ranlib";
#		  $ENV{'NAWK'} = "nawk";
} elsif ( $opsys eq 'Solaris' ) {
  $c_compile = "cc -DSOL -DSVR4 -lnsl -lsocket";
  $fortran_compile = "f77 -lnsl -lsocket";
  $archive_command = "/usr/ccs/bin/ar srv";
  $ranlib = "echo";
  $nawk = "nawk";
} elsif ( $opsys eq 'Irix' ) {
  $c_compile = "cc -cckr -DIRIX";
  $fortran_compile = "f77 -woff 2290";
  $archive_command = "/usr/bin/ar -srv";
  $ranlib = "echo";
  $nawk = "nawk";
  $sh = "/sbin/sh";
} else {
  die "Unrecognized value for \$opsys (operating system) variable: $opsys";
}

&use_build_env_conf_val("c_compile");
&use_build_env_conf_val("fortran_compile");
&use_build_env_conf_val("archive_command");
&use_build_env_conf_val("ranlib");
&use_build_env_conf_val("nawk");
&use_build_env_conf_val("sh");

&out("SYS",$opsys);
&out("CC",$c_compile);
&out("FC",$fortran_compile);
&out("AR",$archive_command);
&out("RANLIB",$ranlib);
&out("NAWK",$nawk);
$sh && &out("SHELL",$sh);

&out("PERL",$perl_location);



###
#	JGOFS SYSTEM/WEB SERVER STUFF
###

&use_build_env_conf_val("program");
(defined $program) || ($program = "");

$opt_prefix = ($file =~ /^opt-/) ? "opt" : "";

if ($defnlist) {
  $defnlist_env_var = $opt_prefix;
  $defnlist_env_var && ($defnlist_env_var .= "-");
  $defnlist_env_var .= "BUILD-ENV-DEFNS";
}

&use_build_env_conf_val("server_name");
$server_name || die ("\$server_name variable empty or undefined");
#   Field requirement because of Apache // -> / mod in PATH_INFO
#   JGOFS system uses /X.Y.Z to mean remote object.  Can be > 3 fields
(undef,undef,$dot_field_3) = split /\./,$server_name;
(defined $dot_field_3) || 
    die ("\$server_name variable must have at least 3 dot-separated fields." .
						"  Bad value is $server_name");

&use_build_env_conf_val("port");
$port || die ("\$port variable empty or undefined");
($port =~ /^\d+$/) || die ("\$port variable must consist entirely of digits." .
			    "  Bad value is $port");

&use_build_env_conf_val("doc_root");
$doc_root || die ("\$doc_root variable empty or undefined");
#   Single initial/trailing slash worth fixing.  Multiples too suspicious
($doc_root =~ q|^//|) && 
	die("\$doc_root variable may not begin with multiple slashes." .
						"  Bad value is $doc_root");
($doc_root =~ q|^/|) && ($doc_root = ($doc_root =~ /.(.+)/));
($doc_root =~ q|//$|) && 
	die("\$doc_root variable may not end with multiple slashes." .
						"  Bad value is $doc_root");
($doc_root =~ q|/$|) && (chop $doc_root);

&use_build_env_conf_val("jgofs_cgi_root");
$jgofs_cgi_root || die ("\$jgofs_cgi_root variable empty or undefined");
#   Single initial/trailing slash worth fixing.  Multiples too suspicious
($jgofs_cgi_root =~ q|^//|) && 
	die("\$jgofs_cgi_root variable may not begin with multiple slashes." .
					"  Bad value is $jgofs_cgi_root");
($jgofs_cgi_root =~ q|^/|) && ($jgofs_cgi_root = ($jgofs_cgi_root =~ /.(.+)/));
($jgofs_cgi_root =~ q|//$|) && 
	die("\$jgofs_cgi_root variable may not end with multiple slashes." .
					"  Bad value is $jgofs_cgi_root");
($jgofs_cgi_root =~ q|/$|) && (chop $jgofs_cgi_root);

#   Presence/absence of slashes, "http:"s, etc is historical.
#   So are most comments below.
#   Unfortunately not much consistency...
$myaddr = "$server_name:$port";

#   I suspect trouble if $jgofs_root is /.  Since things that use this
#   assume that $jgofs_root does not end in /, we'll put out the empty
#   string and hope for the best
&out("JGOFSDIR",$jgofs_root);

&out("OPT","$opt_prefix");
&out("PORT",$port);
&out("MYADDR",$myaddr);

#
# location to be added to MYADDR to find Optionserver scripts
# (and other stuff, like serv.  Probably not in use in data servers
# at the moment. wjs Jun 04)
#
&out("JGSCRIPTDIR","/$jgofs_cgi_root");

#   Deliberately allowing user to specify empty $button_images_dir
#   which means that images are in default doc root of server
&use_build_env_conf_val("button_images_dir");
&use_build_env_conf_val("button_images_port");
&use_build_env_conf_val("button_images_server");
if (defined $button_images_dir) {
  $button_images_server || ($button_images_server = $server_name);
  $button_images_port && ($button_images_port = ":$button_images_port");
  $temp = "http://$button_images_server$button_images_port";
  $button_images_dir && ($temp .= "/$button_images_dir");
  &out("BUTTONIMAGESDIR",$temp);
}

#   option_server_program definition below assumes that the OOserver being
#   pointed to actually HAS a jg/otheropt3.  This file was added
#   to the default gb11.whoi.edu OOserver 1 Jun 09.
#     If option_server_host in fact points to the default
#   OOserver, then the defn assumes that the JGOFS server 
#   being build with this file is running at least outer 3.2.  If
#   this second assumption is NOT correct, then the defn should 
#   be changed to point to jg/otheropt2, if the data server is
#   running at least outer 3.0, or jg/otheropt otherwise
$option_server_host = "optserv1.whoi.edu";
$option_server_port = "";
$option_server_program = "jg/otheropt3";
&use_build_env_conf_val("option_server_host");
&use_build_env_conf_val("option_server_port");
&use_build_env_conf_val("option_server_program");
$option_server = $option_server_host;
$option_server_port && ($option_server .= ":" . $option_server_port); 
$option_server .= "/" . $option_server_program;
&out("OPTIONSERVER",$option_server);

#   Deliberately allowing user to specify empty $dmoname
&use_build_env_conf_val("dmoname");
if ($program eq "JGOFS") {
  $info_server = "usjgofs.whoi.edu/jg/info";
  $dir_server = "usjgofs.whoi.edu/jg/dir";
  (defined $dmoname) || ($dmoname = "US JGOFS");
  $top_object_subdir = "/jgofs";
} elsif ($program eq "GLOBEC") {
  $info_server = "globec.whoi.edu/jg/info";
  $dir_server = "globec.whoi.edu/jg/dir";
  (defined $dmoname) || ($dmoname = "U.S. GLOBEC");
  $top_object_subdir = "/globec";
} else {
  $info_server = "$myaddr/$jgofs_cgi_root/info";
  $dir_server = "$myaddr/$jgofs_cgi_root/dir";
  (defined $dmoname) || ($dmoname = "$server_name -- ");
  $top_object_subdir = "/";
}
&use_build_env_conf_val("info_server");
&use_build_env_conf_val("dir_server");
&use_build_env_conf_val("top_object_subdir");
$objdir = $top_object_subdir;

&use_build_env_conf_val("background_color");
&use_build_env_conf_val("display_data_url");
(defined $background_color) && &out("BACKGROUND_COLOR",$background_color);
(defined $display_data_url) && &out("DISPLAY_DATA_URL",$display_data_url);

&use_build_env_conf_val("usedatabase");
&use_build_env_conf_val("info_sources");
&use_build_env_conf_val("info_log_all_sources");
&use_build_env_conf_val("info_log_misses");
&use_build_env_conf_val("info_action_on_data_source_error");
&use_build_env_conf_val("info_action_on_logging_failure");
&use_build_env_conf_val("info_BCODMO_host_port");
&use_build_env_conf_val("info_BCODMO_access_techniques");
&use_build_env_conf_val("info_BCODMO_attempt_all_techniques");
&use_build_env_conf_val("info_remote_traditional_servers");
&use_build_env_conf_val("info_attempt_all_remote_traditional_servers");
(defined $usedatabase) && &out("USEDATABASE",$usedatabase);
(defined $usedatabase) && &out("INFO_USEDATABASE",$usedatabase);
(defined $info_sources) && &out("INFO_SOURCES",$info_sources);
(defined $info_log_all_sources) && 
			&out("INFO_LOG_ALL_SOURCES",$info_log_all_sources);
(defined $info_log_misses) && &out("INFO_LOG_MISSES",$info_log_misses);
(defined $info_action_on_data_source_error) && 
    &out("INFO_ACTION_ON_DATA_SOURCE_ERROR",$info_action_on_data_source_error);
(defined $info_action_on_logging_failure) && 
	&out("INFO_ACTION_ON_LOGGING_FAILURE",$info_action_on_logging_failure);
(defined $info_BCODMO_host_port) && 
			&out("INFO_BCO-DMO_HOST_PORT",$info_BCODMO_host_port);
(defined $info_BCODMO_access_techniques) && 
	&out("INFO_BCO-DMO_ACCESS_TECHNIQUES",$info_BCODMO_access_techniques);
(defined $info_BCODMO_attempt_all_techniques) &&
	&out("INFO_BCO-DMO_ATTEMPT_ALL_TECHNIQUES",
					$info_BCODMO_attempt_all_techniques);
(defined $info_remote_traditional_servers) && 
      &out("INFO_REMOTE_TRADITIONAL_SERVERS",$info_remote_traditional_servers);
(defined $info_attempt_all_remote_traditional_servers) && 
      &out("INFO_ATTEMPT_ALL_REMOTE_TRADITIONAL_SERVERS",
				$info_attempt_all_remote_traditional_servers);

&out("INFOSERVER",$info_server);
&out("DIRSERVER",$dir_server);
&out("OBJDIR",$objdir);
&out("DMONAME",$dmoname);

if ($opt_prefix) {
#
#   HELPDIR is subdirectory from JGOFSDIR for help documents
#   HELPADDR is full URL to the location for help documents
#
  $temp = $doc_root;
  $help_dir && ($temp .= "/$help_dir");
  &out("HELPDIR",$temp);

  $temp = "http://$myaddr";
  $help_dir && ($temp .= "/$help_dir");
  &out("HELPADDR",$temp);

#
#   TEMPDIR/TEMPADDR are similar but, alas, not same as
#   HELPDIR/HELPADDR.
#      TEMPDIR is absolute dir spec to
#   a temporary directory unique to OOserver on this port
#   (HELPDIR is a relative directory)
#      TEMPADDR is URL of that directory, which depends
#   on TEMPDIR being a subdir of $jgofs_root/$doc_root
#   (HELPADDR works on same concept)
#      Note that the soft link from $doc_root/jgofsopt
#   to top-level /jgofsopt is NOT necessary (but I think
#   some code accidentally works because of it)
#
  &use_build_env_conf_val("temp_dir");
  $temp = "$jgofs_root/$doc_root";
  $temp_dir && ($temp .= "/$temp_dir");
  &out("TEMPDIR",$temp);

  $temp = "http://$myaddr";
  $temp_dir && ($temp .= "/$temp_dir");
  &out("TEMPADDR",$temp);

  &use_build_env_conf_val("mail_sender");
  &use_build_env_conf_val("zip");
  &use_build_env_conf_val("mv");
  &use_build_env_conf_val("cp");
  &use_build_env_conf_val("tar");
  &use_build_env_conf_val("compress");
  &use_build_env_conf_val("gzip");

  &out("MAIL_SENDER",$mail_sender);
  &out("ZIPPROG",$zip);
  &out("MVPROG",$mv);
  &out("CPPROG",$cp);
  &out("TARPROG",$tar);
  &out("UNIXCOMPRESS",$compress);
  &out("GNUCOMPRESS",$gzip);
}

# linker stuff for netcdf and matlab converters
# Allow converters to be built in a data server if user wants them (set param to 1)
&use_build_env_conf_val("setup_ooserver_build_environment");
($setup_ooserver_build_environment eq "") || ($setup_ooserver_build_environment == 0) ||
  ($setup_ooserver_build_environment == 1) || 
        (die "Bad value for \$setup_ooserver_build_environment.  Legal values are empty string, 0 & 1\n" .
            "Value received was -->$setup_ooserver_build_environment<--");
if ($opt_prefix || $setup_ooserver_build_environment) {
  &use_build_env_conf_val($matlab_version_list);
  foreach (  "netcdf",(split /,/,$matlab_version_list)  ) {
    &use_build_env_conf_val($_);
    &check_and_out_inclib_dirs($_);
  }
#   Default matlab based on 1st in $matlab_version_list (or what's in conf file)
  ($matlab) = split /,/,$matlab_version_list;
  &use_build_env_conf_val("matlab");
  $tmp = $matlab . "incdir";
  $matlabincdir = $$tmp;
  $tmp = $matlab . "libdir";
  $matlablibdir = $$tmp;
  $tmp = $matlab . "syslibdir";
  $matlabsyslibdir = $$tmp;
  &check_and_out_inclib_dirs("matlab");
  &out("MATLAB_VERSION_LIST",$matlab_version_list);
}

&use_build_env_conf_val("local_server_roots");
&out("LOCAL_SERVER_ROOTS",$local_server_roots);

#   Check that all conf file data were used
#   The hash is probably always defined, but too lazy to
#   be exact about this [Removed  defined  per newer compiler
#   suggestion/objection (build-env.pl v 1.4a); in a test, expected
#   but did not get "undefined %foo" in the only statement that contained
#   %foo; hypothesis about "hash always defined" seems syntactically true,
#   logic of this program being irrelevant to the assertion]
  if (%build_env_conf) {
    $nkeys = 0;
    $msg = "*** Error\n" .
	   "*** Following data defined in $build_env_conf_file " .
					"but not used by $this_file\n";
    foreach (keys(%build_env_conf)) {
      $nkeys++;
#	If the string is ACTUALLY defined as "[empty string]", I still
#	think that the problem can be figured out from the message
#	  Don't see how hash value can be undefined, but the test is really 
#	  cheap compared to the mystery if it actually DOES happen
      (defined ($value = $build_env_conf{$_})) || ($value = "[not defined]");
      $value || ($value = "[empty string]");
      $msg .= "***\t$_ defined as $value\n";
    }
    ($nkeys == 0) || die $msg;
  }

#   Avoid "only one use" diagnostics
(exists $old_ENV{"JGOFSDIR"}) && 1;
undef $build_env_pl_version;
undef $netcdfincdir;
undef $netcdflibdir;
undef $matlabincdir;
undef $matlablibdir;
undef $matlabsyslibdir;
undef $matlab5incdir;
undef $matlab5libdir;
undef $matlab5syslibdir;
undef $matlab65incdir;
undef $matlab65libdir;
undef $matlab65syslibdir;
undef $matlab713incdir;
undef $matlab713libdir;
undef $matlab713syslibdir;

#   Next line sets "status" of file to 1.  This game an attempt
#   to allow this program to be run both standalone and as a
#   "require"d file.  Not sure if it is really necessary
1;
}
#   Next line is so file can be "require"d successfully
1;


sub check_and_out_inclib_dirs
#     Fooling with trailing slashes may not be necessary.
#   In any case, objective is 1) make the cc options -L & -I happy
#   &  2) make LD_LIBRARY_PATH happy.  At minimum, objective should
#   be to put out a consistent string whether or not user plays with
#   slashes
{
  my ($root,$dummy) = @_;
  my ($incdir,$libdir,$syslibdir);
  my ($save_chomp_char);

  ($root && ! $dummy) || die "Internal error: check_and_out called w/bad args";

  $incdir = $root . "incdir";
  $libdir = $root . "libdir";
  $syslibdir = $root . "syslibdir";
  &use_build_env_conf_val($incdir);
  &use_build_env_conf_val($libdir);
  &use_build_env_conf_val($syslibdir);
  (defined $$syslibdir) || ($$syslibdir = "");
  if ($incdir || $libdir) {
    ($incdir && $libdir) || 
	die uc($root) .
		" include or library directory specified but not the other";
    $save_chomp_char = $/;
    $/ = '/';
    chomp $$incdir;
    chomp $$libdir;
    chomp $$syslibdir;
    $/ = $save_chomp_char;
    &out(uc($incdir),$$incdir);
    &out(uc($libdir),$$libdir);
    &out(uc($syslibdir),$$syslibdir);
  }
  return;
}


sub out
{
  my ($env_var,@env_var_val) = @_;
  my ($env_var_val);

  (scalar @env_var_val > 0) || die "No value for env var $env_var\n";

  $env_var_val = join (' ',@env_var_val);

  $sh_out && (print "export $env_var=\"$env_var_val\"\n");
  $csh_out && (print "setenv $env_var \"$env_var_val\"\n");
  if (defined $defnlist) {
    $defnlist && ($defnlist .= ",");
    $defnlist .= $env_var;
    $ENV{$defnlist_env_var} = $defnlist;
  }
#   If we are about to change an env var, save it... but only save
#   the first one (protects in case this routine for some reason
#   changes something more than once - we want the "original")
  if (exists $ENV{$env_var}) {
    (exists $old_ENV{$env_var}) || ($old_ENV{$env_var} = $ENV{$env_var});
  }
  $ENV{$env_var} = $env_var_val;
  return;
}

sub use_build_env_conf_val
#   Given a string as an argument, check if build_env_conf{string} is defined
#   Assumes that build_env_conf and $string are global, so only works if
#     build_env_conf is the name of the hash.  Routine will never do anything
#     if "wrong" hash is in use
#   If so, set $string to build_env_conf{string} and remove 
#     the build_env_conf entry
#   If not, do nothing
#   Return whether build_env_conf{string} existed
#   Requires a version of perl sufficiently new that $$x works
{
  my ($arg,$dummy) = @_;
  my $temp;
  ($arg && ( ! $dummy)) || 
	(die "Internal error.  use_build_env_conf_val called w/incorrect args");
  if  ( $status = (exists $build_env_conf{$arg})  ) {
    $$arg = $build_env_conf{$arg};
    delete $build_env_conf{$arg};
  }
  return $status;
}
