
#!/bin/bash

# This bash script is used to set up an environment
# for the Fortran Package Manager (FPM). By specifying
# compiler, linking libraries and flags, the "fpm build"
# command will automatically make use of the evironmental
# variables specified below.

# Note: Script makes use of builds from Univ. of Utah CHPC
# Include module names or update paths as required.

# ------------------------------------------------------------
# ------------ USER SETTINGS ---------------------------------
# ------------------------------------------------------------


# Specify Compiler of choice
# gfortran, nvfortran or ifort
compiler="gfortran"
comp_version="8.5.0"

base_netcdf_path="/path/to/location/of/builds"

# ------------------------------------------------------------
# ------------------------------------------------------------
# ------------------------------------------------------------


# COMPILER SELECTION
if [[ $compiler == "nvfortran" ]]; then
    echo -e "Using nvfortran"
    LIB_NETCDF="${base_netcdf_path}/nvhpc-${comp_version}/netcdf/lib"
    INC_NETCDF="${base_netcdf_path}/nvhpc-${comp_version}/netcdf/include"

elif [[ $compiler == "gfortran" ]]; then
    echo -e "Using gfortran"
    LIB_NETCDF="${base_netcdf_path}/gcc-${comp_version}/netcdf/lib"
    INC_NETCDF="${base_netcdf_path}/gcc-${comp_version}/netcdf/include"

elif [[ $compiler == "ifort" ]]; then
    echo -e "Using ifort"
    LIB_NETCDF="${base_netcdf_path}/intel-${comp_version}/netcdf/lib"
    INC_NETCDF="${base_netcdf_path}/intel-${comp_version}/netcdf/include"
    
else
    echo -e "Unkown compiler specification.\nDefaulting to gfortran."
    compiler = "gfortran"
    LIB_NETCDF="${base_netcdf_path}/gcc-${comp_version}/netcdf/lib"
    INC_NETCDF="${base_netcdf_path}/gcc-${comp_version}/netcdf/include"
fi

compiler_path=$(which $compiler)
export FPM_FC="${compiler_path}"


# LINKING FLAG SELECTION

export FPM_LDFLAGS="-L${LIB_NETCDF} -Wl,-rpath=${LIB_NETCDF}"

# FLAG SELECTION
FPM_FFLAGS+=" -I${INC_NETCDF} -g -O2 -ffree-line-length-none"
export FPM_FFLAGS