#!/bin/sh 
# 
# This script is attempt to make it easier to build and install all the
# optional and non-optional external software packages needed for
# NCL, PyNGL, and PyNIO
# 
# You will need to read through the notes below, to:
# 
#   - set possible environment variables for your compilers and options,
#   - indicate which packages you want to install,
#   - edit some files by hand before you run this script.
# 
#  Search for lines that start with "### CHANGE". These are things
#  you will likely need to change.
#
# Software packages to install:
# 
# NCL/NCARG:
#   zlib-x.y.z             NCL:   required 
#                          NCARG: optional (needed for PNG, HDF4,
#                                           classic NetCDF-4)
#
#   jpeg-xx                NCL:   required 
#                          NCARG: optional (needed for HDF4)
#      Note: HDF4 folks used to warn against jpeg-7. We decided to
#          move from jpeg-6b to jpeg-9 during the NCL 6.4. release.
#          The HDF4 build page said they preferred people using their
#          own JPEG libraries anyway.
# 
#   libpng-x.y.z           NCL:   optional (needed for GRIB2 or PNG support)
#                          NCARG: optional (needed for PNG)
#                          
#   HDF-x.y.z              NCL:   required
#                          NCARG: optional 
# 
#   pkg-config-x.y         NCL/NCARG: optional (needed for cairo support)
#   expat-x.y.z            NCL/NCARG: optional (needed for cairo support)
#   freetype-x.y.z         NCL/NCARG: optional (needed for cairo support)
#   fontconfig-x.y.z       NCL/NCARG: optional (needed for cairo support)
#   pixman-x.y.z           NCL/NCARG: optional (needed for cairo support)
#   cairo-x.y.z            NCL/NCARG: optional (needed for cairo support)
#
# Note on cairo above: We've had problems with cairo-1.12.x (NCL-1860 
# and NCL-1862), so we've decided to stick with cairo-1.10.2 for 
# V6.2.x. Also, Rick recommends using the system fontconfig to avoid
# some quirky problems with that and the high-res map database (NCL-1210).
#
# NCL only:
#   hdf5-x.y.z             Required if you want classic NetCDF-4 support
#   szip-x.y.z             Required if you want classic NetCDF-4 support
#   curl-x.y.z             Required if you want OPeNDAP support in NetCDF
#   jasper-x.y.z           Required if you want GRIB2 support
#   g2clib-x.y.z           Required if you want GRIB2 support
#   gdal-x.y.z             Required if you want GDAL support
#   gsl-x.y.z              Required (for EEMD function)
#   netcdf-x.y.z           Required (classic NetCDF-4 support is optional)
#   proj-x.y.z             Required if you want GDAL support
#   udunits-x.y.z          Required if you want ut_calendar/ut_inv_calendar
#   hdfeos-x.y.z           Required if you want HDF-EOS 2 support
#   hdfeos5-x.y.z          Required if you want HDF-EOS 5 support
#   vis5d+-x.y.z           Required if you want v5d* functions
#   esmf-x.y.z             Required if you want ESMF regridding
#
#----------------------------------------------------------------------
# PyNGL/PyNIO
#   Python-x.y.z           Required
#   numpy-x.y.1            Required
#
#   NOTE: since it's likely that users have different python/numpy versions, the
#      Python and numpy packages will get installed in their own parent directory
#      under PREFIX, with a name like "python-x.y.z-numpy-x.y.z
#
#   PyNIO depends on many of the file I/O packages listed above.
#   PyNGL depends on NCL
#----------------------------------------------------------------------
#   These next packages should no longer be required because NetCDF-4.1
#   contains OPeNDAP. However, we keep them here because some of
#   these libraries (like xml2) might be needed by other
#   packages.
#
#   openssl-x.y.z          Required if you want OPeNDAP support
#   libxml2-x.y.z          Required if you want OPeNDAP support
#   libdap-x.y.z           Required if you want OPeNDAP support
#   libnc-dap-x.y.z        Required if you want OPeNDAP support
#
#
# PREFIX is the parent location for installing external software.
#
# PREFIX_NODAP is the parent location for installing a version
# of NetCDF that is not opendap-enabled.  This is done because
# the OPeNDAP version can have a lot of dependencies that make it
# hard for a user who doesn't have the correct shared libraries on
# their system. If you don't want to build the non-opendap version
# of NetCDF (and ESMF), then set BUILD_NON_OPENDAP to 0.
#
### CHANGE THE NEXT TWO LINES AS NEEDED
PREFIX=/usr/local
NCARG_ROOT=/usr/local

# PREFIX_NODAP is the parent location for installing a version
# of NetCDF that is not opendap-enabled.
# If you don't want to build the non-opendap version
# of NetCDF (and ESMF), then set BUILD_NON_OPENDAP to 0.

BUILD_NON_OPENDAP=1
PREFIX_NODAP=$PREFIX/nodap

# Set up file to hold build output.
dd=`date +'%m%d%y'`
un=`uname -n`
um=`uname -m`
os=`uname -s`

# Just in case you're on an NCAR super. May need to set more of these.
unset LIB_NCAR
unset INC_NCAR
unset NETCDF

# Get MacOS version
if [ "$os" = "Darwin" ] ; then
  os_version=`sw_vers -productVersion`
fi

# Set environment variables for building software
### Change to indicate what options you want for compilation.
if [ "$os" = "Darwin" ] ; then
  export CC=gcc
  export CXX=g++
  export FC=gfortran
  export F77=gfortran
  export F90=gfortran
elif [ "$un" = "yellowstone_gnu483" ] ; then
  export CC=/glade/u/apps/opt/gnu/4.8.3/bin/gcc
  export CXX=/glade/u/apps/opt/gnu/4.8.3/bin/g++
  export FC=/glade/u/apps/opt/gnu/4.8.3/bin/gfortran
  export F77=/glade/u/apps/opt/gnu/4.8.3/bin/gfortran
  export F90=/glade/u/apps/opt/gnu/4.8.3/bin/gfortran
elif [ "$un" = "yellowstone_gnu472" ] ; then
  export CC=/glade/u/apps/opt/gnu/4.7.2/bin/gcc
  export CXX=/glade/u/apps/opt/gnu/4.7.2/bin/g++
  export FC=/glade/u/apps/opt/gnu/4.7.2/bin/gfortran
  export F77=/glade/u/apps/opt/gnu/4.7.2/bin/gfortran
  export F90=/glade/u/apps/opt/gnu/4.7.2/bin/gfortran
elif [ "$un" = "yellowstone_gnu447" ] ; then
  export CC=/usr/bin/gcc
  export CXX=/usr/bin/g++
  export FC=/usr/bin/gfortran
  export F77=/usr/bin/gfortran
  export F90=/usr/bin/gfortran
elif [ "$un" = "yellowstone_intel1215" ] || [ "$un" = "yellowstone_intel1701" ]; then
  export CC=icc
  export CXX=icpc
  export FC=ifort
  export F77=ifort
  export F90=ifort
  export CFLAGS=
  export FFLAGS=
  export F90FLAGS=
  export CXXFLAGS=
else
  export CC=gcc
  export CXX=g++
  export FC=gfortran
  export F77=gfortran
  export F90=gfortran
fi
if [ "$os" = "Darwin" ] ; then
  export CFLAGS='-fPIC -m64 -Df2cFortran'
  export FFLAGS='-m64 -fPIC'
  export F90FLAGS='-m64 -fPIC'
  export CXXFLAGS='-m64 -fPIC'
else
  export CFLAGS=-fPIC
  export FFLAGS=-fPIC
  export F90FLAGS=-fPIC
  export CXXFLAGS=-fPIC
fi

if [ "$os" = "Linux" ] ; then
  if [ "$um" = "x86_64" ] ; then
    export LINUX_BRAND linux64
  else
    export LINUX_BRAND linux32
  fi
fi  

echo "=================================================="
echo "un is '$un'"
echo "os is '$os'"
echo "um is '$um'"
echo "CC is $CC"
echo "FC is $FC"
echo "GNU compiler info"
echo "which $CC is"
which $CC
echo "$CC --version is"
$CC --version
echo "which $FC is"
which $FC
echo "$FC --version is"
$FC --version
echo "which libtool is"
which libtool
echo "libtool --version is"
libtool --version
echo "=================================================="

#--------------------------------------------------------------------
# Software directories, sorted alphabetically
#--------------------------------------------------------------------

