#!/bin/bash


LOCAL=$(pwd)
DST=$( echo ${LOCAL}/../.. )

if [[ -z $( module list 2>&1 >/dev/null |  grep "No modules" ) ]] ; then
    module --force save pre_cmor_lib_compilation
    modules_empty=false
else
    modules_empty=true
fi

module purge

module load intel/2016 openmpi/1.10.2

module load szip/2.1
module load hdf5/1.8.16
module load netcdf-fortran/4.4.3
module load uuid/1.6.2
module load udunits/2.2.20

DIR_UUID=/opt/cesga/uuid/1.6.2/intel/2016
DIR_UDUNITS=/opt/cesga/udunits/2.2.20/intel/2016
DIR_NETCDF=/opt/cesga/netcdf-fortran/4.4.3/gcc/intel/2016/
DIR_HDF5=/opt/cesga/hdf5/1.8.16/intel/2016

export CC=icc
export CFLAGS='-fpic -O3 -ip -no-prec-div -static'

export CPP='icc -E'
export CPPFLAGS='-DpgiFortran'
export CXXCPP='icpc -E'

export F77=ifort
export FC=ifort
export FFLAGS='-fpic -O3 -ip -no-prec-div -static'


set -ev

./configure --prefix=$DST


make
make install

if [[ modules_empty == "false" ]] ; then
    module --force restore pre_cmor_lib_compilation
else
    module purge
fi


date
