eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}'
                    & eval 'exec perl -S $0 $argv:q'
                         if 0;
#--------------------------------------------------------------------
#    The MB-system:	install_makefiles	5/20/94
#    $Id: install_makefiles 1811 2009-12-26 21:53:44Z caress $
#
#    Copyright (c) 1993-2009 by 
#    D. W. Caress (caress@lamont.ldgo.columbia.edu)
#    and D. N. Chayes (dale@lamont.ldgo.columbia.edu)
#    Lamont-Doherty Earth Observatory
#    Palisades, NY  10964
#
#    See README file for copying and redistribution conditions.
#--------------------------------------------------------------------
#
# Command:
#   install_makefiles [-Ppreset]
#
# Author:
#   David W. Caress
#   Lamont-Doherty Earth Observatory
#   Palisades, NY  10964
#   May 20, 1994
#
# Purpose:
#   Perl shellscript to create makefiles appropriate to a particular
#   MB-System installation.  Before running this script, the
#   installer should edit the variable declarations below (where
#   indicated) so that these values are appropriate for the system
#   on which MB-System is being installed.  At execution the script 
#   will read template makefiles in the MB-System source directories 
#   and create makefiles with the correct attributes.
#   This enables users to install MB-System without having to edit
#   the makefiles.  Instead of editing the makefiles directly, the
#   installer should edit this script so that the locations of
#   important libraries and include files are accurate and so that
#   the compile and link flags are correct.
#
#   The required and optional installation parameters are:
#
#   REQUIRED:
#	MBSYSTEM_HOME	Absolute path to the directory containing this file.
#	OS		Operating system (Recognized options are:
#				DARWIN, LINUX, CYGWIN, SUN, IRIX, IRIX64,
#				SOLARIS, HPUX, LYNX, CYGWIN or OTHER).
#	CFLAGS		Compile flags for C source files. These vary with
#				different operating systems and installations.
#	LFLAGS		Load flags for all object files.
#	NETCDFLIBDIR	Location of NetCDF library libnetcdf.a
#	NETCDFINCDIR	Location of NetCDF include file netcdf.h
#	GMTLIBDIR	Location of GMT libraries libgmt.a and libpsl.a
#	GMTINCDIR	Location of GMT include files gmt.h, grd.h, and pslib.h
#	LEVITUS		Path of Levitus global water properties data file. This is usually 
#                               $MBSYSTEM_HOME/share/annual.
#	PROJECTIONS	Path of PROJ4 projections database. This is usually 
#                               $MBSYSTEM_HOME/share/Projections.dat.
#   REQUIRED for spectrogram tool mbsegypsd..
#	FFTWLIBDIR	Location of FFTW library libfftw.a
#	FFTWINCDIR	Location of FFTW include file fftw.h 
#			If these parameters are not both set, mbsegypsd will 
#			not be built.
#   REQUIRED for openGL tools mbgrdviz and mbeditviz:
#       OPENGLLIBS	OpenGL libraries required for new graphical tools,
#.                              including mbgrdviz and mbeditviz. If this  
#                               parameter is not set, mbgrdviz and mbeditviz
#                               will not be built
#   REQUIRED for tidal prediction program mbotps:
#       OTPSDIR		Location of OTPSnc package, including executable
#.                              predict_tide and batch files used by  
#                               predict_tide. If this is not specified
#				correction mbotps will build but not run
#				correctly
#   OPTIONAL - use non-default C compiler:
#	CC		C compiler to be used (optional).
#   OPTIONAL - use independent installation of the PROJ4 library:
#       PROJLIB         Path to PROJ4 library (usually libproj.a)
#       PROJINCDIR      Location of PROJ4 include file proj_api.h
#   OPTIONAL - use independent installation of the GSF library:
#       GSFLIB          Path to GSF library (usually libgsf.a)
#       GSFINCDIR       Location of GSF include file gsf.h
#
#   A few preset variable configurations are defined and can be
#   specified using the -P option. These include:
#	macosxfink 	-install on MacOs X using fink
#				- assumes installation in /sw
#				- assumes use of Apple's X11
#				- assumes netCDF, GMT, and OpenMotif installe
#					using fink
#       macosxusrlocal - install manually on MacOs X 10.5 (Leopard)
#				- assumes installation in /usr/local/mbsystem
#				- assumes use of Apple's X11
#				- assumes netCDF, GMT, and OpenMotif installe
#					using fink
#       irixusrlocal - install manually on SGI IRIX 
#				- assumes installation in /usr/local/mbsystem
#				- assumes netCDF and GMT installs in /usr/local
#	
#--------------------------------------------------------------------
# EDIT THE PARAMETERS HERE IF NO APPROPRIATE PRESET CONFIGURATION IS DEFINED
# Set the configuration parameters manually:
#--------------------------------------------------------------------
#
# Required parameters:
$MBSYSTEM_HOME = "/usr/local/mbsystem";
$OS = "DARWIN";
$CFLAGS = "-Wall -g -I/usr/X11R6/include";
$LFLAGS = "-Wall -lm -bind_at_load -Wl,-dylib_file,/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib:/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/LibGL.dylib";
$NETCDFLIBDIR = "/sw/lib";
$NETCDFINCDIR = "/sw/include";
$GMTLIBDIR = "/usr/local/gmt/lib";
$GMTINCDIR = "/usr/local/gmt/include";
$LEVITUS = "$MBSYSTEM_HOME/share/LevitusAnnual82.dat";
$PROJECTIONS = "$MBSYSTEM_HOME/share/Projections.dat";
#
# Required parameters for spectrogram program mbsegypsd
#   - mbsegypsd will not be built if these are commented out
$FFTWLIBDIR = "/usr/local/fftw/lib";
$FFTWINCDIR = "/usr/local/fftw/include";
#
# Required parameters for graphical tools
#   - graphical tools will not be built if these
#     are commented out
$MOTIFINCDIR = "/sw/include";
$MOTIFLIBS = "-L/sw/lib -L/usr/X11R6/lib -lXm -lXt -lX11";
#
# Required parameter for visualization tools
#   - visualization tools will not be built if this
#     is commented out
$OPENGLLIBS = "-lGLU -lGL";
#
# Required parameter for mblevitus program
$LEVITUS = "$MBSYSTEM_HOME/share/LevitusAnnual82.dat";
#
# Required parameter for mbotps program
$OTPSDIR = "/usr/local/OTPSnc";
#
# Optional parameter for use of non-default C compiler
#$CC = "cc";
#
# Optional parameters for use of independent GSF library installation
#   - GSF library included with MB-System will be used 
#     if these are commented out
#$GSFLIB = "/usr/local/gsf/lib/libgsf.a";
#$GSFINCDIR = "/usr/local/gsf/include/";
#
# Optional parameters for use of independent PROJ4 library installation
#   - PROJ4 library included with MB-System will be used 
#     if these are commented out
#$PROJLIB = "/usr/local/proj/lib/libproj.a";
#$PROJINCDIR = "/usr/local/proj/include/";
#
#--------------------------------------------------------------------
# END OF PARAMETER EDITING SECTION
#--------------------------------------------------------------------
#
# Version:
#   $Id: install_makefiles 1811 2009-12-26 21:53:44Z caress $
#
# Revisions:
#   $Log: install_makefiles,v $
#   Revision 5.35  2009/03/13 07:05:58  caress
#   Release 5.1.2beta02
#
#   Revision 5.34  2009/03/02 18:59:05  caress
#   Moving towards 5.1.2beta1.
#
#   Revision 5.33  2008/12/31 08:47:38  caress
#   Updates towards release 5.1.1
#
#   Revision 5.32  2008/12/22 08:36:18  caress
#   Check in of 22 Dec 2008.
#
#   Revision 5.31  2008/09/11 19:58:27  caress
#   Update to Mac templates.
#
#   Revision 5.30  2008/05/24 19:28:41  caress
#   Updated description on how to build MB-System on MacOs X.
#
#   Revision 5.29  2007/08/24 23:37:05  caress
#   Fixed CYGWIN preset and removed -lGLw references.
#
#   Revision 5.28  2007/05/25 19:32:04  caress
#   Removed reference to libGLw
#
#   Revision 5.27  2006/12/15 21:42:49  caress
#   Incremental CVS update.
#
#   Revision 5.26  2006/09/11 18:55:51  caress
#   Changes during Western Flyer and Thomas Thompson cruises, August-September
#   2006.
#
#   Revision 5.25  2006/07/06 05:30:57  caress
#   Working more towards 5.1.0beta
#
#   Revision 5.24  2006/07/05 19:50:21  caress
#   Working towards 5.1.0beta
#
#   Revision 5.23  2006/06/16 19:24:47  caress
#   Check in after the Santa Monica Basin Mapping AUV Expedition.
#
#   Revision 5.22  2006/01/27 19:08:27  caress
#   Version 5.0.8beta2
#
#   Revision 5.21  2006/01/18 07:26:04  caress
#   Removed all references to GMT versions since MB-System will now build only with GMT4.1.
#
#   Revision 5.20  2006/01/11 07:23:24  caress
#   Working towards 5.0.8
#
#   Revision 5.19  2006/01/06 18:19:00  caress
#   Working towards 5.0.8
#
#   Revision 5.18  2005/06/04 03:32:26  caress
#   Updates during 5.0.8 development.
#
#   Revision 5.17  2005/03/25 04:01:02  caress
#   Fixes to 5.0.6.
#
#   Revision 5.16  2005/02/20 07:25:08  caress
#   Release 5.0.6
#
#   Revision 5.15  2004/10/18 03:32:55  caress
#   Added -P preset option.
#
#   Revision 5.14  2004/10/06 22:06:32  caress
#   Release 5.0.5
#
#   Revision 5.13  2004/05/21 23:06:29  caress
#   Supporting GMT 4.0
#
#   Revision 5.12  2004/04/16 19:35:28  caress
#   Changed MacOs X $LFLAG settings so OpenMotif can be used instead of LessTif.
#
#   Revision 5.11  2004/02/13 20:24:06  caress
#   Added up to date parameters for Solaris 2.7
#
#   Revision 5.10  2003/12/06 00:15:06  caress
#   Fixed for release 5.0.0
#
#   Revision 5.9  2003/12/05 07:54:13  caress
#   The script now recognizes mbview when OpenGL libraries are specified.
#
#   Revision 5.8  2003/12/02 07:49:03  caress
#   Now creates bin include lib directories if needed.
#
#   Revision 5.7  2003/11/25 21:07:09  caress
#   Dave's current install_makefiles.
#
#   Revision 5.6  2003/08/25 01:30:06  caress
#   *** empty log message ***
#
#   Revision 5.5  2003/07/02 18:08:43  caress
#   Release 5.0.0
#
#   Revision 5.4  2002/05/29 21:06:33  caress
#   Release 5.0.beta18
#
#   Revision 5.3  2002/04/06 03:15:17  caress
#   Release 5.0.beta16
#
#   Revision 5.2  2002/01/24 02:12:31  caress
#   Added DARWIN
#
# Revision 5.0  2001/01/22  05:02:30  caress
# Version 5.0.0
#
# Revision 4.20  2000/10/25  20:31:20  caress
# Added references to GMT version 3.3
#
# Revision 4.19  2000/09/30  05:07:19  caress
# Snapshot for Dale.
#
# Revision 4.18  1999/04/16  00:26:05  caress
# Final 4.6 release?
#
# Revision 4.17  1999/02/04  23:55:57  caress
# MB-System version 4.6beta7
#
# Revision 4.16  1998/10/05  22:12:48  caress
# MB-System version 4.6beta
#
# Revision 4.15  1997/09/18  13:29:15  caress
# Removed references to realtime stuff.
#
# Revision 4.14  1997/09/15  19:02:15  caress
# Real Version 4.5
#
# Revision 4.13  1997/04/21  16:51:19  caress
# MB-System 4.5 Beta Release.
#
# Revision 4.14  1997/04/17  19:31:15  caress
# Removed Fortran related parameters from user edit area
# and added LINUX to list of supported operating systems.
#
# Revision 4.13  1997/04/17  15:18:34  caress
# MB-System 4.5 Beta Release
#
# Revision 4.12  1996/09/09  15:17:50  caress
# Turning GRAPHICAL off now turns mbnavedit off.
#
# Revision 4.11  1995/08/17  16:39:53  caress
# Revision for release 4.3.
#
# Revision 4.10  1995/08/07  17:25:26  caress
# Moved to GMT V3.
#
# Revision 4.9  1995/07/14  14:09:01  caress
# Check during serious bug fix flail.
#
# Revision 4.8  1995/06/06  14:18:26  caress
# Updated example for installing under Solaris 2.4
#
# Revision 4.7  1995/03/23  16:30:08  caress
# Added -Olimit flag for compiling mbgrid.c under IRIX
#
# Revision 4.6  1995/03/22  19:19:27  caress
# Added support for HPUX.
#
# Revision 4.5  1995/02/15  14:42:44  caress
# Version 4.2 Release
#
# Revision 4.4  1994/10/21  13:38:18  caress
# Release V4.0
#
# Revision 4.3  1994/10/20  21:23:53  caress
# Release V4.0
#
# Revision 4.2  1994/06/22  19:30:38  caress
# Added support for different archivers used under
# different operating systems (e.g. ar used for Suns and
# SGIs, libr used under Lynx).
#
# Revision 4.1  1994/06/21  22:52:01  caress
# Changes to support PCs running Lynx OS.
#
# Revision 4.0  1994/05/24  20:40:50  caress
# First cut.
#
#
#--------------------------------------------------------------------