### Change to indicate what versions of the following software you have.
# Note that this build script expects the directory for this software
# to be called exactly by these names, i.e. the netCDF software better
# be in a directory called "netcdf-4.4.0".
AUTOMAKE_DIR="automake-1.15"
AUTOCONF_DIR="autoconf-2.69"
CAIRO_DIR="cairo-1.16.0"
CURL_DIR="curl-7.64.0"
ESMF_DIR="esmf-7.1.0r"
EXPAT_DIR="expat-2.2.5"
FREETYPE_DIR="freetype-2.9.1"
G2CLIB_DIR="g2clib-1.6.0-patch"
GDAL_DIR="gdal-2.4.0"
GSL_DIR="gsl-2.5"
HDF4_DIR="hdf-4.2.14"
HDF5_DIR="hdf5-1.10.4"
HDFEOS_DIR="hdfeos-2.20"
HDFEOS5_DIR="hdfeos5-1.16"
JASPER_DIR="jasper-1.900.1"
JPEG_DIR="jpeg-9b"
LIBTOOL_DIR="libtool-2.4.6"
NETCDF4_DIR="netcdf-c-4.6.2"
NETCDF4_FORTRAN_DIR="netcdf-fortran-4.4.4"
PIXMAN_DIR="pixman-0.38.0"
PKGCONFIG_DIR="pkg-config-0.29.2"
PNG_DIR="libpng-1.6.36"
PROJ4_DIR="proj-5.2.0"
SZIP_DIR="szip-2.1.1"
UDUNITS_DIR="udunits-2.2.26"
VIS5DPLUS_DIR="vis5d+-1.3.0-beta"
ZLIB_DIR="zlib-1.2.11"
PYTHON_DIR="Python-2.7.12"
NUMPY_DIR="numpy-1.13.3"
INSTALL_PYTHON_DIR="$PREFIX/$PYTHON_DIR-$NUMPY_DIR"

#FONTCONFIG_DIR="fontconfig-2.10.93"
#OPENSSL_DIR="openssl-1.0.0c"
#XML2_DIR="libxml2-2.9.1"
#DAP_DIR="libdap-3.8.2"
#NC_DIR="libnc-dap-3.7.4"

### Change to indicate which packages you want to build.
# Any packages that are already built should have the line commented out.
# The packages are listed in the order they get built in.
#INSTALL_AUTOCONF=1
#INSTALL_AUTOMAKE=1
#INSTALL_LIBTOOL=1
INSTALL_PKGCONFIG=1
INSTALL_ZLIB=1
INSTALL_PNG=1
INSTALL_JPEG=1
INSTALL_JASPER=1
INSTALL_SZIP=1
# Maybe don't install curl on the Macs, unless you also build with --with-ssl=...
# Otherwise, the https OPeNDAP tests may not work.
INSTALL_CURL=1
INSTALL_HDF5=1
INSTALL_HDFEOS5=1
INSTALL_HDF4=1
INSTALL_HDFEOS=1
INSTALL_NETCDF4=1
INSTALL_NETCDF4_FORTRAN=1
INSTALL_PROJ4=1
# You may need to hand-build this. It's a small Makefile that requires simple edits.
#INSTALL_G2CLIB=1
INSTALL_GDAL=1
INSTALL_GSL=1
INSTALL_UDUNITS=1
#DON'T DO THIS ONE ON MAC? LET IT USE /opt/X11/lib?
INSTALL_EXPAT=1
INSTALL_FREETYPE=1
INSTALL_PIXMAN=1
INSTALL_CAIRO=1
INSTALL_ESMF=1

#INSTALL_PYTHON=1
#INSTALL_NUMPY=1

# These are ones we probably don't want to do any more
#INSTALL_VIS5DPLUS=1
#INSTALL_FONTCONFIG=1
#INSTALL_XML2=1
#INSTALL_OPENSSL=1
#INSTALL_DAP=1
#INSTALL_NCDAP=1

# Needed for cairo stuff.
export PKG_CONFIG_PATH="$PREFIX/lib/pkgconfig"
export PATH="$PREFIX/bin:$PATH"

# This setting for CPPFLAGS may be needed for some systems (Linux 32-bit
# was one). Only set it if the large file stuff doesn't work with NetCDF4.
# export CPPFLAGS='-D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE'.

CLEAN_BEFORE=1                      # Whether to clean before making
#CLEAN_AFTER=1                       # Whether to clean after making

#----------------------------------------------------------------------
# End of user modification section.
#----------------------------------------------------------------------

if [ $BUILD_NON_OPENDAP ] ; then
  if [ ! -d "$PREFIX_NODAP" ] ; then
    mkdir $PREFIX_NODAP
  fi
fi
if [ ! -d "$PREFIX/lib" ] ; then
  mkdir $PREFIX/lib
fi
if [ ! -d "$PREFIX/include" ] ; then
  mkdir $PREFIX/include
fi
if [ ! -d "$PREFIX/bin" ] ; then
  mkdir $PREFIX/bin
fi
if [ ! -d "$PREFIX/man" ] ; then
  mkdir $PREFIX/man
fi
if [ ! -d "$PREFIX/man/man1" ] ; then
  mkdir $PREFIX/man/man1
fi
if [ $INSTALL_AUTOCONF ] && [ ! -d $AUTOCONF_DIR ] ; then
  echo "$AUTOCONF_DIR does not exist"
  exit
fi
if [ $INSTALL_AUTOMAKE ] && [ ! -d $AUTOMAKE_DIR ] ; then
  echo "$AUTOMAKE_DIR does not exist"
  exit
fi
if [ $INSTALL_LIBTOOL ] && [ ! -d $LIBTOOL_DIR ] ; then
  echo "$LIBTOOL_DIR does not exist"
  exit
fi
if [ $INSTALL_PKGCONFIG ] && [ ! -d $PKGCONFIG_DIR ] ; then
  echo "$PKGCONFIG_DIR does not exist"
  exit
fi
if [ $INSTALL_ZLIB ] && [ ! -d $ZLIB_DIR ] ; then
  echo "$ZLIB_DIR does not exist"
  exit
fi
if [ $INSTALL_PNG ] && [ ! -d $PNG_DIR ] ; then
  echo "$PNG_DIR does not exist"
  exit
fi
if [ $INSTALL_JPEG ] && [ ! -d $JPEG_DIR ] ; then
  echo "$JPEG_DIR does not exist"
  exit
fi
if [ $INSTALL_JASPER ] && [ ! -d $JASPER_DIR ] ; then
  echo "$JASPER_DIR does not exist"
  exit
fi
if [ $INSTALL_SZIP ] && [ ! -d $SZIP_DIR ] ; then
  echo "$SZIP_DIR does not exist"
  exit
fi
if [ $INSTALL_CURL ] && [ ! -d $CURL_DIR ] ; then
  echo "$CURL_DIR does not exist"
  exit
fi
if [ $INSTALL_HDF5 ] && [ ! -d $HDF5_DIR ] ; then
  echo "$HDF5_DIR does not exist"
  exit
fi
if [ $INSTALL_HDFEOS5 ] && [ ! -d $HDFEOS5_DIR ] ; then
  echo "$HDFEOS5_DIR does not exist"
  exit
fi
if [ $INSTALL_HDF4 ] && [ ! -d $HDF4_DIR ] ; then
  echo "$HDF4_DIR does not exist"
  exit
fi
if [ $INSTALL_HDFEOS ] && [ ! -d $HDFEOS_DIR ] ; then
  echo "$HDFEOS_DIR does not exist"
  exit
fi
if [ $INSTALL_NETCDF4 ] && [ ! -d $NETCDF4_DIR ] ; then
  echo "$NETCDF4_DIR does not exist"
  exit
fi
if [ $INSTALL_NETCDF4_FORTRAN ] && [ ! -d $NETCDF4_FORTRAN_DIR ] ; then
  echo "$NETCDF4_FORTRAN_DIR does not exist"
  exit
fi
if [ $INSTALL_PROJ4 ] && [ ! -d $PROJ4_DIR ] ; then
  echo "$PROJ4_DIR does not exist"
  exit
fi
if [ $INSTALL_GDAL ] && [ ! -d $GDAL_DIR ] ; then
  echo "$GDAL_DIR does not exist"
  exit
fi
if [ $INSTALL_G2CLIB ] && [ ! -d $G2CLIB_DIR ] ; then
  echo "$G2CLIB_DIR does not exist"
  exit
fi
if [ $INSTALL_GSL ] && [ ! -d $GSL_DIR ] ; then
  echo "$GSL_DIR does not exist"
  exit
fi
if [ $INSTALL_UDUNITS ] && [ ! -d $UDUNITS_DIR ] ; then
  echo "$UDUNITS_DIR does not exist"
  exit
