#!/bin/ksh 

## Script to compile the OSM
## Emanuel Dutra, 2017 

set -eux

LOCALROOT=""  # full path to "surf" folder location 
              # $LOCALROOT/make/osm.cfg 
LOCALROOT="$(cd $(dirname $0);echo $PWD)/../../" # a good guess in case it's not provided 
NTHREADS=1
BUILD=opt
COMP=gnu
EXTRAOPT="-v"
USAGE="USE: ./compile_fcm.ksh  -q LOCALROOT  [-d BUILD:opt (opt/noopt] [-c COMP:gnu (gnu/cce)] [-n NTHREADS:1 ] -o [extra options for fcm e.g. --new -v -vv ]"

while getopts q:d:c:n:o: OPT;
do
    case $OPT in
        b) BRANCH=$OPTARG;;
        q) LOCALROOT=$OPTARG;;
        d) BUILD=$OPTARG;;
        c) COMP=$OPTARG;;
        n) NTHREADS=$OPTARG;;
        o) EXTRAOPT=$OPTARG;;
        \?) 
            echo "Invalid option" 
            echo $USAGE
            exit -1;;
    esac
done
if [[ $LOCALROOT = "" ]]; then
    echo "-q option not set, must provide valid LOCALROOT "
    echo $USAGE
    exit -1
fi

## Specific configuration for ECMWF machines 
## checks for the presence of ECACCOUNT variable that normaly is defined by default 
if [[ "${ECACCOUNT:-unset}" != "unset" ]]; then
  echo "We're at ECMWF"
  module load fcm 
  ## ECMWF HPC 
  if [[ $HOSTNAME = cc* ]]; then
    BUILD=${BUILD:-opt} # default
    COMP=${COMP:-cce}   # default 
    if [[ $COMP == intel ]]; then 
      module switch PrgEnv-cray PrgEnv-intel
    fi
    if [[ $COMP == gnu ]]; then 
      module switch PrgEnv-cray PrgEnv-gnu
    fi
    module load ifs-support/CY43R1.0
    module load cray-netcdf/4.3.2
    module load cray-hdf5/1.8.13
    module load atp
    
    export OSM_COMP=${COMP}
    export OSM_BUILD=${BUILD}

  else  # linux
    BUILD=${BUILD:-opt}
    COMP=${COMP:-gnu}
    module rm gnu
    module rm pgi 
    module load ${COMP}
    module load netcdf4
    module load hdf5
    module load eccodes
    export OSM_COMP=${COMP}
    export OSM_BUILD=${BUILD}  
  fi

# marenostrum4 machine at BSC
elif [[ "${BSC_MACHINE:-unset}" == "mn4" ]]; then
  BUILD=opt
  COMP=intel
  #load modules before launching script since "module" does not work with ksh on MN4
  #module unload intel ; module load intel/2017.4 netcdf hdf5 ; module list
  #TODO use bash instead of ksh!
  export PATH=/gpfs/projects/bsc32/share/fcm-2017.10.0/bin:$PATH
  export NETCDF_DIR=$NETCDF
  export ECCODES_DIR=/gpfs/projects/bsc32/share/eccodes
  export GRIB_API_INCLUDE="-I${ECCODES_DIR}/include"
  export GRIB_API_LIB="-L${ECCODES_DIR}/lib -leccodes_f90"
  export LD_LIBRARY_PATH=${ECCODES_DIR}/lib:$LD_LIBRARY_PATH
  ## All variables in the *.cfg can be defined here and will replace the default
  ## example to use a different fortran compiler and GRIB_API libraries 
  export OSM_FC=ifort
  export OSM_FFLAGS="-g -qopenmp -O3 -traceback -fpe0 -I$NETCDF_DIR/include $GRIB_API_INCLUDE "
  export OSM_LFLAGS="-qopenmp  \
      -L$NETCDF_DIR/lib -lnetcdff -lnetcdf -Wl,-rpath,$NETCDF_DIR/lib  \
      -L$HDF5_DIR/lib -lhdf5 -Wl,-rpath,$HDF5_DIR/lib  \
      $GRIB_API_LIB "

  export OSM_COMP=${COMP}
  export OSM_BUILD=${BUILD} 

else
  # You local configuration
  # Should define NETCDF_DIR and ECCODES_DIR 
  # ECCODES_DIR can also point to GRIB_API_DIR 
  # This is not necessary to compile the OSM only.
  # The grib library is only required to compile some utilities
  # If ECCODES or GRIB_API are not installed: 
  # set ECCODES_DIR=""
  # change surf/make/osm.cfg: 
  # build.target = master1s.exe  
  # i.e. remove all the other targets (create_grid_info.exe, etc... 
  echo "DO SOMETHING"
  
  ## example of local configuration:
  COMP=gnu
  export NETCDF_DIR="/home/emanuel/apps/netcdf4/4.4.1.1/GNU/5/"
  export ECCODES_DIR="/home/emanuel/apps/grib_api/1.19.0/GNU/5"
  ## All variables in the *.cfg can be defined here and will replace the default
  ## example to use a different fortran compiler and GRIB_API libraries 
  export OSM_FC=gfortran-5 
  export OSM_LFLAGS="-fopenmp  \
                   -L$NETCDF_DIR/lib -lnetcdff -lnetcdf -Wl,-rpath,$NETCDF_DIR/lib  \
                   -L$ECCODES_DIR/lib  -lgrib_api -lgrib_api_f90 -Wl,-rpath,$ECCODES_DIR/lib "

  export OSM_COMP=${COMP}
  export OSM_BUILD=${BUILD} 
fi   





VERBOSE="-v" #  empty, -v or -vv
fcm make $EXTRAOPT -j $NTHREADS -f $LOCALROOT/make/osm.cfg
 
