#!/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)
#
#	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
#	    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
#	  BUTTONIMAGESDIR	http://optserv1.whoi.edu:8200/images
#	  CC		cc -DSOL -DSVR4 -lnsl -lsocket
#	  CONFIG_FILE	/export/home/wsass/opt-build-env.pl
#	  DIRSERVER	optserv1.whoi.edu:8200/jg/dir
#	  DMONAME	Option Server
#	  FC		f77 -lnsl -lsocket
#	  GNUCOMPRESS	/usr/local/bin/gzip
#	  HELPADDR	http://optserv1.whoi.edu:8200
#	  HELPDIR	htdocs
#	  INFOSERVER	optserv1.whoi.edu:8200/jg/info
#	  JGOFSDIR	/export/home/wsass
#	  JGSCRIPTDIR	/jg
#	  MYADDR	optserv1.whoi.edu:8200
#	  NAWK		nawk
#	  OBJDIR	/
#	  OPT		opt
#	  OPTIONSERVER	optserv1.whoi.edu/jg/otheropt
#	  PERL		/usr/bin/perl
#	  PORT		8200
#	  RANLIB	echo
#	  SYS		Solaris
#	  TARPROG	/usr/bin/tar
#	  TEMPDIR	/export/home/wsass/htdocs/jgofsopt/8200
#	  UNIXCOMPRESS	/usr/bin/compress
#	  ZIPPROG	/usr/local/bin/zip


#  16 Jul 04.  wjs
#	ENV{"PERL"} for backward compatibility
#	Add _LIB_LOC to MATLABx defns
#  29 Jun 04.  wjs
#	$ENV{"OPT"}.  "opt" if XXXXbuild-env.pl starts with opt-; "" else
#	  Looks at main pgm variable $::build_env_file_name; if undefined, uses $0
#	Default infoserver, dirserver, dir's object root, and default server
#	  name for JGOFS & GLOBEC programs
#   9 Jun 04.  wjs
#	Switches; argument; perl location.
#   4 Jun 04.  wjs
#	Stop supporting SunOS (saves us from having to figure out where
#	  ar is located, among other things)
#   1 Jun 04.  wjs
#	Change from csh to perl.
#	Add CONFIG_FILE - name of this file
#	  Derive JGOFSDIR from CONFIG_FILE instead of from `pwd`
#	Add dir spec to ar - seems to "move around"
#	Point TEMPDIR to htdocs/jgofsopt/PORT.  The fact that we
#	  alias that to /jgofsopt is irrelevant to the working of
#	  the software, and might not be something that an installer
#	  wants to do.  On the other hand, the web server will
#	  insist that jgofsopt be a subdir in the htdocs tree
#   5 May 04.  wjs
#	Add matlab library locations
#  12 Apr 04.  wjs
#	Mods for port 8200 wjs server.  Leave node as optserv1
#  29 Dec 1999. Use development file but change port to 80.  rcg
#  18 Nov 1999. clh add vars for download specifically, but may be useful
# 		for other scripts in system.
#  

  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";
  }

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

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

#   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 /usr/bin/perl wherever_perl_is)
#	2) "require"ing this file
#	3) using $ENV{"whatever"} instead of the placeholder
$perl_location = "/usr/bin/perl";
########
#   Coordinate web server and this directory tree ($jgofs_root)

#   Next group must be defined and non-empty
#   _root's are relative to the directory this config file is in,
#     and must be defined in server config for $server_name:$port
$server_name = "optserv1.whoi.edu";
$port = 8200;
$doc_root = "htdocs";
$jgofs_cgi_root = "jg";

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

#   directory relative to $doc_root where images for outer's
#   display buttons are located.  If no images, comment out next
#   line
$button_images_dir = "images";

#	NEXT VARIABLE ONLY NEEDED FOR OPTION SERVER SETUP
#   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)
$temp_dir = "jgofsopt/$port";
########

#	NEXT VARIABLE 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 help
#   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 = "";

#
#   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 (above)
$dmoname = "Option Server";