fi
if [ $INSTALL_VIS5DPLUS ] && [ ! -d $VIS5DPLUS_DIR ] ; then
  echo "$VIS5DPLUS_DIR does not exist"
  exit
fi
if [ $INSTALL_EXPAT ] && [ ! -d $EXPAT_DIR ] ; then
  echo "$EXPAT_DIR does not exist"
  exit
fi
if [ $INSTALL_FREETYPE ] && [ ! -d $FREETYPE_DIR ] ; then
  echo "$FREETYPE_DIR does not exist"
  exit
fi
if [ $INSTALL_FONTCONFIG ] && [ ! -d $FONTCONFIG_DIR ] ; then
  echo "$FONTCONFIG_DIR does not exist"
  exit
fi
if [ $INSTALL_PIXMAN ] && [ ! -d $PIXMAN_DIR ] ; then
  echo "$PIXMAN_DIR does not exist"
  exit
fi
if [ $INSTALL_CAIRO ] && [ ! -d $CAIRO_DIR ] ; then
  echo "$CAIRO_DIR does not exist"
  exit
fi
if [ $INSTALL_ESMF ] && [ ! -d $ESMF_DIR ] ; then
  echo "$ESMF_DIR does not exist"
  exit
fi
if [ $INSTALL_PYTHON ] && [ ! -d $PYTHON_DIR ] ; then
  echo "$PYTHON_DIR does not exist"
  exit
fi
if [ $INSTALL_NUMPY ] && [ ! -d $NUMPY_DIR ] ; then
  echo "$NUMPY_DIR does not exist"
  exit
fi
if [ $INSTALL_XML2 ] && [ ! -d $XML2_DIR ] ; then
  echo "$XML2_DIR does not exist"
  exit
fi

#
# The following section should not need any modification by 
# the user, unless there is a problem in the script.
#

#--------------------------------------------------------------------
# autoconf (sometimes this is needed for systems with old versions)
#--------------------------------------------------------------------
if [ $INSTALL_AUTOCONF ]
then
  if [ -d "$AUTOCONF_DIR" ]
  then
    cd $AUTOCONF_DIR
    if [ $CLEAN_BEFORE ]
    then
      echo "Cleaning $AUTOCONF_DIR before making..."
      make distclean
      make clean
    fi
    echo "Building $AUTOCONF_DIR..."
    ./configure --prefix\=$PREFIX
    make all install
    if [ $CLEAN_AFTER ]
    then
      echo "Cleaning $AUTOCONF_DIR after making..."
      make distclean
      make clean
    fi
    cd ..
  else
    echo "Directory $AUTOCONF_DIR doesn't exist"
  fi
else
  echo "Not building AUTOCONF..."
fi

#--------------------------------------------------------------------
# automake (sometimes this is needed for systems with old versions)
#--------------------------------------------------------------------
if [ $INSTALL_AUTOMAKE ]
then
  if [ -d "$AUTOMAKE_DIR" ]
  then
    cd $AUTOMAKE_DIR
    if [ $CLEAN_BEFORE ]
    then
      echo "Cleaning $AUTOMAKE_DIR before making..."
      make distclean
      make clean
    fi
    echo "Building $AUTOMAKE_DIR..."
    ./configure --prefix\=$PREFIX
    make all install
    if [ $CLEAN_AFTER ]
    then
      echo "Cleaning $AUTOMAKE_DIR after making..."
      make distclean
      make clean
    fi
    cd ..
  else
    echo "Directory $AUTOMAKE_DIR doesn't exist"
  fi
else
  echo "Not building AUTOMAKE..."
fi

#--------------------------------------------------------------------
# libtool (sometimes this is needed for systems with old versions)
#--------------------------------------------------------------------
if [ $INSTALL_LIBTOOL ]
then
  if [ -d "$LIBTOOL_DIR" ]
  then
    cd $LIBTOOL_DIR
    if [ $CLEAN_BEFORE ]
    then
      echo "Cleaning $LIBTOOL_DIR before making..."
      make distclean
      make clean
    fi
    echo "Building $LIBTOOL_DIR..."
    ./configure --prefix\=$PREFIX
    make all install
    if [ $CLEAN_AFTER ]
    then
      echo "Cleaning $LIBTOOL_DIR after making..."
      make distclean
      make clean
    fi
    cd ..
  else
    echo "Directory $LIBTOOL_DIR doesn't exist"
  fi
else
  echo "Not building LIBTOOL..."
fi

#--------------------------------------------------------------------
# pkg-config
#--------------------------------------------------------------------
if [ $INSTALL_PKGCONFIG ] ; then
  if [ -d "$PKGCONFIG_DIR" ] ; then
    cd $PKGCONFIG_DIR
    if [ $CLEAN_BEFORE ] ; then
      echo "Cleaning $PKGCONFIG_DIR before making..."
      make distclean
      make clean
    fi
    echo "Building $PKGCONFIG_DIR..."
# You shouldn't need --with-internal-glib if you set PKG_CONFIG_PATH
# to point to directory containing the glib-2.0.pc file.
#   ./configure --with-internal-glib --prefix\=$PREFIX
# Mac systems may need the "--with-internal-glib"
# if using gcc/gfortran.
#    ./configure --prefix\=$PREFIX  --with-internal-glib
    ./configure --prefix\=$PREFIX
    make all install
    if [ $? ] ; then
      echo "$PKGCONFIG_DIR build successful."
      export PKG_CONFIG_PATH="$PREFIX/lib/pkgconfig"
      export PATH="$PREFIX/bin:$PATH"
      if [ $CLEAN_AFTER ] ; then
        echo "Cleaning $PKGCONFIG_DIR after making..."
        make distclean
        make clean
      fi
    else
      echo "$PKGCONFIG_DIR build NOT successful."
    fi
    cd ..
  else
    echo "Directory $PKGCONFIG_DIR doesn't exist"
  fi
else
  echo "Not building pkgconfig..."
fi

echo "----------------------------------------------------------------------"
echo "Version of pkg-config being used"
which pkg-config
pkg-config --version
echo "----------------------------------------------------------------------"

#--------------------------------------------------------------------
# zlib
#--------------------------------------------------------------------
if [ $INSTALL_ZLIB ] ; then
  if [ -d "$ZLIB_DIR" ] ; then
    cd $ZLIB_DIR
    if [ $CLEAN_BEFORE ] ; then
      echo "Cleaning $ZLIB_DIR before making..."
      make distclean
      make clean
    fi
    echo "Building $ZLIB_DIR..."
    ./configure --prefix\=$PREFIX --static
    make all install
    if [ $? ] ; then
      echo "$ZLIB_DIR build successful."
      if [ $CLEAN_AFTER ] ; then
        echo "Cleaning $ZLIB_DIR after making..."
        make distclean
        make clean
      fi
    else
      echo "$ZLIB_DIR build NOT successful."
    fi
    cd ..
  else
    echo "Directory $ZLIB_DIR doesn't exist"
  fi
else
  echo "Not building zlib..."
fi

#--------------------------------------------------------------------
# png
#--------------------------------------------------------------------
if [ $INSTALL_PNG ] ; then
  if [ -d "$PNG_DIR" ] ; then
    cd $PNG_DIR
    if [ $CLEAN_BEFORE ] ; then
      echo "Cleaning $PNG_DIR before making..."
      make distclean
      make clean
    fi
    cp $PREFIX/include/zlib.h .
    cp $PREFIX/include/zconf.h .
    export LIBS="-L$PREFIX/lib"
    echo "Building $PNG_DIR..."
    ./configure  --with-pic --disable-shared --prefix\=$PREFIX
    make all install
    unset LIBS
    if [ $? ] ; then
      echo "$PNG_DIR build successful."
      if [ $CLEAN_AFTER ] ; then
        echo "Cleaning $PNG_DIR after making..."
        make distclean
        make clean
      fi
    else
      echo "$PNG_DIR build NOT successful."
    fi
    cd ..
  else
    echo "Directory $PNG_DIR doesn't exist"
  fi
else
  echo "Not building png..."
fi

#--------------------------------------------------------------------
# jpeg
#--------------------------------------------------------------------
if [ $INSTALL_JPEG ] ; then
  if [ -d "$JPEG_DIR" ] ; then
    cd $JPEG_DIR
    if [ $CLEAN_BEFORE ] ; then
      echo "Cleaning $JPEG_DIR before making..."
      make distclean
      make clean
    fi
    echo "Building $JPEG_DIR..."
    ./configure --disable-shared --prefix\=$PREFIX
    make all install
