#! /usr/bin/env bash
#
#
# Build options for PathScale (EKOPATH) Compiler on Linux x86-64
#  first version tested on Fedore Core 3 x86-64 Opteron (Linux batsi)
#  updated and tested on acesgrid (Fedora Core 15), using:
#     module load ekopath	(Version 4.0.10)
# Also tested with MPI on acesgrid, using:
#     module load ekopath	(Version 4.0.10)
#   and with mpich1:
#     module load mvapich
#   or  with mpich2:
#     module load mvapich2
#   or  with openmpi:
#     module load openmpi

#-------
# run with OpenMP: needs to set environment var. OMP_NUM_THREADS
#    and generally, needs to increase the thread stack-size:
#   -  sh,bash:
#     > export OMP_NUM_THREADS=2
#     > export PSC_OMP_STACK_SIZE=400m
#   - csh,tcsh:
#     > setenv OMP_NUM_THREADS 2
#     > setenv PSC_OMP_STACK_SIZE 400m
#-------

if test "x$MPI" = xtrue ; then
  CC=mpicc
  FC=mpif90
  LINK=mpif90
else
  CC=pathcc
  FC=pathf90
  LINK=pathf90
fi

DEFINES='-DWORDLENGTH=4'
EXTENDED_SRC_FLAG='-extend-source'
GET_FC_VERSION="--version"
OMPFLAG='-openmp'
#OMPFLAG='-openmp -OPT:early_mp=ON'

NOOPTFLAGS='-O0 -g'
NOOPTFILES=''

CFLAGS='-O0 -m64'
FFLAGS="$FFLAGS -m64 -convert big_endian"

#- for big setups, compile & link with "-fPIC" or set memory-model to "medium":
 CFLAGS="$CFLAGS -fPIC"
 FFLAGS="$FFLAGS -fPIC"
#-  with FC 19, need to use this without -fPIC (which cancels -mcmodel option):
#CFLAGS="$CFLAGS -mcmodel=medium"
#FFLAGS="$FFLAGS -mcmodel=medium"
#- might want to use '-r8' for fizhi pkg:
#FFLAGS="$FFLAGS -r8"

if test "x$IEEE" = x ; then     #- with optimisation:
   #- full optimisation, trips over MITgcm code because of compiler problems:
   #FOPTIM='-Ofast -LNO:fusion=2 -funsafe-math-optimizations'
   #- high optimisation (-O3 & (loop)fusion=2), might find Pb with few S/R:
   #FOPTIM='-O3 -LNO:fusion=2 -funsafe-math-optimizations'
   #FOPTIM='-O3 -funsafe-math-optimizations'
   #- fully safe: switch to -O2 without (loop)fusion=2 (to fix some restart Pb):
    FOPTIM='-O2 -funsafe-math-optimizations'
    NOOPTFILES="$NOOPTFILES exf_interpolate.F"
   #- multi-threaded: mon_set_iounit.F needs lower optimisation (lower than -02)
    NOOPTFILES="$NOOPTFILES mon_set_iounit.F"
    NOOPTFLAGS='-O1'
else
  if test "x$DEVEL" = x ; then  #- no optimisation + IEEE :
    FOPTIM='-O0 -OPT:roundoff=0:IEEE_arithmetic=1 -fmath-errno -fno-fast-math'
  else                          #- development/check options:
    FFLAGS="$FFLAGS -ffortran-bounds-check -trapuv -g"
    FOPTIM='-O0 -OPT:roundoff=0:IEEE_arithmetic=1 -fmath-errno -fno-fast-math'
  fi
fi

F90FLAGS=$FFLAGS
F90OPTIM=$FOPTIM
GSLLIB='-lgsl -lgslcblas'

INCLUDEDIRS=''
INCLUDES=''
LIBS=''

if [ "x$NETCDF_ROOT" != x ] ; then
    INCLUDEDIR="${NETCDF_ROOT}/include"
    INCLUDES="-I${NETCDF_ROOT}/include"
    LIBDIR="${NETCDF_ROOT}/lib"
    LIBS="-L${NETCDF_ROOT}/lib"
elif [ "x$NETCDF_HOME" != x ]; then
    INCLUDEDIR="${NETCDF_HOME}/include"
    INCLUDES="-I${NETCDF_HOME}/include"
    LIBDIR="${NETCDF_HOME}/lib"
    LIBS="-L${NETCDF_HOME}/lib"
elif [ "x$NETCDF_INC" != x -a "x$NETCDF_LIB" != x ]; then
    NETCDF_INC=`echo $NETCDF_INC | sed 's/-I//g'`
    NETCDF_LIB=`echo $NETCDF_LIB | sed 's/-L//g'`
    INCLUDEDIR="${NETCDF_INC}"
    INCLUDES="-I${NETCDF_INC}"
    LIBDIR="${NETCDF_LIB}"
    LIBS="-L${NETCDF_LIB}"
elif [ "x$NETCDF_INCDIR" != x -a "x$NETCDF_LIBDIR" != x ]; then
    INCLUDEDIR="${NETCDF_INCDIR}"
    INCLUDES="-I${NETCDF_INCDIR}"
    LIBDIR="${NETCDF_LIBDIR}"
    LIBS="-L${NETCDF_LIBDIR}"
elif test -d /usr/include/netcdf-3 ; then
    INCLUDES='-I/usr/include/netcdf-3'
    LIBS='-L/usr/lib/netcdf-3 -L/usr/lib64/netcdf-3'
elif test -d /usr/include/netcdf ; then
    INCLUDES='-I/usr/include/netcdf'
elif test -d /usr/local/netcdf ; then
    INCLUDES='-I/usr/local/netcdf/include'
    LIBS='-L/usr/local/netcdf/lib'
elif test -d /usr/local/include/netcdf.inc ; then
    INCLUDES='-I/usr/local/include'
    LIBS='-L/usr/local/lib64'
fi

if [ -n "$MPI_INC_DIR" -a "x$MPI" = xtrue ] ; then
    INCLUDES="$INCLUDES -I$MPI_INC_DIR"
    INCLUDEDIRS="$INCLUDEDIRS $MPI_INC_DIR"
    #- used for parallel (MPI) DIVA
    MPIINCLUDEDIR="$MPI_INC_DIR"
   #MPI_HEADER_FILES='mpif.h mpiof.h'
fi
