#!/bin/bash
#
#
#  Constantinos Evangelinos
#
#  Build options for the intel 10.1 fortran compiler (with/without MPI)
#
#  with MPI : need to set environment variable MPI_INC_DIR to the include
#             directory of your MPI implementation

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

if test "x$MPI" = xtrue ; then
  CC=${CC:=mpicc}
  FC=${FC:=mpif77}
  F90C=${F90C:=mpif90}
  LINK="$F90C -shared-intel"
else
  CC=icc
  FC=ifort
  F90C=ifort
  LINK=$FC
fi

DEFINES='-DWORDLENGTH=4'
CPP='cpp  -traditional -P'
F90FIXEDFORMAT='-fixed -Tf'
EXTENDED_SRC_FLAG='-132'
OMPFLAG='-openmp'

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

FFLAGS="$FFLAGS -convert big_endian -assume byterecl"
#- might want to use '-r8' for fizhi pkg:
#FFLAGS="$FFLAGS -r8"

if test "x$GENERIC" != x ; then
    PROCF=-axSOTPNWK
else
    has_sse4a=f
    has_sse4=f
    has_ssse3=f
    has_sse3=f
    has_sse2=f
    has_sse=f
    is_opteron=f
    is_pentiumm=f
    grep flags /proc/cpuinfo | grep " sse4a " > /dev/null 2>&1  &&  has_sse4a=t
    grep flags /proc/cpuinfo | grep " sse4 " > /dev/null 2>&1  &&  has_sse4=t
    grep flags /proc/cpuinfo | grep " sse3 " > /dev/null 2>&1  &&  has_ssse3=t
    grep flags /proc/cpuinfo | grep " sse3 " > /dev/null 2>&1  &&  has_sse3=t
    grep flags /proc/cpuinfo | grep " sse2 " > /dev/null 2>&1  &&  has_sse2=t
    grep flags /proc/cpuinfo | grep " sse " > /dev/null 2>&1  &&  has_sse=t
    grep "model name" /proc/cpuinfo | grep " Opteron" >  /dev/null 2>&1  && is_opteron=t
    grep "model name" /proc/cpuinfo | grep " Phenom" >  /dev/null 2>&1  && is_opteron=t
    grep "model name" /proc/cpuinfo | grep " Athlon 64" >  /dev/null 2>&1  && is_opteron=t
    grep "model name" /proc/cpuinfo | grep " Pentium(R) M" >  /dev/null 2>&1  && is_pentiumm=t
    if test "x$has_sse4a" = xt ; then
	PROCF=-xO
    elif test "x$has_sse4" = xt ; then
	PROCF=-xS
    elif test "x$has_ssse3" = xt ; then
	PROCF=-xT
    elif test "x$has_sse3" = xt ; then
	if test "x$is_opteron" = xt ; then
	    PROCF=-xO
	else
	    PROCF=-xP
	fi
    elif test "x$has_sse2" = xt ; then
	if test "x$is_pentiumm" = xt ; then
	    PROCF=-xB
	elif test "x$is_opteron" = xt ; then
	    PROCF=-xW
	else
	    PROCF=-xN
	fi
    elif test "x$has_sse" = xt ; then
	PROCF=-xK
    fi
fi

if test "x$DEVEL" = x ; then
    FFLAGS="$FFLAGS -W0 -WB"
else
    FFLAGS="$FFLAGS -O0 -g -debug all -debug-parameters all -noalign -fp-stack-check -check all -fpe0 -traceback -ftrapuv -fp-model strict -warn all"
fi

#  Note that the -mp switch is for ieee "maintain precision" and is
#  roughly equivalent to -ieee
if test "x$IEEE" = x ; then     #- with optimisation:
    FOPTIM="-O3 -align -ip -opt-streaming-stores auto $PROCF"
else
  if test "x$DEVEL" = x ; then  #- no optimisation + IEEE :
    FOPTIM="-O0 -mp -noalign $PROCF"
  else                          #- development/check options:
    FOPTIM="$PROCF"
  fi
fi

F90FLAGS=$FFLAGS
F90OPTIM=$FOPTIM
CFLAGS="-O0 $PROCF"

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

if [ "x$NETCDF_ROOT" != x ] ; then
    INCLUDEDIRS="${NETCDF_ROOT}/include"
    INCLUDES="-I${NETCDF_ROOT}/include"
    LIBS="-L${NETCDF_ROOT}/lib"
elif [ "x$NETCDF_HOME" != x ]; then
    INCLUDEDIRS="${NETCDF_HOME}/include"
    INCLUDES="-I${NETCDF_HOME}/include"
    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'`
    INCLUDEDIRS="${NETCDF_INC}"
    INCLUDES="-I${NETCDF_INC}"
    LIBS="-L${NETCDF_LIB}"
elif [ "x$NETCDF_INCDIR" != x -a "x$NETCDF_LIBDIR" != x ]; then
    INCLUDEDIRS="${NETCDF_INCDIR}"
    INCLUDES="-I${NETCDF_INCDIR}"
    LIBS="-L${NETCDF_LIBDIR}"
elif test -d /usr/include/netcdf-3 ; then
    INCLUDEDIRS='/usr/include/netcdf-3'
    INCLUDES='-I/usr/include/netcdf-3'
    LIBS='-L/usr/lib/netcdf-3'
elif test -d /usr/local/pkg/netcdf ; then
    INCLUDEDIRS='/usr/local/pkg/netcdf/include'
    INCLUDES='-I/usr/local/pkg/netcdf/include'
    LIBS='-L/usr/local/pkg/netcdf/lib'
elif test -d /usr/include/netcdf ; then
    INCLUDEDIRS='/usr/include/netcdf'
    INCLUDES='-I/usr/include/netcdf'
elif test -d /usr/local/netcdf ; then
    INCLUDEDIRS='/usr/include/netcdf/include'
    INCLUDES='-I/usr/local/netcdf/include'
    LIBS='-L/usr/local/netcdf/lib'
elif test -f /usr/local/include/netcdf.inc ; then
    INCLUDEDIRS='/usr/local/include'
    INCLUDES='-I/usr/local/include'
    LIBS='-L/usr/local/lib'
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