#	NEXT VARIABLE ONLY NEEDED FOR OPTION SERVER SETUP
# matlab library locations.  V5 is special in that the matlab formats
# after it were made proprietary by matlab.  Hence we should 'always'
# offer v5, as well as 'latest'.  Put as many 'aliases' as needed"; eg,
# if there are several versions of matlab 6, define all of interest,
# plus a MATLAB6 pointing to the one of choice.  Similarly, define
# a default MATLAB in case user amazingly does NOT want to pick!
# Value here is propagated in build-opt-env.pl.  Format of names
# and env var must match optbin/makematlab.pl.
$matlab_list =
     q(
	MATLAB_LIB_LOC		/data13/opt/matlab61/extern/lib/sol2
	MATLAB6_LIB_LOC		/data13/opt/matlab61/extern/lib/sol2
	MATLAB61_LIB_LOC	/data13/opt/matlab61/extern/lib/sol2
	MATLAB5_LIB_LOC		/data13/opt/matlab531/extern/lib/sol2
	MATLAB531_LIB_LOC	/data13/opt/matlab531/extern/lib/sol2
       );

#   DOWNLOAD FUNCTIONALITY ONLY NEEDED FOR OPTION SERVER SETUP
#   auxiliary programs required for download functionality
$zip = "/usr/local/bin/zip";
$tar = "/usr/bin/tar";
$compress = "/usr/bin/compress";
$gzip = "/usr/local/bin/gzip";

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



###
#	OPERATING SYSTEM STUFF
###

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

if ( $opsys eq 'linux' ) {
  &out("CC","cc");
  &out("FC","g77 -O");
  &out("AR","/usr/bin/ar rv");
  &out("RANLIB","ranlib");
  &out("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' ) {
  &out("CC","cc -DSOL -DSVR4 -lnsl -lsocket");
  &out("FC","f77 -lnsl -lsocket");
  &out("AR","/usr/ccs/bin/ar srv");
  &out("RANLIB","echo");
  &out("NAWK","nawk");
} elsif ( $opsys eq 'Irix' ) {
  &out("CC","cc -cckr -DIRIX");
  &out("F77","f77 -woff 2290");
  &out("AR","/usr/bin/ar -srv");
  &out("RANLIB","echo");
  &out("NAWK","nawk");
  &out("SHELL","/sbin/sh");
} else {
  die "Unrecognized value for \$opsys (operating system) variable: $opsys";
}
&out("SYS",$opsys);

&out("PERL",$perl_location);



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

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

$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);

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

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

#   Could do more checks on next group.  port should be numeric.
#   _root's should neither lead nor end w/slashes
$server_name || die ("\$server_name variable empty or undefined");
$port || die ("\$port variable empty or undefined");
$doc_root || die ("\$doc_root variable empty or undefined");
$jgofs_cgi_root || die ("\$jgofs_cgi_root variable empty or undefined");

#   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);

#
# 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);


#
# 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 $doc_root
if (defined $button_images_dir) {
  $temp = "http://$myaddr";
  $button_images_dir && ($temp .= "/$button_images_dir");
  &out("BUTTONIMAGESDIR",$temp);
}

$temp = "$jgofs_root/$doc_root";
$temp_dir && ($temp .= "/$temp_dir");
&out("TEMPDIR",$temp);

$option_server = "optserv1.whoi.edu/jg/otheropt";
$opt_prefix && ($option_server ne "$myaddr/$jgofs_cgi_root/otheropt") &&
	(warn "$this_file not being used to set up system OO server\n" .
	      "system OO server = $option_server\n" .
	      "server being set up = $myaddr/$jgofs_cgi_root/otheropt\n");
&out("OPTIONSERVER",$option_server);

#   Deliberately allowing user to specify empty $dmoname
if ($program eq "JGOFS") {
  $info_server = "usjgofs.whoi.edu/jg/info";
  $dir_server = "usjgofs.whoi.edu/jg/dir";
  (defined $dmoname) || ($dmoname = "US JGOFS");
  $objdir = "/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");
  $objdir = "/globec";
} else {
  $info_server = "$myaddr/$jgofs_cgi_root/info";
  $dir_server = "$myaddr/$jgofs_cgi_root/dir";
  (defined $dmoname) || ($dmoname = "$server_name -- ");
  $objdir = "/";
}

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

if ($opt_prefix) {
  &out("ZIPPROG",$zip);
  &out("TARPROG",$tar);
  &out("UNIXCOMPRESS",$compress);
  &out("GNUCOMPRESS",$gzip);
  &out("M_LIST",$matlab_list);
}

#   Refer to $old_ENV to avoid "only one use" diagnostic
(exists $old_ENV{"JGOFSDIR"}) && 1;
1;
}
1;

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

  $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;
}