# Deal with command line arguments
$command_line = "@ARGV";
&MBGetopts('P:p:');
$preset = 	($opt_P || $opt_p);

	
#--------------------------------------------------------------------
# USE PARAMETERS SET ABOVE IF NO APPROPRIATE PRESET CONFIGURATION IS DEFINED
#--------------------------------------------------------------------
if (!$preset)
	{
	print "\nUsing manually defined configuration...\n";
	}
#--------------------------------------------------------------------
# ELSE RESET PARAMETERS USING SPECIFIED PRESET CONFIGURATION
#--------------------------------------------------------------------
# 
# Preset configuration for MacOs X when building through fink
elsif ($preset eq "macosxfink")
	{
	print "\nUsing preset configuration for MacOs X by fink in /sw...\n";
	$FINKPREFIX = "\@FINKPREFIX@";
	$MBSYSTEM_HOME = "$FINKPREFIX";
	$OS = "DARWIN";
	$CC = "cc";
	$MOTIFINCDIR = "$FINKPREFIX/include";
	$MOTIFLIBS = "-L$FINKPREFIX/lib -L/usr/X11R6/lib -lXm -lXt -lX11";
	$OPENGLLIBS = "-lGLU -lGL";
	$GMTLIBDIR = "$FINKPREFIX/lib";
	$GMTINCDIR = "$FINKPREFIX/include";
	$NETCDFLIBDIR = "$FINKPREFIX/lib";
	$NETCDFINCDIR = "$FINKPREFIX/include";
	$LEVITUS = "$MBSYSTEM_HOME/share/mbsystem/LevitusAnnual82.dat";
	$PROJECTIONS = "$MBSYSTEM_HOME/share/mbsystem/Projections.dat";
	$FFTWLIBDIR = "$FINKPREFIX/lib";
	$FFTWINCDIR = "$FINKPREFIX/include";
	$OTPSDIR = "/usr/local/OTPSnc";
	$CFLAGS = "-O3 -funroll-loops -fexpensive-optimizations -I/usr/X11R6/include -Wall -Wimplicit -pedantic -W -Wstrict-prototypes -Wredundant-decls  -Wimplicit-int -Wimplicit-function-declaration -Wnested-externs";
	$LFLAGS = "-Wl -lm -bind_at_load";
	}
