#!/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-xlf.sh"  #
#    2. Build as root:  "sudo ./macosx-xlf.sh"                  #
#    3. Tested on:  Mac OS X Panther 10.3.6,                    #
#       IBM xlf Fortran compilers,                              #
#       GCC version 3.3 C/C++ compilers                         #
#    4. Last modified:  Dec. 8, 2004                            #
#################################################################

#####################################
# 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.0-p1"

########################################
# Specify top-level install directory: #
########################################
export NETCDFHOME="/Users/ryanglover/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-xlf.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-beta.tar.Z -o netcdf-beta.tar.Z
#curl ftp://ftp.unidata.ucar.edu/pub/netcdf/${SOURCE}.tar.gz -o ${SOURCE}.tar.gz
echo "... done."

echo "----------------------------------"
echo "2. Uncompressing NetCDF source ..."
echo "----------------------------------"
#gnutar -xzvf netcdf-beta.tar.Z
echo "... done."

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

export CC=/usr/bin/gcc-3.3
export CPPFLAGS="-O -DNDEBUG -DIBMR2Fortran"
export CFLAGS="-O"
export CXX=/usr/bin/c++
export CXXFLAGS="-O"
 
export FC=/opt/ibmcmp/xlf/8.1/bin/xlf
export F77=/opt/ibmcmp/xlf/8.1/bin/xlf
export F90=/opt/ibmcmp/xlf/8.1/bin/xlf90
export FFLAGS="-O5 -qtune=auto -qarch=auto -qhot -qinitauto -qsave"
export F90FLAGS=-qsuffix=f=f90:cpp=F90
 

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