# Need this for jpeg-6b
#    make install-lib
#    make install-headers
    if [ $? ] ; then
      echo "$JPEG_DIR build successful."
      if [ $CLEAN_AFTER ] ; then
        echo "Cleaning $JPEG_DIR after making..."
        make distclean
        make clean
      fi
    else
      echo "$JPEG_DIR build NOT successful."
    fi
    cd ..
  else
    echo "Directory $JPEG_DIR doesn't exist"
  fi
else
  echo "Not building jpeg..."
fi

#--------------------------------------------------------------------
# Jasper
#--------------------------------------------------------------------
if [ $INSTALL_JASPER ] ; then
  if [ -d "$JASPER_DIR" ] ; then
    cd $JASPER_DIR
    if [ $CLEAN_BEFORE ] ; then
      echo "Cleaning $JASPER_DIR before making..."
      make distclean
      make clean
    fi
    echo "Building $JASPER_DIR..."
## Use this for Jasper V2 and greater
##  export SOURCE_DIR=`pwd`
##  export BUILD_DIR=$SOURCE_DIR/tmp
##  export INSTALL_DIR=$PREFIX
##  rm -rf $BUILD_DIR
##  mkdir $BUILD_DIR
##  cd $BUILD_DIR
##  cmake -G "Unix Makefiles" -H$SOURCE_DIR -B$BUILD_DIR  -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR -DJAS_ENABLE_SHARED=OFF
##  make all install
## Use this for jasper V1.900
# Need --includedir to point to jpeg include files.      
    ./configure --prefix\=$PREFIX --includedir\=$PREFIX/include
    make all install
    if [ $? ] ; then
      echo "$JASPER_DIR build successful."
      if [ $CLEAN_AFTER ] ; then
        echo "Cleaning $JASPER_DIR after making..."
        make distclean
        make clean
      fi
    else
      echo "$JASPER_DIR build NOT successful."
    fi
## Use this for Jasper V2 and greater
##  unset SOURCE_DIR
##  unset BUILD_DIR
##  unset INSTALL_DIR
    cd ..
  else
    echo "Directory $JASPER_DIR doesn't exist"
  fi
else
  echo "Not building Jasper..."
fi

#--------------------------------------------------------------------
# szip
#--------------------------------------------------------------------
if [ $INSTALL_SZIP ] ; then
  if [ -d "$SZIP_DIR" ] ; then
    cd $SZIP_DIR
    if [ $CLEAN_BEFORE ] ; then
      echo "Cleaning $SZIP_DIR before making..."
      make distclean
      make clean
    fi
    echo "Building $SZIP_DIR..."
    ./configure --disable-shared --prefix\=$PREFIX
    make all install
    if [ $? ] ; then
      echo "$SZIP_DIR build successful."
      if [ $CLEAN_AFTER ] ; then
        echo "Cleaning $SZIP_DIR after making..."
        make distclean
        make clean
      fi
    else
      echo "$SZIP_DIR build NOT successful."
    fi
    cd ..
  else
    echo "Directory $SZIP_DIR doesn't exist"
  fi
else
  echo "Not building szip..."
fi

#--------------------------------------------------------------------
# libcurl (needed for opendap-enabled netcdf) and may be picked up
# by other packages like GDAL and cairo.
#--------------------------------------------------------------------
if [ $INSTALL_CURL ] ; then
  if [ -d "$CURL_DIR" ] ; then
    cd $CURL_DIR
    if [ $CLEAN_BEFORE ] ; then
      echo "Cleaning $CURL_DIR before making..."
      make distclean
      make clean
    fi
    echo "Building $CURL_DIR..."
#    ./configure --prefix\=$PREFIX --with-zlib\=$PREFIX --with-pic --disable-shared --with-ssl=/usr/local
    ./configure --prefix\=$PREFIX --with-zlib\=$PREFIX --with-pic --disable-shared
    make all install
    if [ $? ] ; then
      echo "$CURL_DIR build successful."
      if [ $CLEAN_AFTER ] ; then
        echo "Cleaning $CURL_DIR after making..."
        make distclean
        make clean
      fi
    else
      echo "$CURL_DIR build NOT successful."
    fi
    cd ..
  else
    echo "Directory $CURL_DIR doesn't exist"
  fi
else
  echo "Not building libcurl..."
fi

# Needed for NetCDF and OPeNDAP
if which curl-config > /dev/null 2>&1; then
  CURL_LIBS=`curl-config --libs`
else
  echo "Can't find curl-config."
  exit
fi

echo "=================================================="
echo "CURL_LIBS = $CURL_LIBS"
which curl-config
echo "=================================================="

#--------------------------------------------------------------------
# HDF-5
#--------------------------------------------------------------------
if [ $INSTALL_HDF5 ] ; then
  if [ -d "$HDF5_DIR" ] ; then
    cd $HDF5_DIR
    if [ $CLEAN_BEFORE ] ; then
      echo "Cleaning $HDF5_DIR before making..."
      make distclean
      make clean
    fi
    echo "Building $HDF5_DIR..."
    ./configure --disable-shared --with-zlib\=$PREFIX --with-szlib\=$PREFIX --prefix\=$PREFIX
    make all install
    if [ $? ] ; then
      echo "$HDF5_DIR build successful."
      if [ $CLEAN_AFTER ] ; then
        echo "Cleaning $HDF5_DIR after making..."
        make distclean
        make clean
      fi
    else
      echo "$HDF5_DIR build NOT successful."
    fi
    cd ..
  else
    echo "Directory $HDF5_DIR doesn't exist"
  fi
else
  echo "Not building HDF5..."
fi

#--------------------------------------------------------------------
# HDF-EOS 5
#--------------------------------------------------------------------
if [ $INSTALL_HDFEOS5 ] ; then
  if [ -d "$HDFEOS5_DIR" ] ; then
    cd $HDFEOS5_DIR
    if [ $CLEAN_BEFORE ] ; then
      echo "Cleaning $HDFEOS5_DIR before making..."
      make distclean
      make clean
    fi
    echo "Building $HDFEOS5_DIR..."
# Might need this on a Mac and other systems?
#    ./configure --enable-static --with-hdf\=$PREFIX --with-zlib\=$PREFIX --with-szlib\=$PREFIX --prefix\=$PREFIX
# Need the H5_USE_16_API for HDF-EOS 1.16 compatibility with HDF5 1.10 (maybe not for HDF-EOS 1.17 when released)
#    export CFLAGS_HE5="$CFLAGS -DH5_USE_16_API"
#    ./configure --with-hdf5\=$PREFIX CFLAGS\=$CFLAGS_HE5 --with-zlib\=$PREFIX --with-szlib\=$PREFIX --prefix\=$PREFIX
    ./configure CC\=$PREFIX/bin/h5cc --with-hdf5\=$PREFIX --with-zlib\=$PREFIX --with-szlib\=$PREFIX --prefix\=$PREFIX
    make all install
    /bin/rm -rf $PREFIX/include/HE5_HdfEosDef.h $PREFIX/include/HE5_GctpFunc.h
    cp include/HE5_HdfEosDef.h include/HE5_GctpFunc.h $PREFIX/include/.
    if [ $? ] ; then
      echo "$HDFEOS5_DIR build successful."
      if [ $CLEAN_AFTER ] ; then
        echo "Cleaning $HDFEOS5_DIR after making..."
        make distclean
        make clean
      fi
    else
      echo "$HDFEOS5_DIR build NOT successful."
    fi
    cd ..
  else
    echo "Directory $HDFEOS5_DIR doesn't exist"
  fi
else
  echo "Not building HDFEOS5..."
fi

#--------------------------------------------------------------------
# HDF-4
#--------------------------------------------------------------------
if [ $INSTALL_HDF4 ] ; then
  if [ -d "$HDF4_DIR" ] ; then
    cd $HDF4_DIR
    if [ $CLEAN_BEFORE ] ; then
      echo "Cleaning $HDF4_DIR before making..."
      make distclean
      make clean
    fi
    echo "Building $HDF4_DIR..."
    if [ "$os" = "Darwin" ] ; then
      ./configure --disable-netcdf --disable-dependency-tracking --with-zlib\=$PREFIX --with-jpeg\=$PREFIX --prefix\=$PREFIX
    else
      ./configure  --disable-netcdf --with-zlib\=$PREFIX --with-jpeg\=$PREFIX --prefix\=$PREFIX
    fi
    make all install
    if [ $? ] ; then
      echo "$HDF4_DIR build successful."
      if [ $CLEAN_AFTER ] ; then
        echo "Cleaning $HDF4_DIR after making..."
        make distclean
        make clean
      fi
    else
      echo "$HDF4_DIR build NOT successful."
    fi
    cd ..
  else
    echo "Directory $HDF4_DIR doesn't exist"
  fi