# 
# Preset configuration for MacOs X 10.6 (Snow Leopard) when building in /usr/local
#       assumes that GMT, netCDF, and OpenMotif are installed using fink in 32 bit mode
elsif ($preset eq "macosxusrlocal")
	{
	print "\nUsing preset configuration for MacOs X in /usr/local/...\n";
	$FINKPREFIX = "/sw";
	$MBSYSTEM_HOME = "/usr/local/mbsystem";
	$OS = "DARWIN";
	$CC = "cc";
	$MOTIFINCDIR = "$FINKPREFIX/include";
	$MOTIFLIBS = "-L$FINKPREFIX/lib -L/usr/X11R6/lib -lXm -lXt -lX11";
	$OPENGLLIBS = "-lGLU -lGL";
	$GMTLIBDIR = "$FINKPREFIX/lib";
	$GMTINCDIR = "$FINKPREFIX/include";
	$NETCDFLIBDIR = "$FINKPREFIX/lib";
	$NETCDFINCDIR = "$FINKPREFIX/include";
	$LEVITUS = "$MBSYSTEM_HOME/share/LevitusAnnual82.dat";
	$PROJECTIONS = "$MBSYSTEM_HOME/share/Projections.dat";
	$FFTWLIBDIR = "$FINKPREFIX/lib";
	$FFTWINCDIR = "$FINKPREFIX/include";
	$OTPSDIR = "/usr/local/OTPSnc";
	$CFLAGS = "-m32 -g -I/usr/X11R6/include";
	$LFLAGS = "-m32 -Wl -lm";
	}
# 
# Preset configuration for MacOs X 10.5 (Leopard) when building in /usr/local
#       assumes that GMT, netCDF, and OpenMotif are installed using fink
elsif ($preset eq "macosx10.5usrlocal")
	{
	print "\nUsing preset configuration for MacOs X in /usr/local/...\n";
	$FINKPREFIX = "/sw";
	$MBSYSTEM_HOME = "/usr/local/mbsystem";
	$OS = "DARWIN";
	$CC = "cc";
	$MOTIFINCDIR = "$FINKPREFIX/include";
	$MOTIFLIBS = "-L$FINKPREFIX/lib -L/usr/X11R6/lib -lXm -lXt -lX11";
	$OPENGLLIBS = "-lGLU -lGL";
	$GMTLIBDIR = "$FINKPREFIX/lib";
	$GMTINCDIR = "$FINKPREFIX/include";
	$NETCDFLIBDIR = "$FINKPREFIX/lib";
	$NETCDFINCDIR = "$FINKPREFIX/include";
	$LEVITUS = "$MBSYSTEM_HOME/share/LevitusAnnual82.dat";
	$PROJECTIONS = "$MBSYSTEM_HOME/share/Projections.dat";
	$FFTWLIBDIR = "$FINKPREFIX/lib";
	$FFTWINCDIR = "$FINKPREFIX/include";
	$OTPSDIR = "/usr/local/OTPSnc";
	$CFLAGS = "-g -I/usr/X11R6/include";
	$LFLAGS = "-Wl -lm -bind_at_load -Wl,-dylib_file,/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib:/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/LibGL.dylib";
	}
