#!/bin/bash
##################################################################################################
# A script to prepare the compilation of nextsim.
##################################################################################################
# 1. Before compiling, install:
# - necdf, necdfcxx, boost, openmpi, gcc11:
# sudo apt install libnetcdf-c++4 libnetcdf-dev libblas-dev liblapack-dev libboost-dev \
#	libboost-chrono-dev \ libboost-date-time-dev \ libboost-filesystem-dev \
#	libboost-iostreams-dev \ libboost-log-dev \ libboost-locale-dev \
#	libboost-math-dev \ libboost-mpi-dev \ libboost-program-options-dev \
#	libboost-regex-dev \ libboost-serialization-dev \ libboost-system-dev \ libboost-timer-dev
# - also go to dependencies/gmsh-3.0.6-source
# and run the install_gmsh_for_nextsim.sh WITHOUT changing the CMAKE_PREFIX_INSTALL variable.
# 2. Compiling:
# -check that paths listed below, fit your configuration.
# - run this script to set the environement: ". ./set_env_linux.sh" (NOT ./set_env_linux.sh)
# - run make -j${nproc}

# A couple of lib.so are going to be installed when running make in dependencies/lib :
# libbamg.so libGmsh.so libmapx.so libnextsim.so
# needed to build the model.exec final binary in src/model/bin

###################################################################################################
# Variables needed to be set before running make
###################################################################################################

# Path to current working directory
export NEXTSIMDIR="`pwd`"

# Openmpi compiler
export CC=mpicc.openmpi
export CXX=mpicxx.openmpi
export FC=mpifort.openmpi

# where to install shared libs that are going to be built
export LIBRARYDIR="${NEXTSIMDIR}/dependencies/lib"

# Global flags
export CFLAGS="-O3 -fPIC";                      # NOTE: many other flags are still hard-coded in the Makefiles!
export CCFLAGS="${CFLAGS}";                     # only for `mapx` ???
export CXXFLAGS="-O3 -pthread -fPIC -fopenmp "  # NOTE: many other flags are still hard-coded in the Makefiles!
export LDFLAGS=""
export FFLAGS="-O2 -fopenmp -lstdc++ -fPIC"

# Path to opempi include, libs
export MPI_DIR="/usr/lib/x86_64-linux-gnu/openmpi" ; # default OpenMPI
export MPI_INC_DIR="${MPI_DIR}/include";             # OpenMPI Include
export MPI_LIB_DIR="${MPI_DIR}/lib";                 # OpenMPI Lib

# netcdf libs
export NETCDF_DIR="/usr/lib/x86_64-linux-gnu"
export NETCDF_CXX_DIR="/usr/lib/x86_64-linux-gnu"

# gmsh 3.0.6 libs
export GMSH_DIR="${NEXTSIMDIR}/dependencies"

# boost libs
export BOOST_DIR="/usr"
export BOOST_INCDIR="/usr/include"
export BOOST_LIBDIR="/usr/lib/x86_64-linux-gnu"

# options we dont use, need to be set to false
l_aerobulk=false ; # Call AeroBulk to compute air-sea fluxes ? If true, give the appropriate value for "AEROBULK_DIR" in the arch CASE block...
l_cpl_oasis=false ; # neXtSIM is going to be coupled to something via OASIS ? If true, give the appropriate value for "OASIS_DIR" in the arch CASE block...

if [ ! -f ./model/src/version.hpp ]; then
    cd model/src
    ./version.sh
    cd ../..
fi