else
  echo "Not building HDF4..."
fi

#--------------------------------------------------------------------
# HDF-EOS 2
#--------------------------------------------------------------------
if [ $INSTALL_HDFEOS ] ; then
  if [ -d "$HDFEOS_DIR" ] ; then
    cd $HDFEOS_DIR
    if [ $CLEAN_BEFORE ] ; then
      echo "Cleaning $HDFEOS_DIR before making..."
      make distclean
      make clean
    fi
    echo "Building $HDFEOS_DIR..."
    ./configure CC\=$PREFIX/bin/h4cc --with-hdf4\=$PREFIX --with-zlib\=$PREFIX --with-jpeg\=$PREFIX --prefix\=$PREFIX
    make all install
    make install
    /bin/rm -rf $PREFIX/include/HdfEosDef.h $PREFIX/include/ease.h $PREFIX/include/cfortHdf.h
    cp include/HdfEosDef.h include/ease.h include/cfortHdf.h $PREFIX/include/.
    if [ $? ] ; then
      echo "$HDFEOS_DIR build successful."
      if [ $CLEAN_AFTER ] ; then
        echo "Cleaning $HDFEOS_DIR after making..."
        make distclean
        make clean
      fi
    else
      echo "$HDFEOS_DIR build NOT successful."
    fi
    cd ..
  else
    echo "Directory $HDFEOS_DIR doesn't exist"
  fi
else
  echo "Not building HDFEOS..."
fi


#--------------------------------------------------------------------
# NetCDF C interface (this section handles regular NetCDF or 
# classic NetCDF-4). This section makes the non-OPeNDAP version
# first, then the OPeNDAP version
#--------------------------------------------------------------------
if [ "$INSTALL_NETCDF4" ]
then
  if [ -d "$NETCDF4_DIR" ] ; then
    cd $NETCDF4_DIR
    if [ $CLEAN_BEFORE ] ; then
      echo "Cleaning $NETCDF4_DIR before making..."
      make distclean
      make clean
    fi
    echo "Building $NETCDF4_DIR..."
    export LDFLAGS="-L$PREFIX/lib"
    export CPPFLAGS="-I$PREFIX/include"
    echo "Cleaning $NETCDF4_DIR to do non-OPeNDAP build"
    make distclean
    make clean
    if [ "$os" = "Darwin" ] ; then
      if [ $BUILD_NON_OPENDAP ] ; then
        export LIBS="-lhdf5_hl -lhdf5 -lsz -lz"
        ./configure --enable-netcdf-4 --disable-dap --disable-dispatch --disable-shared --prefix\=$PREFIX_NODAP
        make all install
        make install
        make distclean
        make clean
        unset LIBS
      fi
      echo "Cleaning $NETCDF4_DIR to do OPeNDAP build"
      export LIBS="-lhdf5_hl -lhdf5 -lsz -lz $CURL_LIBS"
      ./configure --enable-netcdf-4 --enable-dap --disable-dispatch --disable-shared --prefix\=$PREFIX  --enable-large-file-tests --enable-dap-remote-tests
      make all install
      make install
      unset LIBS
    else
      export LIBS="-lhdf5_hl -lhdf5 -lsz -lz"
      if [ $BUILD_NON_OPENDAP ] ; then
        ./configure --enable-netcdf-4 --disable-dap --disable-shared --prefix\=$PREFIX_NODAP
        make all install
        make install
        mv config.log config.log.nodap
        make distclean
        make clean
        unset LIBS
      fi
      echo "Cleaning $NETCDF4_DIR to do OPeNDAP build"
      export LIBS="-lhdf5_hl -lhdf5 -lsz -lz $CURL_LIBS"
      ./configure --enable-netcdf-4 --enable-dap --disable-shared --prefix\=$PREFIX  --enable-large-file-tests --enable-dap-remote-tests
      make all install
      make install
      unset LIBS
    fi
    if [ $? ] ; then
      echo "$NETCDF4_DIR build successful."
      if [ $CLEAN_AFTER ] ; then
        echo "Cleaning $NETCDF4_DIR after making..."
        make distclean
        make clean
      fi
    else
      echo "$NETCDF4_DIR build NOT successful."
    fi
    cd ..
  else
    echo "Directory $NETCDF4_DIR doesn't exist"
  fi
else
  echo "Not building NetCDF C interface..."
fi

#--------------------------------------------------------------------
# NetCDF-fortran (this is for the Fortran interface only, for ESMF)
#--------------------------------------------------------------------
if [ "$INSTALL_NETCDF4_FORTRAN" ]
then
  if [ -d "$NETCDF4_FORTRAN_DIR" ] ; then
    cd $NETCDF4_FORTRAN_DIR
    if [ $CLEAN_BEFORE ] ; then
      echo "Cleaning $NETCDF4_FORTRAN_DIR before making..."
      make distclean
      make clean
    fi
    echo "Building $NETCDF4_FORTRAN_DIR..."
    export CPPFLAGS="-I$PREFIX/include"
    export LDFLAGS="-L$PREFIX/lib"
    export LIBS="-lnetcdf -lhdf5_hl -lhdf5 -lsz -lz $CURL_LIBS"
    ./configure --disable-shared --prefix\=$PREFIX
    make all install
# This extra "install" step was required in the past. May not be needed any more.
    make install
    unset LIBS
    if [ $? ] ; then
      echo "$NETCDF4_FORTRAN_DIR build successful."
      if [ $CLEAN_AFTER ] ; then
        echo "Cleaning $NETCDF4_FORTRAN_DIR after making..."
        make distclean
        make clean
      fi
    else
      echo "$NETCDF4_FORTRAN_DIR build NOT successful."
    fi
    cd ..
  else
    echo "Directory $NETCDF4_FORTRAN_DIR doesn't exist"
  fi
else
  echo "Not building NetCDF Fortran interface..."
fi

#--------------------------------------------------------------------
# g2clib (GRIB2)
#
# For 64-bit systems, you need to edit the Makefile and add
# -D__64BIT__ on the DEFS line
#--------------------------------------------------------------------
if [ $INSTALL_G2CLIB ] ; then
  if [ -d "$G2CLIB_DIR" ] ; then
    export EXTRA_INC="-I$PREFIX/include"
#
# No clean check needed; this software is automatically
# cleaned after each build.
#
    echo "Building $G2CLIB_DIR..."
    cd $G2CLIB_DIR
    make all
    if [ $? ] ; then
      mv libgrib2c.a $PREFIX/lib
      cp grib2.h $PREFIX/include
      echo "$G2CLIB_DIR build successful."
#
# No clean check needed; this software is automatically
# cleaned after each build.
#
    else
      echo "$G2CLIB_DIR build NOT successful."
    fi
    cd ..
  else
    echo "Directory $G2CLIB_DIR doesn't exist"
  fi
else
  echo "Not building G2CLIB..."
fi

#--------------------------------------------------------------------
# PROJ.4
#--------------------------------------------------------------------
if [ $INSTALL_PROJ4 ] ; then
  if [ -d "$PROJ4_DIR" ] ; then
    cd $PROJ4_DIR
    if [ $CLEAN_BEFORE ] ; then
      echo "Cleaning $PROJ4_DIR before making..."
      make distclean
      make clean
    fi
    echo "Building $PROJ4_DIR..."
    ./configure --enable-static=yes --enable-shared=no --without-jni --prefix\=$PREFIX
    make all install
    if [ $? ] ; then
      echo "$PROJ4_DIR build successful."
      if [ $CLEAN_AFTER ] ; then
        echo "Cleaning $PROJ4_DIR after making..."
        make distclean
        make clean
      fi
    else
      echo "$PROJ4_DIR build NOT successful."
    fi
    cd ..
  else
    echo "Directory $PROJ4_DIR doesn't exist"
  fi
else
  echo "Not building PROJ4..."
fi

#--------------------------------------------------------------------
# GDAL
#--------------------------------------------------------------------
if [ $INSTALL_GDAL ] ; then
  if [ -d "$GDAL_DIR" ] ; then
    cd $GDAL_DIR
    if [ $CLEAN_BEFORE ] ; then
      echo "Cleaning $GDAL_DIR before making..."
      make distclean
      make clean
    fi
    echo "Building $GDAL_DIR..."