# 
# Preset configuration for Cygwin on Windows when building in /usr/local
#       assumes that GMT and netCDF are also installed in /usr/local
elsif ($preset eq "CYGWIN")
	{
	print "\nUsing preset configuration for Cygwin on Windows in /usr/local...\n";
	$MBSYSTEM_HOME = "/usr/local/mbsystem";
	$OS = "CYGWIN";
	$CC = "gcc";
	$MOTIFINCDIR = "/usr/X11R6/include/Xm";
	$MOTIFLIBS = "-L/usr/X11R6/lib -lXm -lXt -lX11";
	$OPENGLLIBS = "-lGLU -lGL";
	$GMTLIBDIR = "/usr/local/gmt/lib";
	$GMTINCDIR = "/usr/local/gmt/include";
	$NETCDFLIBDIR = "/usr/local/netcdf/lib";
	$NETCDFINCDIR = "/usr/local/netcdf/include";
	$LEVITUS = "$MBSYSTEM_HOME/share/LevitusAnnual82.dat";
	$PROJECTIONS = "$MBSYSTEM_HOME/share/Projections.dat";
	$FFTWLIBDIR = "/usr/local/fftw/lib";
	$FFTWINCDIR = "/usr/local/fftw/include";
	$OTPSDIR = "/usr/local/OTPSnc";
	$CFLAGS = "-g -fdata-sections -I/usr/X11R6/include";
	$LFLAGS = "-lrpc -lrpclib -lm -Wl,--enable-runtime-pseudo-reloc";
	}
# 
# Preset configuration for SGI Irix when building in /usr/local
#       assumes that GMT and netCDF are installed in /usr/local
elsif ($preset eq "irixusrlocal")
	{
	print "\nUsing preset configuration for SGI Irix in /usr/local/...\n";
	$MBSYSTEM_HOME = "/usr/local/mbsystem";
	$OS = "IRIX64";
	$CC = "cc";
	$MOTIFINCDIR = "/usr/include/Xm";
	$MOTIFLIBS = "-lXm -lXt -lX11";
	$OPENGLLIBS = "-lGLU -lGL";
	$GMTLIBDIR = "/usr/local/gmt/src";
	$GMTINCDIR = "/usr/local/gmt/src";
	$NETCDFLIBDIR = "/usr/local/netcdf/lib";
	$NETCDFINCDIR = "/usr/local/netcdf/include";
	$LEVITUS = "$MBSYSTEM_HOME/share/LevitusAnnual82.dat";
	$PROJECTIONS = "$MBSYSTEM_HOME/share/Projections.dat";
	$FFTWLIBDIR = "/usr/local/fftw/lib";
	$FFTWINCDIR = "/usr/local/fftw/include";
	$OTPSDIR = "/usr/local/OTPSnc";
	$CFLAGS = "-O2";
	$LFLAGS = "-lm -lsun";
	}
	
# Quit because preset not recognized:
else
	{
	print "\nUnrecognized preset $preset specified...\n";
	print "The currently supported preset configurations include:\n\tmacosfink\n\tmacosusrlocal\n";
	die "Exiting without creating new Makefiles...\n";
	}

