#! /bin/bash +x

# conda deactivate

. setenv.bash

mkdir -p $YOUR_INSTALL_ROOT

#
# Checking paths...
#
echo "zlib=$ZDIR" 
echo "hdf5=$H5DIR" 
echo "netcdf=$NFDIR" 
echo "pnetcdf=$PNDIR" 
echo "path=$PATH"
#
# Installing libs...
#

#
# ZLIB
#

zlib_version=v1.3.1
echo " "
echo "Instaling zlib version $zlib_version ... please wait"
echo " "
cd $YOUR_INSTALL_ROOT
git clone https://github.com/madler/zlib zlib_src
cd zlib_src   
git checkout tags/$zlib_version -b branch_$zlib_version
####
#rm -rf $MY_LOCAL_DIR/lib/zlib_src
#cp -r $MY_LOCAL_DIR/src/zlib_src $MY_LOCAL_DIR/lib
#cd zlib_src   
####
mkdir -p ${ZDIR}
./configure --prefix=${ZDIR}
make check
make install
cd $MY_LOCAL_DIR


#
# HDF5
#

hdf5_version=hdf5-1_12_1
echo " "
echo "Instaling hdf5 version $hdf5_version ... please wait"
echo " "
cd $YOUR_INSTALL_ROOT
git clone https://github.com/HDFGroup/hdf5 hdf5_src 
cd hdf5_src
git checkout tags/$hdf5_version -b branch_$hdf5_version
####
#rm -rf $MY_LOCAL_DIR/lib/hdf5_src
#cp -r $MY_LOCAL_DIR/src/hdf5_src $MY_LOCAL_DIR/lib
#cd hdf5_src
####
mkdir -p ${H5DIR}
./configure --enable-parallel --enable-fortran --prefix=${H5DIR} --with-zlib=${ZDIR}
make
make install
cd $MY_LOCAL_DIR


#
# NETCDF-C
#

netcdf_c_version=v4.8.1
echo " "
echo "Instaling netcdf-c version $netcdf_c_version ... please wait"
echo " "
cd $YOUR_INSTALL_ROOT
git clone http://github.com/Unidata/netcdf-c netcdf-c_src 
cd netcdf-c_src
git checkout tags/${netcdf_c_version} -b branch_${netcdf_c_version} 
####
#rm -rf $MY_LOCAL_DIR/lib/netcdf-c_src
#cp -r $MY_LOCAL_DIR/src/netcdf-c_src $MY_LOCAL_DIR/lib
#cd netcdf-c_src
####
mkdir ${NFDIR}
CPPFLAGS="-I${H5DIR}/include -I$ZDIR/include"
LDFLAGS="-L${H5DIR}/lib -L$ZDIR/lib"
./configure --prefix=${NFDIR}  --enable-netcdf4 --disable-hdf5 --disable-shared
make 
make check
make install
cd $MY_LOCAL_DIR


#
# PNETCDF
#

pnetcdf_version=checkpoint.1.12.2
echo " "
echo "Instaling pnetcdf version $pnetcdf_version ... please wait"
echo " "
cd $YOUR_INSTALL_ROOT
git clone https://github.com/Parallel-NetCDF/PnetCDF PnetCDF_src
cd PnetCDF_src
git checkout tags/$pnetcdf_version -b branch_$pnetcdf_version
####
#rm -rf $MY_LOCAL_DIR/lib/PnetCDF_src
#cp -r $MY_LOCAL_DIR/src/PnetCDF_src $MY_LOCAL_DIR/lib
#cd PnetCDF_src
####
autoreconf -i
mkdir $PNDIR
#FC=/opt/ohpc/pub/mpi/openmpi4-gnu9/4.1.1/bin/mpif90
#CC=/opt/ohpc/pub/mpi/openmpi4-gnu9/4.1.1/bin/mpicc
#CXX=/opt/ohpc/pub/mpi/openmpi4-gnu9/4.1.1/bin/mpicxx
FC=mpif90
CC=mpicc
CXX=mpicxx
./configure --prefix=$PNDIR
#./configure --prefix=$PNDIR \
#                  MPICC=mpicc   MPICXX=mpicxx \
#                  MPIF77=mpif77 MPIF90=mpif90 \
#                  CC=gcc CXX=g++ F77=gfortran FC=gfortran
make -j8
make install
cd $MY_LOCAL_DIR

#
# NETCDF-FORTRAN
#

export CC=/opt/ohpc/pub/mpi/openmpi4-gnu9/4.1.1/bin/mpicc
export FC=/opt/ohpc/pub/mpi/openmpi4-gnu9/4.1.1/bin/mpif90
export CPPFLAGS="-I$H5DIR/include -I$ZDIR/include -I$NFDIR/include"
export LDFLAGS="-L$H5DIR/lib -L$ZDIR/lib -L$NFDIR/lib"
export LD_LIBRARY_PATH=${H5DIR}/lib:$ZDIR/lib:$NFDIR/lib${LD_LIBRARY_PATH}
export LIBS="-lnetcdf -lhdf5_hl -lhdf5 -lz"

echo "cc=$CC" 
echo "fc=$FC" 
echo "cppflags=$CPPFLAGS" 
echo "ldflags=$LDFLAGS" 
echo "ld_library_path=$LD_LIBRARY_PATH" 
echo "libs=$LIBS"

netcdff_version=v4.5.4
echo " "
echo "Instaling netcdf-fortran version $netcdff_version ... please wait"
echo " "
cd $YOUR_INSTALL_ROOT
git clone http://github.com/Unidata/netcdf-fortran netcdf-fortran_src
cd netcdf-fortran_src
git checkout tags/${netcdff_version} -b branch_${netcdff_version} 
####
#rm -rf $MY_LOCAL_DIR/lib/netcdf-fortran_src
#cp -r $MY_LOCAL_DIR/src/netcdf-fortran_src $MY_LOCAL_DIR/lib
#cd netcdf-fortran_src
####
./configure --disable-shared --prefix=${NCDIR}
make
make install
cd $MY_LOCAL_DIR


#
# Moving/Copying libs...
#

# Nothing here yet
