#!/bin/sh
#################################################################
# This UNIX script builds the NetCDF (network Common Data Form) #
# library -- a machine-independent format for the creation,     #
# access, and sharing of scientific data -- under Mac OS X.     #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# http://my.unidata.ucar.edu/content/software/netcdf/index.html #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Notes:                                                        #
#    1. Make this script executable:  "chmod +x macosx_abs.sh"  #
#    2. Build as root:  "sudo ./macosx-abs.sh"                  #
#    3. Tested on:  Mac OS X Panther 10.4.2,                    #
#       Absoft 9.2 Fortran compilers,                           #
#       GCC version 4.0 C/C++ compilers                         #
#    4. Last modified:  Aug. 26, 2005                           #
#    5. Based on script provided by Craig Mattocks              #
#################################################################

#####################################
# Set trap to allow abort on signal #
#####################################
trap 'echo "Ouch. Dude! Ya fragged me." 1>&2; exit' 1 2 3 15

########################
# Specify source name: #
########################
export SOURCE="netcdf-3.6.1-beta3"

########################################
# Specify top-level install directory: #
########################################
export NETCDFHOME="/usr/local/netcdf"	# Change?
if test ! -d ${NETCDFHOME}; then
	mkdir -p ${NETCDFHOME}
fi

###################################
# Specify location for man pages: #
###################################
export MANPAGES="/usr/share/man"	# Change?
# export MANPAGES=${NETCDFHOME}/man

if test ! -d ${MANPAGES}; then
	mkdir -p ${MANPAGES}
fi
n=1
while [ $n -le 9 ]
do
	if test ! -d ${MANPAGES}/man$n; then
		mkdir -p ${MANPAGES}/man$n
	fi
	if test ! -d ${MANPAGES}/cat$n; then
		mkdir -p ${MANPAGES}/cat$n
	fi
	n=`expr $n + 1`
done

#############
# Greetings #
#############
echo "============================="
echo "NetCDF installer for Mac OS X"
echo "============================="
echo "USAGE: sudo ./macosx-abs.sh"

##################
# Rock 'n' roll! #
##################
echo "---------------------------------------------"
echo "1. Downloading NetCDF source from the Unidata"
echo "   Program Center in Boulder, Colorado ...   "
echo "---------------------------------------------"
curl ftp://ftp.unidata.ucar.edu/pub/netcdf/netcdf-3.6.1-beta3.tar.gz -o ${SOURCE}.tar.gz

echo "... done."

echo "----------------------------------"
echo "2. Uncompressing NetCDF source ..."
echo "----------------------------------"
gnutar -xzvf ${SOURCE}.tar.gz
echo "... done."

echo "----------------------------------"
echo "3. Configuring the environment ..."
echo "----------------------------------"
export MAKE=/usr/bin/gnumake   # gnumake supports "include" syntax in makefiles

export CC='/usr/bin/cc'
export CXX='/usr/bin/c++'
export CFLAGS='-m64 -mpowerpc64 -mcpu=powerpc64 -mtune=powerpc64 -fPIC -fast'
export CXXFLAGS='-m64 -mpowerpc64 -mcpu=powerpc64 -mtune=powerpc64 -fPIC -fast'
export CPPFLAGS='-DAbsoftProFortran'
export FC='/Applications/Absoft/bin/f90'
export FFLAGS='-m64 -w'
export F90='/Applications/Absoft/bin/f90'
export F90FLAGS=' -m64 -w'
export FLIBS='-lU77'

cd $SOURCE/src/
./configure --prefix=$NETCDFHOME

echo "... done.  See config.log file for details."

echo "---------------------------------------------"
echo "4. Preparing Makefile for building NetCDF ..."
echo "---------------------------------------------"
mv macros.make macros.orig
sed 's/$(prefix)\/man/$(MANPAGES)/' macros.orig > macros.make
echo "... done."

echo "----------------------"
echo "5. Building NetCDF ..."
echo "----------------------"
make
echo "... done."

echo "---------------------"
echo "6. Testing NetCDF ..."
echo "---------------------"
make test
echo "... done."

echo "------------------------"
echo "7. Installing NetCDF ..."
echo "------------------------"
make install
echo "... done."

echo "------------------"
echo "8. Cleaning up ..."
echo "------------------"
make clean
cd ..
echo "... All done!"

exit