#--------------------------------------------------------------------
# HERE ARE SOME EXAMPLE CONFIGURATION SETTINGS FOR VARIOUS SYSTEMS
#--------------------------------------------------------------------
#
# Example for installing on Apple Macintosh Computers
# running MacOS X version 10.2 to 10.4. Here I am using the 
# Apple X11 distribution and the OpenMotif (www.openmotif.org)
# port provided by Fink (www.fink.org). GMT and netCDF have
# also been installed using fink (note: current versions of
# GMT are only available through the "unstable" fink tree).
# The "-force_flat_namespace" linker flag was
# required in $LFLAGS prior to 10.2.6
#	$MBSYSTEM_HOME = "cmbsystem";
#	$OS = "DARWIN";
#	$CC = "cc";
#	$MOTIFINCDIR = "/sw/include";
#	$MOTIFLIBS = "-L/sw/lib -L/usr/X11R6/lib -lXm -lXt -lX11";
#	$GMTLIBDIR = "/sw/lib";
#	$GMTINCDIR = "/sw/include";
#	$NETCDFLIBDIR = "/sw/lib";
#	$NETCDFINCDIR = "/sw/include";
#	$LEVITUS = "$MBSYSTEM_HOME/share/LevitusAnnual82.dat";
#	$PROJECTIONS = "$MBSYSTEM_HOME/share/Projections.dat";
#	$FFTWLIBDIR = "/usr/local/fftw/lib";
#	$FFTWINCDIR = "/usr/local/fftw/include";
#	$OTPSDIR = "/usr/local/OTPSnc";
#	$CFLAGS = "-g -I/usr/X11R6/include";
#	$LFLAGS = "-Wl -lm -bind_at_load";
#
# Example for installing on Apple Macintosh Computers
# running MacOS X version 10.5 ("Leopard"). Here I am using the 
# Apple X11 distribution and the OpenMotif (www.openmotif.org)
# port provided by Fink (www.fink.org). GMT and netCDF have
# also been installed using fink (note: current versions of
# GMT are only available through the "unstable" fink tree).
#	$MBSYSTEM_HOME = "/usr/local/mbsystem";
#	$OS = "DARWIN";
#	$CC = "cc";
#	$MOTIFINCDIR = "/sw/include";
#	$MOTIFLIBS = "-L/sw/lib -L/usr/X11R6/lib -lXm -lXt -lX11";
#	$GMTLIBDIR = "/sw/lib";
#	$GMTINCDIR = "/sw/include";
#	$NETCDFLIBDIR = "/sw/lib";
#	$NETCDFINCDIR = "/sw/include";
#	$LEVITUS = "$MBSYSTEM_HOME/share/LevitusAnnual82.dat";
#	$PROJECTIONS = "$MBSYSTEM_HOME/share/Projections.dat";
#	$FFTWLIBDIR = "/usr/local/fftw/lib";
#	$FFTWINCDIR = "/usr/local/fftw/include";
#	$OTPSDIR = "/usr/local/OTPSnc";
#	$CFLAGS = "-g -I/usr/X11R6/include";
#	$LFLAGS = "-Wl -lm -bind_at_load -Wl,-dylib_file,/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib:/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/LibGL.dylib";
#
# Example for installing on L-DEO Sun workstations running SunOs 4.1.x:
#	$MBSYSTEM_HOME = "/home/hs/caress/dev/mbsystem";
#	$OS = "SUN";
#	$CFLAGS = "-O2";
#	$LFLAGS = "-lm";
#	$MOTIFINCDIR = "/usr/include/Xm";
#	$MOTIFLIBS = "-lXm -lXt -lX11";
#	$GMTLIBDIR = "/ocean/mgg/gmt2/sun4/lib";
#	$GMTINCDIR = "/ocean/mgg/gmt2/src";
#	$NETCDFLIBDIR = "/ocean/mgg/netcdf/sun4/lib";
#	$NETCDFINCDIR = "/ocean/mgg/netcdf/include";
#	$LEVITUS = "/ocean/mgg/levitus/annual";
#	$PROJECTIONS = "$MBSYSTEM_HOME/share/Projections.dat";
#	$FFTWLIBDIR = "/usr/local/fftw/lib";
#	$FFTWINCDIR = "/usr/local/fftw/include";
#	$OTPSDIR = "/usr/local/OTPSnc";
#	$CFLAGS = "-O2";
#	$LFLAGS = "-lm";
#
# Example for installing on R/V Ewing Sun workstations:
#	$MBSYSTEM_HOME = "/net/heezen/packages/mbsystem";
#	$OS = "SUN";
#	$CFLAGS = "-O2";
#	$LFLAGS = "-lm";
#	$MOTIFINCDIR = "/usr/include/Xm";
#	$MOTIFLIBS = "-lXm -lXt -lX11";
#	$GMTLIBDIR = "/net/heezen/packages/gmt2/bin";
#	$GMTINCDIR = "/net/heezen/packages/gmt2/src";
#	$NETCDFLIBDIR = "/net/heezen/packages/netcdf/lib";
#	$NETCDFINCDIR = "/net/heezen/packages/netcdf/include";
#	$LEVITUS = "/net/heezen/packages/levitus/annual";
#	$PROJECTIONS = "$MBSYSTEM_HOME/share/Projections.dat";
#	$OTPSDIR = "/usr/local/OTPSnc";
#	$FFTWLIBDIR = "/usr/local/fftw/lib";
#	$FFTWINCDIR = "/usr/local/fftw/include";
#
# Example for installing on Sun workstations running Solaris 2.4:
#
#	$MBSYSTEM_HOME = "/usr/local/mbsystem";
#	$OS = "SOLARIS";
#	$CFLAGS = "-O -I/usr/openwin/include -I/usr/ucbinclude";
#	$LFLAGS = "-lnsl -lm";
#	$MOTIFINCDIR = "/usr/dt/include";
#	$MOTIFLIBS = "-L/usr/openwin/lib -L/usr/dt/lib -lXm -lXt -lX11";
#	$GMTLIBDIR = "/usr/local/gmt/src";
#	$GMTINCDIR = "/usr/local/gmt/src";
#	$NETCDFLIBDIR = "/usr/local/netcdf/lib";
#	$NETCDFINCDIR = "/usr/local/netcdf/include";
#	$LEVITUS = "/usr/local/levitus/annual";
#	$PROJECTIONS = "$MBSYSTEM_HOME/share/Projections.dat";
#	$OTPSDIR = "/usr/local/OTPSnc";
#	$FFTWLIBDIR = "/usr/local/fftw/lib";
#	$FFTWINCDIR = "/usr/local/fftw/include";
#
# Example for installing on Sun workstations running Solaris 2.6
#   with GMT built to utilize sincos() - add -lsunmath to $LFLAGS:
#
#	$MBSYSTEM_HOME = "/usr/local/mbsystem";
#	$OS = "SOLARIS";
#	$CC = "cc";
#	$MOTIFINCDIR = "/usr/dt/include";
#	$MOTIFLIBS = "-L/usr/openwin/lib -L/usr/dt/lib -lXm -lXt -lX11";
#	$GMTLIBDIR = "/usr/local/gmt/src";
#	$GMTINCDIR = "/usr/local/gmt/src";
#	$NETCDFLIBDIR = "/usr/local/netcdf/lib";
#	$NETCDFINCDIR = "/usr/local/netcdf/include";
#	$LEVITUS = "/usr/local/levitus/annual";
#	$PROJECTIONS = "$MBSYSTEM_HOME/share/Projections.dat";
#	$FFTWLIBDIR = "/usr/local/fftw/lib";
#	$FFTWINCDIR = "/usr/local/fftw/include";
#	$OTPSDIR = "/usr/local/OTPSnc";
#	$CFLAGS = "-O -I/usr/openwin/include -I/usr/ucbinclude";
#	$LFLAGS = "-lnsl -lm -lsunmath";
#
# Example for installing on Sun workstations running Solaris 2.7
#
#	$MBSYSTEM_HOME = "/usr/local/mbsystem";
#	$OS = "SOLARIS";
#	$CC = "gcc";
#	$MOTIFINCDIR = "/usr/dt/include/Xm";
#	$MOTIFLIBS = "-L/usr/dt/lib -lXm -lXt -lX11";
#	$OPENGLLIBS = "-lGLU -lGL";
#	$GMTLIBDIR = "/usr/local/GMT4.1/lib";
#	$GMTINCDIR = "/usr/local/GMT4.1/include";
#	$NETCDFLIBDIR = "/usr/local/netcdf-3.4/lib";
#	$NETCDFINCDIR = "/usr/local/netcdf-3.4/include";
#	$LEVITUS = "/usr/local/mbsystem/annual";
#	$PROJECTIONS = "$MBSYSTEM_HOME/share/Projections.dat";
#	$FFTWLIBDIR = "/usr/local/fftw/lib";
#	$FFTWINCDIR = "/usr/local/fftw/include";
#	$OTPSDIR = "/usr/local/OTPSnc";
#	$CFLAGS = "-O -I/usr/openwin/share/include";
#	$LFLAGS = "-lnsl -lm";
#
# Example for installing on SeaBeam Instruments SGI IRIX workstations:
#	$MBSYSTEM_HOME = "/usr/local/mbsystem";
#	$OS = "IRIX";
#	$CFLAGS = "-O2";
#	$LFLAGS = "-lm -lsun";
#	$MOTIFINCDIR = "/usr/include/Xm";
#	$MOTIFLIBS = "-lXm -lXt -lX11";
#	$GMTLIBDIR = "/usr/local/gmt/src";
#	$GMTINCDIR = "/usr/local/gmt/src";
#	$NETCDFLIBDIR = "/usr/local/netcdf/lib";
#	$NETCDFINCDIR = "/usr/local/netcdf/include";
#	$LEVITUS = "/usr/local/levitus/annual";
#	$PROJECTIONS = "$MBSYSTEM_HOME/share/Projections.dat";
#	$OTPSDIR = "/usr/local/OTPSnc";
#	$FFTWLIBDIR = "/usr/local/fftw/lib";
#	$FFTWINCDIR = "/usr/local/fftw/include";
#	$OTPSDIR = "/usr/local/OTPSnc";
#
# Example for installing on SeaBeam Instruments SGI IRIX 6.4 workstations:
#	$MBSYSTEM_HOME = "/usr/local/mbsystem";
#	$OS = "IRIX64";
#	$CFLAGS = "-O2";
#	$LFLAGS = "-lm -lsun";
#	$MOTIFINCDIR = "/usr/include/Xm";
#	$MOTIFLIBS = "-lXm -lXt -lX11";
#	$GMTLIBDIR = "/usr/local/gmt/src";
#	$GMTINCDIR = "/usr/local/gmt/src";
#	$NETCDFLIBDIR = "/usr/local/netcdf/lib";
#	$NETCDFINCDIR = "/usr/local/netcdf/include";
#	$LEVITUS = "/usr/local/levitus/annual";
#	$PROJECTIONS = "$MBSYSTEM_HOME/share/Projections.dat";
#	$FFTWLIBDIR = "/usr/local/fftw/lib";
#	$FFTWINCDIR = "/usr/local/fftw/include";
#	$OTPSDIR = "/usr/local/OTPSnc";
#
# Example for installing on PC running LINUX:
#       $MBSYSTEM_HOME = "/usr/local/mbsystem";
#       $OS = "LINUX";
#       $CC = "cc";
#       $CFLAGS = "-g -w";
#       $LFLAGS = "-L$NETCDFLIBDIR -lm -lnetcdf";
#       $GRAPHICAL = "YES";
#       $MOTIFINCDIR = "/usr/X11R6/LessTif/Motif1.2/include";
#       $MOTIFLIBS = "-L/usr/X11R6/LessTif/Motif1.2/lib -lXm -L/usr/X11R6/lib -lXt -lX11";
#       $GMTLIBDIR = "/usr/local/GMT4.1/lib";
#       $GMTINCDIR = "/usr/local/GMT4.1/include";
#       $NETCDFLIBDIR = "/usr/local/netcdf-3.5.0/lib";
#       $NETCDFINCDIR = "/usr/local/netcdf-3.5.0/include";
#       $LEVITUS = "/usr/local/levitus/annual";
#	$PROJECTIONS = "$MBSYSTEM_HOME/share/Projections.dat";
#	$FFTWLIBDIR = "/usr/local/fftw/lib";
#	$FFTWINCDIR = "/usr/local/fftw/include";
#	$OTPSDIR = "/usr/local/OTPSnc";
#
# Example for installing on Hewlett Packard workstations
# running HPUX version 9.0X or later:
#	$MBSYSTEM_HOME = "/usr/local/mbsystem";
#	$OS = "HPUX";
#	$CFLAGS = "-Ae +O3 -I/usr/include/X11R5 -I/usr/include/Motif1.2";
#	$LFLAGS = "-lm ";
#	$MOTIFINCDIR = "/usr/lib/Motif1.2/Xm";
#	$MOTIFLIBS = "-Wl,-L/usr/lib/Motif1.2 -Wl, -Wl,-lXm -Wl,-L/usr/lib/X11R5 -Wl,-lX11 -Wl,-lXt";
#	$GMTLIBDIR = "/usr/local/gmt/src";
#	$GMTINCDIR = "/usr/local/gmt/src";
#	$NETCDFLIBDIR = "/usr/local/gmt/lib";
#	$NETCDFINCDIR = "/usr/local/gmt/include";
#	$LEVITUS = "/usr/local/levitus/annual";
#	$PROJECTIONS = "$MBSYSTEM_HOME/share/Projections.dat";
#	$FFTWLIBDIR = "/usr/local/fftw/lib";
#	$FFTWINCDIR = "/usr/local/fftw/include";
#	$OTPSDIR = "/usr/local/OTPSnc";
#
#--------------------------------------------------------------------
#
# **** LEAVE EVERYTHING ELSE BELOW ALONE ****