#    if [ "$os" = "Darwin" ] ; then
#      export CPPFLAGS="-I/opt/X11/include"
#      export LDFLAGS="-L/opt/X11/lib"
#    fi
# Some systems don't have all these extra libraries!
#  ./configure --with-static-proj4\=$PREFIX --prefix\=$PREFIX --without-pam --with-png\=$PREFIX --with-gif=internal --with-libtiff=internal --with-geotiff=internal --with-jpeg\=$PREFIX --with-libz\=$PREFIX --with-sqlite3=no --with-expat=no --with-curl=no --without-ld-shared --with-hdf4=no --with-hdf5=no --with-pg=no --without-grib --disable-shared --with-freexl=no --with-geos=no --with-openjpeg=no --with-mysql=no --with-ecw=no --with-fgdb=no --with-odbc=no --with-xml2=no --with-ogdi=no --with-pcraster=no --with-xerces=no --with-libkml=no --with-libjson-c=no
    ./configure --with-static-proj4\=$PREFIX --prefix\=$PREFIX --without-pam --with-png\=$PREFIX --with-gif=internal --with-libtiff=internal --with-geotiff=internal --with-jpeg\=$PREFIX --with-libz\=$PREFIX --with-sqlite3=no --with-expat=no --with-curl=no --without-ld-shared --with-hdf4=no --with-hdf5=no --with-pg=no --without-grib --disable-shared --with-freexl=no --with-geos=no --with-openjpeg=no --with-mysql=no --with-ecw=no --with-fgdb=no --with-odbc=no --with-xml2=no --with-ogdi=no --with-pcraster=no --with-xerces=no
    make all install
    make install
#    if [ "$os" = "Darwin" ] ; then
#      unset CPPFLAGS
#      unset LDFLAGS
#    fi
    if [ $? ] ; then
      echo "$GDAL_DIR build successful."
      if [ $CLEAN_AFTER ] ; then
        echo "Cleaning $GDAL_DIR after making..."
        make distclean
        make clean
      fi
    else
      echo "$GDAL_DIR build NOT successful."
    fi
    cd ..
  else
    echo "Directory $GDAL_DIR doesn't exist"
  fi
else
  echo "Not building GDAL..."
fi

#--------------------------------------------------------------------
# GSL
#--------------------------------------------------------------------
if [ $INSTALL_GSL ] ; then
  if [ -d "$GSL_DIR" ] ; then
    cd $GSL_DIR
    if [ $CLEAN_BEFORE ] ; then
      echo "Cleaning $GSL_DIR before making..."
      make distclean
      make clean
    fi
    echo "Building $GSL_DIR..."
   ./configure --prefix\=$PREFIX --disable-shared --enable-static
    make all install
    make install
    if [ $? ] ; then
      echo "$GSL_DIR build successful."
      if [ $CLEAN_AFTER ] ; then
        echo "Cleaning $GSL_DIR after making..."
        make distclean
        make clean
      fi
    else
      echo "$GSL_DIR build NOT successful."
    fi
    cd ..
  else
    echo "Directory $GSL_DIR doesn't exist"
  fi
else
  echo "Not building GSL..."
fi

#--------------------------------------------------------------------
# Udunits
# NOT NEEDED B/C PART OF NETCDF-4.1
#--------------------------------------------------------------------
if [ $INSTALL_UDUNITS ] ; then
  if [ -d "$UDUNITS_DIR" ] ; then
    export PERL=""
    cd $UDUNITS_DIR
    if [ $CLEAN_BEFORE ] ; then
      echo "Cleaning $UDUNITS_DIR before making..."
      make distclean
      make clean
    fi
    echo "Building $UDUNITS_DIR..."
    ./configure --prefix\=$PREFIX --disable-shared
    make all install
    if [ $? ] ; then
      echo "$UDUNITS_DIR build successful."
      if [ $CLEAN_AFTER ] ; then
        echo "Cleaning $UDUNITS_DIR after making..."
        make distclean
        make clean
      fi
    else
      echo "$UDUNITS_DIR build NOT successful."
    fi
    cd ..
  else
    echo "Directory $UDUNITS_DIR doesn't exist"
  fi
else
  echo "Not building UDUNITS..."
fi


#--------------------------------------------------------------------
# Vis5D+
#--------------------------------------------------------------------
if [ $INSTALL_VIS5DPLUS ] ; then
  if [ -d "$VIS5DPLUS_DIR" ] ; then
    cd $VIS5DPLUS_DIR
    if [ $CLEAN_BEFORE ] ; then
      echo "Cleaning $VIS5DPLUS_DIR before making..."
      make distclean
      make clean
    fi
    echo "Building $VIS5DPLUS_DIR..."
    ./configure --prefix\=$PREFIX --with-netcdf\=$PREFIX/lib/libnetcdf.a
    make all install
    if [ $? ] ; then
      echo "$VIS5DPLUS_DIR build successful."
      if [ $CLEAN_AFTER ] ; then
        echo "Cleaning $VIS5DPLUS_DIR after making..."
        make distclean
        make clean
      fi
    else
      echo "$VIS5DPLUS_DIR build NOT successful."
    fi
    cd ..
  else
    echo "Directory $VIS5DPLUS_DIR doesn't exist"
  fi
else
  echo "Not building VIS5DPLUS..."
fi

#--------------------------------------------------------------------
# Expat
#--------------------------------------------------------------------
if [ $INSTALL_EXPAT ] ; then
  if [ -d "$EXPAT_DIR" ] ; then
    cd $EXPAT_DIR
    if [ $CLEAN_BEFORE ] ; then
      echo "Cleaning $EXPAT_DIR before making..."
      make distclean
      make clean
    fi
    echo "Building $EXPAT_DIR..."
    ./configure --disable-shared --prefix\=$PREFIX
    make all install
    if [ $? ] ; then
      echo "$EXPAT_DIR build successful."
      if [ $CLEAN_AFTER ] ; then
        echo "Cleaning $EXPAT_DIR after making..."
        make distclean
        make clean
      fi
    else
      echo "$EXPAT_DIR build NOT successful."
    fi
    cd ..
  else
    echo "Directory $EXPAT_DIR doesn't exist"
  fi
else
  echo "Not building expat..."
fi

#--------------------------------------------------------------------
# FreeType
#--------------------------------------------------------------------
if [ $INSTALL_FREETYPE ] ; then
  if [ -d "$FREETYPE_DIR" ] ; then
    cd $FREETYPE_DIR
    if [ $CLEAN_BEFORE ] ; then
      echo "Cleaning $FREETYPE_DIR before making..."
      make distclean
      make clean
    fi
    echo "Building $FREETYPE_DIR..."
    ./configure --prefix\=$PREFIX --disable-shared
    make all install
    if [ $? ] ; then
      echo "$FREETYPE_DIR build successful."
      if [ $CLEAN_AFTER ] ; then
        echo "Cleaning $FREETYPE_DIR after making..."
        make distclean
        make clean
      fi
    else
      echo "$FREETYPE_DIR build NOT successful."
    fi
    cd ..
  else
    echo "Directory $FREETYPE_DIR doesn't exist"
  fi
else
  echo "Not building FreeType..."
fi


#--------------------------------------------------------------------
# fontconfig
#--------------------------------------------------------------------
if [ $INSTALL_FONTCONFIG ] ; then
  if [ -d "$FONTCONFIG_DIR" ] ; then
    cd $FONTCONFIG_DIR
    if [ $CLEAN_BEFORE ] ; then
      echo "Cleaning $FONTCONFIG_DIR before making..."
      make distclean
      make clean
    fi
    echo "Building $FONTCONFIG_DIR..."
# You might need these two settings.
    export PKG_CONFIG_PATH="$PREFIX/lib/pkgconfig"
    export PATH="$PREFIX/bin:$PATH"

    ./configure --prefix\=$PREFIX --disable-shared
    make all install
    if [ $? ] ; then
      echo "$FONTCONFIG_DIR build successful."
      if [ $CLEAN_AFTER ] ; then
        echo "Cleaning $FONTCONFIG_DIR after making..."
        make distclean
        make clean
      fi
    else
      echo "$FONTCONFIG_DIR build NOT successful."
    fi
    cd ..
  else
    echo "Directory $FONTCONFIG_DIR doesn't exist"
  fi
else
  echo "Not building fontconfig..."
fi

#--------------------------------------------------------------------
# pixman
#--------------------------------------------------------------------
if [ $INSTALL_PIXMAN ] ; then
  if [ -d "$PIXMAN_DIR" ] ; then
    cd $PIXMAN_DIR
    if [ $CLEAN_BEFORE ] ; then
      echo "Cleaning $PIXMAN_DIR before making..."
      make distclean
      make clean
    fi
    echo "Building $PIXMAN_DIR..."
    ./configure --prefix\=$PREFIX --disable-shared --disable-openmp --disable-arm-iwmmxt --disable-dependency-tracking
    make all install
