#!/bin/bash 
INSTALL_PATH=$(pwd)/local
mkdir -p $INSTALL_PATH/src 

# CURL
APP=curl-7.26.0
CURL_PATH=$INSTALL_PATH/$APP
rm -rf $CURL_PATH 
cd $INSTALL_PATH/src 
rm -rf curl-7.26.0* 
wget ftp://ftp.unidata.ucar.edu/pub/netcdf/netcdf-4/curl-7.26.0.tar.gz
tar xvf curl-7.26.0.tar.gz ; cd curl-7.26.0
CC=gcc ./configure \
--prefix=$INSTALL_PATH | tee $APP.config.log
make 2>&1 | tee $APP.make.log
#make test 2>&1 | tee $APP.make_test.log
make install 2>&1 | tee $APP.install.log

# ZLIB
APP=zlib-1.2.8
ZLIB_PATH=$INSTALL_PATH/$APP 
rm -rf $ZLIB_PATH 
cd $INSTALL_PATH/src 
rm -rf zlib-1.2.8* 
wget ftp://ftp.unidata.ucar.edu/pub/netcdf/netcdf-4/zlib-1.2.8.tar.gz
tar zxf zlib-1.2.8.tar.gz ; cd zlib-1.2.8
CC=gcc FC=gfortran CXX=g++ ./configure \
--prefix=$INSTALL_PATH | tee $APP.config.log
make 2>&1 | tee $APP.make.log
make check 2>&1 | tee $APP.make_check.log
make install 2>&1 | tee $APP.install.log

# HDF5
APP=hdf5-1.8.13
HDF5_PATH=$INSTALL_PATH/$APP 
rm -rf $HDF5_PATH 
cd $INSTALL_PATH/src 
rm -rf hdf5-1.8.13* 
wget ftp://ftp.unidata.ucar.edu/pub/netcdf/netcdf-4/hdf5-1.8.13.tar.gz
tar xzf hdf5-1.8.13.tar.gz ; cd hdf5-1.8.13
CC=mpicc FC=mpif90 CXX=mpiCC ./configure \
--prefix=$INSTALL_PATH \
--enable-fortran \
--enable-parallel \
--with-zlib=$INSTALL_PATH \
--with-pic 2>&1 | tee $APP.config.log
make 2>&1 | tee $APP.make.log
make check 2>&1 | tee $APP.make_check.log
make install 2>&1 | tee $APP.install.log

# NetCDF4
APP=netcdf-4.3.3.1
NETCDF4_PATH=$INSTALL_PATH/$APP
rm -rf $NETCDF4_PATH 
cd $INSTALL_PATH/src 
rm -rf netcdf-4.3.3.1* 
wget ftp://ftp.unidata.ucar.edu/pub/netcdf/netcdf-4.3.3.1.tar.gz
tar xzf netcdf-4.3.3.1.tar.gz ; cd netcdf-4.3.3.1 
export LDFLAGS="-L${INSTALL_PATH}/lib -L${INSTALL_PATH}/lib -L${INSTALL_PATH}/lib -lmpi"
export CFLAGS="-I${INSTALL_PATH}/include -I${INSTALL_PATH}/include -I${INSTALL_PATH}/include"
export LD_LIBRARY_PATH="${INSTALL_PATH}/lib:${INSTALL_PATH}/lib:${INSTALL_PATH}/lib"
CC=mpicc ./configure \
--prefix=$INSTALL_PATH \
--enable-static \
--enable-shared \
--enable-netcdf4 \
--enable-dap \
--with-pic 2>&1 | tee $APP.config.log
make 2>&1 | tee $APP.make.log
make check 2>&1 | tee $APP.make_check.log
make install 2>&1 | tee $APP.install.log

# NetCDF4-Fortran
APP=netcdf-fortran-4.4.2
NETCDF4_FORTRAN_PATH=$INSTALL_PATH/$APP
rm -rf $NETCDF4_FORTRAN_PATH
cd $INSTALL_PATH/src 
rm -rf netcdf-fortran-4.4.2* 
wget ftp://ftp.unidata.ucar.edu/pub/netcdf/netcdf-fortran-4.4.2.tar.gz
tar xzf netcdf-fortran-4.4.2.tar.gz ; cd netcdf-fortran-4.4.2
export LD_LIBRARY_PATH=${INSTALL_PATH}/lib:${LD_LIBRARY_PATH}
export LDFLAGS="-L${INSTALL_PATH}/lib -L${INSTALL_PATH}/lib -L${INSTALL_PATH}/lib -L${INSTALL_PATH}/lib -lmpi"
export CPPFLAGS="-I${INSTALL_PATH}/include -I${INSTALL_PATH}/include -I${INSTALL_PATH}/include -I${INSTALL_PATH}/include"
CC=mpicc FC=mpif90 F77=mpif77 LDFLAGS=-L${INSTALL_PATH}/lib ./configure \
--prefix=$INSTALL_PATH 2>&1 | tee $APP.config.log
make 2>&1 | tee $APP.make.log
make check 2>&1 | tee $APP.make_check.log
make install 2>&1 | tee $APP.install.log

# NetCDF4-C++
APP=netcdf-cxx4-4.2.1
NETCDF4_CXX_PATH=$INSTALL_PATH/$APP
rm -rf $NETCDF4_CXX_PATH
cd $INSTALL_PATH/src 
rm -rf netcdf-cxx4-4.2.1* 
wget https://github.com/Unidata/netcdf-cxx4/archive/v4.2.1.tar.gz
mv v4.2.1.tar.gz netcdf-cxx4-4.2.1.tar.gz
tar xzf netcdf-cxx4-4.2.1.tar.gz ; cd netcdf-cxx4-4.2.1
export LD_LIBRARY_PATH=${INSTALL_PATH}/lib:${LD_LIBRARY_PATH}
export LDFLAGS="-L${INSTALL_PATH}/lib -L${INSTALL_PATH}/lib -L${INSTALL_PATH}/lib -L${INSTALL_PATH}/lib  -lmpi"
export CPPFLAGS="-I${INSTALL_PATH}/include -I${INSTALL_PATH}/include -I${INSTALL_PATH}/include -I${INSTALL_PATH}/include"
CC=mpicc CXX=mpiCC LDFLAGS=-L${INSTALL_PATH}/lib ./configure \
--prefix=$INSTALL_PATH 2>&1 | tee $APP.config.log
make 2>&1 | tee $APP.make.log
make check 2>&1 | tee $APP.make_check.log
make install 2>&1 | tee $APP.install.log