# Program Name:
$program_name = "install_makefiles";

# check for recognized operating system
if (!$OS)
	{
	$OS = "OTHER";
	}
elsif ($OS ne "IRIX"
	&& $OS ne "IRIX64"
	&& $OS ne "SOLARIS"
	&& $OS ne "LINUX"
	&& $OS ne "LYNX"
	&& $OS ne "SUN"
	&& $OS ne "HPUX"
	&& $OS ne "DARWIN"
	&& $OS ne "CYGWIN"
	&& $OS ne "OTHER")
	{
	$OS = "OTHER";
	}

# add operating system to CFLAGS
$CFLAGS = "$CFLAGS -D$OS";

# add netcdf libraries to LFLAGS
$LFLAGS = "$LFLAGS -L$NETCDFLIBDIR -lnetcdf";

# add byte swapping to CFLAGS if necessary
$BYTESWAPTESTSTRING = join(" ", map { sprintf "%#02x", $_ } unpack("C*",pack("L",0x12345678))), "\n";
if ($BYTESWAPTESTSTRING eq "0x78 0x56 0x34 0x12")
	{
	$BYTESWAPPED = "YES";
	$CFLAGS = "$CFLAGS -DBYTESWAPPED";
	}
	
# add __APPLE__ flag to CFLAGS to allow GSF code to build under OSX
if ($OS eq "DARWIN" || $OS eq "CYGWIN")
	{
	$CFLAGS = "$CFLAGS -D__APPLE__";
	}

