#!/bin/bash
#
# Tested on uv100.awi.de (SGI UV 100, details:
#                         http://www.sgi.com/products/servers/uv/specs.html)
# a) For more speed, provided your data size does not exceed 2GB you can
#    remove -fPIC which carries a performance penalty of 2-6%.
# b) You can replace -fPIC with '-mcmodel=medium -shared-intel' which may
#    perform faster than -fPIC and still support data sizes over 2GB per
#    process but all the libraries you link to must be compiled with
#    -fPIC or -mcmodel=medium
# c) flags adjusted for ifort 12.1.0

FC=ifort
F90C=ifort
CC=icc
# requires that all static libraries are available:
#LINK='ifort -static'
LINK='ifort'
# for adjoint runs the default makedepend often cannot handle enough files
#MAKEDEPEND=tools_xmakedepend

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

NOOPTFLAGS="-O0 -g -m64"
NOOPTFILES=''

MCMODEL='-fPIC'
# for large memory requirements uncomment this line
#MCMODEL='-mcmodel=medium -shared-intel'

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

if test "x$IEEE" = x ; then     #- with optimisation:
    FOPTIM='-O3 -align'
# does not work when -static does not work
#    FOPTIM='-fast -align'
# instead you can use
#    FOPTIM='-O3 -ipo -align'
else
  if test "x$DEVEL" = x ; then  #- no optimisation + IEEE :
    FOPTIM='-O0 -noalign -fp-model precise'
   # -fltconsistency
  else                          #- development/check options:
    FFLAGS="$FFLAGS -warn all -warn nounused"
    FOPTIM="-fpe0 -fp-model precise -fp-model except -check all -ftrapuv"
    FOPTIM="$FOPTIM -O0 -noalign  -g -traceback"
  fi
fi

F90FLAGS=$FFLAGS
F90OPTIM=$FOPTIM
CFLAGS="-O0 -ip $MCMODEL"

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 -L/usr/lib64/netcdf-3'
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 -d /usr/local/include/netcdf.inc ; then
    INCLUDEDIRS='/usr/local/include'
    INCLUDES='-I/usr/local/include'
    LIBS='-L/usr/local/lib64'
fi
# uv100 special: 
# special libraries are require for specific versions of ifort/netcdf
if [ "x$NETCDF_LD" != x ] ; then
    LIBS="$NETCDF_LD"
fi

if [ -n "$MPI_INC_DIR" -a "x$MPI" = xtrue ] ; then
    LIBS="$LIBS -lmpi"
    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