# Need this because some test fails with undefined "_lgc_seed"
    make install
    if [ $? ] ; then
      echo "$PIXMAN_DIR build successful."
      if [ $CLEAN_AFTER ] ; then
        echo "Cleaning $PIXMAN_DIR after making..."
        make distclean
        make clean
      fi
    else
      echo "$PIXMAN_DIR build NOT successful."
    fi
    cd ..
  else
    echo "Directory $PIXMAN_DIR doesn't exist"
  fi
else
  echo "Not building pixman..."
fi

#--------------------------------------------------------------------
# cairo
#--------------------------------------------------------------------
if [ $INSTALL_CAIRO ] ; then
  export PKG_CONFIG_PATH="$PREFIX/lib/pkgconfig"
  if [ -d "$CAIRO_DIR" ] ; then
    cd $CAIRO_DIR
    if [ $CLEAN_BEFORE ] ; then
      echo "Cleaning $CAIRO_DIR before making..."
      make distclean
      make clean
    fi
    echo "Building $CAIRO_DIR..."
# Need the quartz disabled for all systems?
    ./configure --prefix\=$PREFIX --enable-quartz-font=no --enable-quartz=no --enable-shared=no --enable-static=yes
# Needed for k5? disable-gobject??
#    ./configure --prefix\=$PREFIX --enable-quartz-font=no --enable-quartz=no --enable-shared=no --enable-static=yes --disable-gobject 
#    if [ "$os" = "Darwin" ] ; then
#      ./configure --prefix\=$PREFIX --enable-quartz-font=no --enable-quartz=no --enable-shared=no --enable-static=yes
#    else
#      ./configure --prefix\=$PREFIX --disable-shared
#    fi
    make all install
    if [ $? ] ; then
      echo "$CAIRO_DIR build successful."
      if [ $CLEAN_AFTER ] ; then
        echo "Cleaning $CAIRO_DIR after making..."
        make distclean
        make clean
      fi
    else
      echo "$CAIRO_DIR build NOT successful."
    fi
    cd ..
  else
    echo "Directory $CAIRO_DIR doesn't exist"
  fi
else
  echo "Not building cairo..."
fi

#--------------------------------------------------------------------
# Open SSL
# Should not be needed because OPeNDAP now part of NetCDF-4.1
#--------------------------------------------------------------------
if [ $INSTALL_OPENSSL ] ; then
  if [ -d "$OPENSSL_DIR" ] ; then
    cd $OPENSSL_DIR
    if [ $CLEAN_BEFORE ] ; then
      echo "Cleaning $OPENSSL_DIR before making..."
      make distclean
      make clean
    fi
    echo "Building $OPENSSL_DIR..."
    ./configure --prefix\=$PREFIX
    make all install
    if [ $? ] ; then
      echo "$OPENSSL_DIR build successful."
      if [ $CLEAN_AFTER ] ; then
        echo "Cleaning $OPENSSL_DIR after making..."
        make distclean
        make clean
      fi
    else
      echo "$OPENSSL_DIR build NOT successful."
    fi
    cd ..
  else
    echo "Directory $OPENSSL_DIR doesn't exist"
  fi
else
  echo "Not building Open SSL..."
fi

#--------------------------------------------------------------------
# ESMF (needed for ESMF regridding)
# This section builds two versions of ESMF_RegridWeightGen: one
# linked against OPenDAP-enabled libraries, and one not.
#
# The non-OPeNDAP version is built first.
#--------------------------------------------------------------------
if [ $INSTALL_ESMF ] ; then
  if [ -d "$ESMF_DIR" ] ; then
    cd $ESMF_DIR

# Do this before cleaning!
    export ESMF_DIR=`pwd`

#
# This group of env vars will need to be changed before OPeNDAP build.
#
# We have to get the non-OPeNDAP path on the load line first. The
# only way to do this seems to be by setting the 
# ESMF_LAPACK_LIBPATH to the non-OPeNDAP NetCDF path, and setting 
# ESMF_NETCDF_LIBPATH to the LAPACK path.
#
# The order of -L paths seems to be:
#  -L$ESMF_LAPACK_LIBPATH -L$ESMF_NETCDF_LIBPATH ESMF_CXXLINKPATHS
#
    if [ $BUILD_NON_OPENDAP ] ; then
      echo "Cleaning $ESMF_DIR before building non-OPeNDAP version..."
      export ESMF_INSTALL_PREFIX=$PREFIX_NODAP
      export ESMF_LAPACK_LIBPATH=$PREFIX_NODAP/lib
      export ESMF_CXXLINKPATHS="-L$NCARG_ROOT/lib -L$PREFIX_NODAP/lib -L$PREFIX/lib"
      export ESMF_CXXCOMPILEPATHS="-I$PREFIX_NODAP/include -I$PREFIX/include"
      export ESMF_NETCDF_LIBS="-lnetcdff -lnetcdf -lhdf5_hl -lhdf5 -lsz -lz"
      if [ "$os" = "Darwin" ] ; then
        export ESMF_F90LINKLIBS="-lstdc++ -lc++ -llapack_ncl -lblas_ncl -lnetcdff -lnetcdf -lhdf5_hl -lhdf5 -lsz -lz"
      fi
      make distclean
      make clean
    fi

# The rest of the env vars should stay the same for both builds
    export ESMF_NETCDF_LIBPATH=$PREFIX/lib
    export ESMF_NETCDF_INCLUDE=$PREFIX/include
    export ESMF_LAPACK_LIBS="-llapack_ncl -lblas_ncl"
    export ESMF_F90LINKPATHS="-L$NCARG_ROOT/lib"
    if [ "$os" = "Darwin" ] ; then
      export ESMF_CXXLINKLIBS="-lstdc++ -lc++"
    fi
    export ESMF_NETCDF="split"
    export ESMF_LAPACK="netlib"
    export ESMF_COMM=mpiuni
#
# k6/7/9.rap.ucar.edu need extra hand-holding for some reason.
# The ESMF build system doesn't see them as being Linux systems.
#
    if [ "$un" = "k6" ] || [ "$un" = "k7" ] || [ "$un" = "k9" ] ; then
      export ESMF_COMPILER=gfortran
      export ESMF_OS=Linux
      export ESMF_CXXCOMPILER=/usr/bin/g++
      export ESMF_F90COMPILER=/usr/bin/gfortran
    elif [ "$un" = "yellowstone_gnu483" ] || [ "$un" = "yellowstone_gnu472" ] ; then
      export ESMF_COMPILER=gfortran
      export ESMF_OS=Linux
    elif [ "$un" = "yellowstone_intel1215" ] || [ "$un" = "yellowstone_intel1701" ] ; then
      export ESMF_COMPILER=intel
      export ESMF_OS=Linux
    elif [ "$os" = "Darwin" ] ; then
#     export ESMF_COMPILER=gfortranclang
      export ESMF_COMPILER=gfortran
      export ESMF_OS=Darwin
    else
      export ESMF_COMPILER=gfortran
    fi
    if [ "$um" = "x86_64" ] ; then
      export ESMF_ABI=64
    else
      export ESMF_ABI=32
    fi
# Don't use ESMF_BUILD_DIR here
    export ESMFBUILDDIR="$os.$ESMF_COMPILER.$ESMF_ABI.$ESMF_COMM.default"
    export ESMFREGRIDFILE="$ESMF_INSTALL_PREFIX/bin/binO/$ESMFBUILDDIR/ESMF_RegridWeightGen"
    if [ "$os" = "Darwin" ] ; then
      export ESMFSHAREDLIB="lib/libO/$ESMFBUILDDIR/libesmf.dylib"
    else
      export ESMFSHAREDLIB="lib/libO/$ESMFBUILDDIR/libesmf.so"
    fi

# Here's the non-OPeNDAP build
    if [ $BUILD_NON_OPENDAP ] ; then
      echo "Building non-OPeNDAP version of $ESMF_DIR..."
      make all install

# This is to get rid of libesmf shared library dependency.
      /bin/rm -rf $ESMF_DIR/$ESMFSHAREDLIB $ESMF_INSTALL_PREFIX/$ESMFSHAREDLIB
      make install