# set archiver 
if ($OS eq "LYNX")
	{
	$ARCHIVE = "libr rcv";
	}
else
	{
	$ARCHIVE = "ar rcv";
	}

# set $GRAPHICAL according to Motif settings
if ($MOTIFINCDIR && $MOTIFLIBS)
	{
	$GRAPHICAL = "YES";
	}
else
	{
	$GRAPHICAL = "NO";
	}

# set $BIGFLAG for compiling mbgrid.c under IRIX
if ($OS eq "IRIX")
	{
	$BIGFLAG = "-Olimit 2000";
	}
elsif ($OS eq "IRIX64")
	{
	$BIGFLAG = "-OPT:Olimit=5000";
	$OS = "IRIX";
	}
else
	{
	$BIGFLAG = "";
	}

# outputting attributes:
print "\nParameters defined for Makefiles:\n";
print "Mb-System Home:            $MBSYSTEM_HOME\n";
print "Operating System:          $OS\n";
print join(" ", map { sprintf "%#02x", $_ } unpack("C*",pack("L",0x12345678))), "\n";
if ($BYTESWAPPED eq "YES")
	{
	print "Byte swapping:             enabled\n";
	}
else
	{
	print "Byte swapping:             disabled\n";
	}
if ($CC)
	{
	print "C Compiler:                $CC\n";
	}
print "C Compile Flags:           $CFLAGS\n";
print "Load Flags:                $LFLAGS\n";
print "Library Archiver:          $ARCHIVE\n";
if ($GRAPHICAL eq "YES")
	{
	print "Graphical Utilities:       enabled\n";
	}
else
	{
	print "Graphical Utilities:       disabled\n";
	}
print "Motif Include Location:    $MOTIFINCDIR\n";
print "Motif Libraries:           $MOTIFLIBS\n";
print "OpenGL Libraries:          $OPENGLLIBS\n";
print "GMT Library Location:      $GMTLIBDIR\n";
print "GMT Include Location:      $GMTINCDIR\n";
print "NetCDF Library Location:   $NETCDFLIBDIR\n";
print "NetCDF Include Location:   $NETCDFINCDIR\n";
if ($FFTWLIBDIR)
	{
	print "FFTW Library Location:     $FFTWLIBDIR\n";
	}
else
	{
	print "FFTW Library Location:     Undefined - mbsegypsd will not be built!\n";
	}
if ($FFTWINCDIR)
	{
	print "FFTW Include Location:     $FFTWINCDIR\n";
	}
else
	{
	print "FFTW Include Location:     Undefined - mbsegypsd will not be built!\n";
	}
if ($OTPSDIR)
	{
	print "OTPSnc Installation:       $OTPSDIR\n";
	}
else
	{
	print "OTPSnc Installation:       Undefined - mbotps will not work!\n";
	}
print "Levitus Data Location:     $LEVITUS\n";
print "Projections Data Location: $PROJECTIONS\n";

# make sure that that the following directories exist:
#      mbsystem/bin mbsystem/lib mbsystem/include 
if (! -d "bin")
	{
	`mkdir bin`;
	print "Created directory: $MBSYSTEM_HOME/bin\n";
	}
if (! -d "lib")
	{
	`mkdir lib`;
	print "Created directory: $MBSYSTEM_HOME/lib\n";
	}
if (! -d "include")
	{
	`mkdir include`;
	print "Created directory: $MBSYSTEM_HOME/include\n";
	}

