#!/bin/bash
#

if [[ -z $( module list 2>&1 >/dev/null |  grep "No modules" ) ]] ; then
    module --force save pre_specs2cmor_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 udunits/2.2.20
module load zlib
module load uuid/1.6.2
module load expat

module save modules_specs2cmor

UUID=/opt/cesga/uuid/1.6.2/intel/2016/
UDUNITS=/opt/cesga/udunits/2.2.20/intel/2016/
NetCDF=/opt/cesga/netcdf-fortran/4.4.3/intel/2016/
HDF5=/opt/cesga/hdf5/1.8.16/intel/2016/
ZLIB=/opt/cesga/zlib/1.2.8/intel/2016/
EXPAT=/opt/cesga/expat/intel/2016

#set -xuve
date

DST=../bin; mkdir -p $DST

F90=ifort

F90FLAGS="-g -traceback -r8 -static -O0 -debug all -check all"

CMOR=../../../lib/
#-L${UDUNITS}/lib -ludunits2 -lexpat \

$F90  $F90FLAGS  cmor-oce.F90 -o cmor-oce.exe \
      -L${CMOR}/lib -lcmor \
      -I${CMOR}/include \
      -L${UUID}/lib -luuid \
      -I${UUID}/include \
      -L${UDUNITS}/lib -ludunits2 \
      -I${UDUNITS}/include \
      -I${EXPAT}/include \
      -L${EXPAT}/lib  -lexpat \
      -L${NetCDF}/lib -lnetcdff -lnetcdf \
      -I${NetCDF}/include \
      -L${HDF5}/lib -lhdf5_hl -lhdf5 \
      -I${HDF5}/include \
      -L${ZLIB}/lib -lsz -lz \
      -I${ZLIB}/include

mv cmor-oce.exe $DST/cmor-oce.exe


#      -L${EMOS}/lib -lgribexR64 \
$F90  $F90FLAGS  cmor-atm.F90 -o cmor-atm.exe \
      -L${CMOR}/lib -lcmor \
      -I${CMOR}/include \
      -L${UUID}/lib -luuid \
      -I${UUID}/include \
      -L${UDUNITS}/lib -ludunits2 \
      -I${UDUNITS}/include \
      -I${EXPAT}/include \
      -L${EXPAT}/lib  -lexpat \
      -L${NetCDF}/lib -lnetcdff -lnetcdf \
      -I${NetCDF}/include \
      -L${HDF5}/lib -lhdf5_hl -lhdf5 \
      -I${HDF5}/include \
      -L${ZLIB}/lib -lsz -lz \
      -I${ZLIB}/include

mv cmor-atm.exe $DST/cmor-atm.exe

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