# If successful, move ESMF_RegridWeightGen to appropriate location.
      if [ -f $ESMFREGRIDFILE ] ; then
        echo "Moving $ESMFREGRIDFILE to $PREFIX/bin/ESMF_RegridWeightGen.nodap"
        /bin/mv $ESMFREGRIDFILE $PREFIX/bin/ESMF_RegridWeightGen.nodap
        /bin/rm -rf $ESMF_INSTALL_PREFIX/bin/binO
        /bin/rm -rf $ESMF_INSTALL_PREFIX/lib/libO
        /bin/rm -rf $ESMF_INSTALL_PREFIX/include/ESMC*
      else
        echo "$ESMFREGRIDFILE doesn't exist"
      fi
    fi
#--------------------------------------------------
# Now build OPeNDAP version
#--------------------------------------------------
    echo "Cleaning $ESMF_DIR before building OPeNDAP version..."
    make distclean
    make clean

# Change these ESMF env vars before OPeNDAP build.
    export ESMF_INSTALL_PREFIX=$PREFIX
    export ESMF_LAPACK_LIBPATH=$NCARG_ROOT/lib
    export ESMF_CXXCOMPILEPATHS="-I$PREFIX/include"
    if [ "$os" = "Darwin" ] ; then
      export ESMF_F90LINKLIBS="-lstdc++ -lc++ -llapack_ncl -lblas_ncl -lnetcdff -lnetcdf -lhdf5_hl -lhdf5 -lsz -lz $CURL_LIBS"
    fi
    export ESMFREGRIDFILE="$ESMF_INSTALL_PREFIX/bin/binO/$ESMFBUILDDIR/ESMF_RegridWeightGen"
    export ESMF_CXXLINKPATHS="-L$NCARG_ROOT/lib -L$PREFIX/lib"
    export ESMF_NETCDF_LIBS="-lnetcdff -lnetcdf -lhdf5_hl -lhdf5 -lsz -lz $CURL_LIBS"
    echo "Building OPeNDAP version of $ESMF_DIR..."
    make all install

# This is to get rid of libesmf shared library dependency.
    /bin/rm -rf $ESMF_DIR/$ESMFSHAREDLIB $ESMF_INSTALL_PREFIX/$ESMFSHAREDLIB
    make install

# If successful, move ESMF_RegridWeightGen to appropriate location and clean up.
    if [ -f $ESMFREGRIDFILE ] ; then
      echo "Moving $ESMFREGRIDFILE to $PREFIX/bin/ESMF_RegridWeightGen.dap"
      /bin/mv $ESMFREGRIDFILE $PREFIX/bin/ESMF_RegridWeightGen.dap
      /bin/rm -rf $ESMF_INSTALL_PREFIX/bin/binO
      /bin/rm -rf $ESMF_INSTALL_PREFIX/lib/libO
      /bin/rm -rf $ESMF_INSTALL_PREFIX/include/ESMC*
    else
      echo "$ESMFREGRIDFILE doesn't exist"
    fi

    if [ $CLEAN_AFTER ] ; then
      echo "Cleaning $ESMF_DIR after making..."
      make distclean
      make clean
    fi
    cd ..
  else
    echo "Directory $ESMF_DIR doesn't exist"
  fi
else
  echo "Not building ESMF..."
fi

#--------------------------------------------------------------------
# Python (needed for PyNGL and PyNIO).
#--------------------------------------------------------------------
if [ $INSTALL_PYTHON ] ; then
  if [ ! -d "$INSTALL_PYTHON_DIR" ] ; then
    mkdir $INSTALL_PYTHON_DIR
  fi
  if [ -d "$PYTHON_DIR" ] ; then
    cd $PYTHON_DIR
    if [ $CLEAN_BEFORE ] ; then
      echo "Cleaning $PYTHON_DIR before making..."
      make distclean
      make clean
    fi
    echo "Building $PYTHON_DIR..."
    ./configure --prefix\=$INSTALL_PYTHON_DIR
    make all install
    if [ $? ] ; then
      echo "$PYTHON_DIR build successful."
      if [ $CLEAN_AFTER ] ; then
        echo "Cleaning $PYTHON_DIR after making..."
        make distclean
        make clean
      fi
    else
      echo "$PYTHON_DIR build NOT successful."
    fi
    cd ..
  else
    echo "Directory $PYTHON_DIR doesn't exist"
  fi
else
  echo "Not building python..."
fi

#--------------------------------------------------------------------
# Numpy (needed for PyNGL and PyNIO).
#--------------------------------------------------------------------
if [ $INSTALL_NUMPY ] ; then
  if [ -d "$NUMPY_DIR" ] ; then
    if [ ! -f $INSTALL_PYTHON_DIR/bin/python ] ; then
      echo "python executable doesn't exist in $INSTALL_PYTHON_DIR/bin"
      exit
    fi
    cd $NUMPY_DIR
    if [ $CLEAN_BEFORE ] ; then
      echo "Cleaning $NUMPY_DIR before making..."
      /bin/rm -rf build
    fi
    echo "Building $NUMPY_DIR..."
    export PATH="$INSTALL_PYTHON_DIR/bin:$PATH"
    echo "----------------------------------------------------------------------"
    echo "Version of python being used to install numpy..."
    which python
    python --version
    echo "----------------------------------------------------------------------"
    python setup.py install
    if [ $? ] ; then
      echo "$NUMPY_DIR build successful."
      if [ $CLEAN_AFTER ] ; then
        echo "Cleaning $NUMPY_DIR after making..."
        /bin/rm -rf build
      fi
    else
      echo "$NUMPY_DIR build NOT successful."
    fi
    cd ..
  else
    echo "Directory $NUMPY_DIR doesn't exist"
  fi
else
  echo "Not building numpy..."
fi

#--------------------------------------------------------------------
# libxml2
# Should not be needed because OPeNDAP now part of NetCDF-4.1
# But might be needed by cairo.
#--------------------------------------------------------------------
if [ $INSTALL_XML2 ] ; then
  if [ -d "$XML2_DIR" ] ; then
    cd $XML2_DIR
    if [ $CLEAN_BEFORE ] ; then
      echo "Cleaning $XML2_DIR before making..."
      make distclean
      make clean
    fi
    echo "Building $XML2_DIR..."
    ./configure --prefix\=$PREFIX --with-zlib\=$PREFIX --with-pic
    make all install
    if [ $? ] ; then
      echo "$XML2_DIR build successful."
      if [ $CLEAN_AFTER ] ; then
        echo "Cleaning $XML2_DIR after making..."
        make distclean
        make clean
      fi
    else
      echo "$XML2_DIR build NOT successful."
    fi
    cd ..
  else
    echo "Directory $XML2_DIR doesn't exist"
  fi
else
  echo "Not building libxml2..."
fi

#--------------------------------------------------------------------
# libdap
# Should not be needed because OPeNDAP now part of NetCDF-4.1
#--------------------------------------------------------------------
if [ $INSTALL_DAP ] ; then
  if [ -d "$DAP_DIR" ] ; then
    cd $DAP_DIR
    if [ $CLEAN_BEFORE ] ; then
      echo "Cleaning $DAP_DIR before making..."
      make distclean
      make clean
    fi
    echo "Building $DAP_DIR..."
    ./configure --prefix\=$PREFIX --with-zlib\=$PREFIX --with-pic
    make all install
    if [ $? ] ; then
      echo "$DAP_DIR build successful."
      if [ $CLEAN_AFTER ] ; then
        echo "Cleaning $DAP_DIR after making..."
        make distclean
        make clean
      fi
    else
      echo "$DAP_DIR build NOT successful."
    fi
    cd ..
  else
    echo "Directory $DAP_DIR doesn't exist"
  fi
else
  echo "Not building libdap..."
fi

#--------------------------------------------------------------------
# libncdap
# Should not be needed because OPeNDAP now part of NetCDF-4.1
#--------------------------------------------------------------------
if [ $INSTALL_NCDAP ] ; then
  if [ -d "$NCDAP_DIR" ] ; then
    cd $NCDAP_DIR
    if [ $CLEAN_AFTER ] ; then
      echo "Cleaning $NCDAP_DIR before making..."
      make distclean
      make clean
    fi
    echo "Building $NCDAP_DIR..."
    ./configure --prefix\=$PREFIX --enable-64bit --with-pic
    make all install
    if [ $? ] ; then
      echo "$NCDAP_DIR build successful."
      if [ $CLEAN_AFTER ] ; then
        echo "Cleaning $NCDAP_DIR after making..."
        make distclean
        make clean
      fi
    else
      echo "$NCDAP_DIR build NOT successful."
    fi
    cd ..
  else
    echo "Directory $NCDAP_DIR doesn't exist"
  fi
else
  echo "Not building libncdap..."
fi