# get filenames
@files = `ls Makefile.template src/Makefile.template src/*/Makefile.template`;
while (@files)
	{
	# get files
	$template = shift @files;
	chop($template);
	print "\nMakefile Template: $template\n";
	$pos = 0;
	while (($tpos = index($template,"/",$pos)) >= 0)
		{
		$pos = $tpos + 1;
		}
	$path = substr($template,0,$pos);
	chop($path);
	if (length($path) == 0)
		{
		$path = ".";
		}
	$makefile = "$path/Makefile";
	print "Output Makefile:   $makefile\n";

	# open files
	open(TMP,"<$template") || die "Cannot open template Makefile $template\nMacro $program_name aborted.\n";
	open(MAK,">$makefile") || die "Cannot open output Makefile $makefile\nMacro $program_name aborted.\n";

	# loop through the file
	while (<TMP>)
		{
		if (index($_,"MBSYSTEM_HOME",0) == 0)
			{
			print MAK "MBSYSTEM_HOME = $MBSYSTEM_HOME\n";
			}
		elsif (index($_,"OS",0) == 0)
			{
			print MAK "OS = $OS\n";
			}
		elsif (index($_,"CC",0) == 0)
			{
			if ($CC)
				{
				print MAK "CC = $CC\n";
				}
			else
				{
				print MAK "# C compiler left as default\n";
				}
			}
		elsif (index($_,"CFLAGS",0) == 0)
			{
			print MAK "CFLAGS = $CFLAGS\n";
			}
		elsif (index($_,"LFLAGS",0) == 0)
			{
			print MAK "LFLAGS = $LFLAGS\n";
			}
		elsif (index($_,"MOTIFINCDIR",0) == 0)
			{
			print MAK "MOTIFINCDIR = $MOTIFINCDIR\n";
			}
		elsif (index($_,"MOTIFLIBS",0) == 0)
			{
			print MAK "MOTIFLIBS = $MOTIFLIBS\n";
			}
		elsif (index($_,"OPENGLLIBS",0) == 0)
			{
			print MAK "OPENGLLIBS = $OPENGLLIBS\n";
			}
		elsif (index($_,"GMTLIBDIR",0) == 0)
			{
			print MAK "GMTLIBDIR = $GMTLIBDIR\n";
			}
		elsif (index($_,"GMTINCDIR",0) == 0)
			{
			print MAK "GMTINCDIR = $GMTINCDIR\n";
			}
		elsif (index($_,"NETCDFLIBDIR",0) == 0)
			{
			print MAK "NETCDFLIBDIR = $NETCDFLIBDIR\n";
			}
		elsif (index($_,"NETCDFINCDIR",0) == 0)
			{
			print MAK "NETCDFINCDIR = $NETCDFINCDIR\n";
			}
		elsif (index($_,"FFTWLIBDIR",0) == 0)
			{
			if ($FFTWLIBDIR)
				{
				print MAK "FFTWLIBDIR = $FFTWLIBDIR\n";
				}
			}
		elsif (index($_,"FFTWINCDIR",0) == 0)
			{
			if ($FFTWINCDIR)
				{
				print MAK "FFTWINCDIR = $FFTWINCDIR\n";
				}
			}
		elsif (index($_,"OTPSDIR",0) == 0)
			{
			if ($OTPSDIR)
				{
				print MAK "OTPSDIR = $OTPSDIR\n";
				}
			}
		elsif ($GSFINCDIR && index($_,"GSFINCDIR",0) == 0)
			{
			print MAK "GSFINCDIR = $GSFINCDIR\n";
			}
		elsif ($GSFLIB && index($_,"GSFLIB",0) == 0)
			{
			print MAK "GSFLIB = $GSFLIB\n";
			}
		elsif ($PROJINCDIR && index($_,"PROJINCDIR",0) == 0)
			{
			print MAK "PROJINCDIR = $PROJINCDIR\n";
			}
		elsif ($PROJLIB && index($_,"PROJLIB",0) == 0)
			{
			print MAK "PROJLIB = $PROJLIB\n";
			}
		elsif (index($_,"LEVITUS",0) == 0)
			{
			print MAK "LEVITUS = $LEVITUS\n";
			}
		elsif (index($_,"PROJECTIONS",0) == 0)
			{
			print MAK "PROJECTIONS = $PROJECTIONS\n";
			}
		elsif (index($_,"ARCHIVE",0) == 0)
			{
			print MAK "ARCHIVE = $ARCHIVE\n";
			}
		elsif (index($_,"BIGFLAG",0) == 0)
			{
			print MAK "BIGFLAG = $BIGFLAG\n";
			}
		elsif (index($_,"mbsegypsd",0) >= 0 && (!$FFTWLIBDIR || !$FFTWINCDIR))
			{
			}
		elsif (index($_,"fftw",0) >= 0 && (!$FFTWLIBDIR || !$FFTWINCDIR))
			{
			}
		elsif (index($_,"ranlib",0) >= 0 && $OS ne "SUN" && $OS ne "DARWIN")
			{
			}
		elsif (index($_,"catman",0) >= 0 && $OS ne "SUN")
			{
			}
		elsif (index($_,"cd gsf",0) >= 0 && $GSFLIB && $GSFINCDIR)
			{
			}
		elsif (index($_,"cd proj",0) >= 0 && $PROJLIB && $PROJINCDIR)
			{
			}
		elsif (index($_,"cd mbedit",0) >= 0 && $GRAPHICAL ne "YES")
			{
			}
		elsif (index($_,"cd mbeditold",0) >= 0 && $GRAPHICAL ne "YES")
			{
			}
		elsif (index($_,"cd mbvelocitytool",0) >= 0 && $GRAPHICAL ne "YES")
			{
			}
		elsif (index($_,"cd mbnavedit",0) >= 0 && $GRAPHICAL ne "YES")
			{
			}
		elsif (index($_,"cd mbnaveditold",0) >= 0 && $GRAPHICAL ne "YES")
			{
			}
		elsif (index($_,"cd mbnavadjust",0) >= 0 && $GRAPHICAL ne "YES")
			{
			}
		elsif (index($_,"cd mbview",0) >= 0 
					&& ($GRAPHICAL ne "YES" 
						|| !$OPENGLLIBS
						|| ! -e "src/mbview"))
			{
			}
		else
			{
			print MAK "$_";
			}
		}

	# close files
	close TMP;
	close MAK;

	}


print "\nAll done!\n";
exit 0;

#-----------------------------------------------------------------------
# This version of Getopts has been augmented to support multiple
# calls to the same option. If an arg in argumentative is followed
# by "+" rather than ":",  then the corresponding scalar will
# be concatenated rather than overwritten by multiple calls to
# the same arg.
#
# Usage:
#      do Getopts('a:b+c'); # -a takes arg, -b concatenates args,  
#			    # -c does not take arg. Sets opt_* as a
#                           # side effect.

sub MBGetopts {
    local($argumentative) = @_;
    local(@args,$_,$first,$rest);
    local($errs) = 0;
    local($[) = 0;

    @args = split( / */, $argumentative );
    while(@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
	($first,$rest) = ($1,$2);
	$pos = index($argumentative,$first);
	if($pos >= $[) {
	    if($args[$pos+1] eq ':') {
		shift(@ARGV);
		if($rest eq '') {
		    ++$errs unless @ARGV;
		    $rest = shift(@ARGV);
		}
		eval "\$opt_$first = \$rest;";
		eval "\$flg_$first = 1;";
	    }
	    elsif($args[$pos+1] eq '+') {
		shift(@ARGV);
		if($rest eq '') {
		    ++$errs unless @ARGV;
		    $rest = shift(@ARGV);
		}
		if (eval "\$opt_$first") {
		    eval "\$opt_$first = \$opt_$first 
				. \":\" . \$rest;";
		}
		else {
		    eval "\$opt_$first = \$rest;";
		}
		eval "\$flg_$first = 1;";
	    }
	    elsif($args[$pos+1] eq '%') {
		shift(@ARGV);
		if($rest ne '') {
		    eval "\$opt_$first = \$rest;";
		}
		else {
		    $rest = $ARGV[0];
		    ($one) = $rest =~ /^-(.).*/;
		    $pos = index($argumentative,$one);
		    if(!$one || $pos < $[) {
			eval "\$opt_$first = \$rest;";
			shift(@ARGV);
		    }
		}
		eval "\$flg_$first = 1;";
	    }
	    else {
		eval "\$opt_$first = 1";
		eval "\$flg_$first = 1;";
		if($rest eq '') {
		    shift(@ARGV);
		}
		else {
		    $ARGV[0] = "-$rest";
		}
	    }
	}
	else {
	    print STDERR "Unknown option: $first\n";
	    ++$errs;
	    if($rest ne '') {
		$ARGV[0] = "-$rest";
	    }
	    else {
		shift(@ARGV);
	    }
	}
    }
    $errs == 0;
}
#-----------------------------------------------------------------------

