#!/bin/bash
#------------------------------------------------------------------------------

# Information about the compilation process
echo -en "

${GREEN}Compilation of LCS4FOAM${NC}
-------------------------------------------------------------------------------
You are about to compile an OpenFOAM function object that allows you to compute
finite-time Lyapunov exponent (FTLE) fields during your simulation.

The compilation consists of two major steps
    1. Compilation of the libcfd2lcs
    2. Compilation of the OpenFOAM function object itself

Before starting the compilation a few prerequisites will be checked:
-------------------------------------------------------------------------------
\n"

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

# Check if mpif90 is in place
mpifortranLocation=`which mpif90 | sed  's/^/\t  /'`
if command -v mpif90 &>/dev/null
then
    echo -e  "\t- ${GREEN}MPI Fortran compiler is in place${NC}"
    echo -e  "\t  Located under"
    echo -e  "${mpifortranLocation}"

else
    echo -e  "\t- ${ORANGE}mpif90 compiler not found."
    echo -e  "\t  The compilation wont work without.${NC}"

    echo -en  "Terminating compilation"
    echo -e "\n\n"
    exit
fi

# Check if fortran compiler is in place
# - Get the fortran compiler that mpif90 links to
fortranLocation=`mpif90 -v |& grep "COLLECT_GCC" | cut -d "=" -f 2`
fortranVersion=`echo $fortranLocation | tr '/' '\n' | tail -n1`
# - Compatible version that gfortran should link to is gfortran-11
compatibleVersions="gfortran-11"
compatibleLocations=`which $compatibleVersions | sed  's/^/\t  /'`

if [ -z "$fortranLocation" ]
then
    errorMessgage=`mpif90 -v |& grep -E "MPI|gfortran" | sed  's/^/\t  /'`
    neededCompiler=`echo $errorMessgage | grep -o ".fortran*"`

    echo -e  "\t- ${ORANGE}Problem detected:"
    echo -e  "${errorMessgage}"

    if command -v $compatibleVersions &>/dev/null
    then
        echo -e  "\t  However, we found the compatible versions:"
        echo -e  "\t  ${compatibleVersions}"
        echo -e  "\t  Located under"
        echo -e  "${compatibleLocations}"

        echo -e  "\n\t To resolve the problem create a symlink that links "
        echo -e  "\t ${neededCompiler} to ${compatibleVersions}${NC}"

        echo -en  "Terminating compilation"
        echo -e "\n\n"
        exit
    else
        echo -e  "\t- ${ORANGE}We also could not find any compatible versions"
        echo -e  "\t  ${compatibleVersions} that one could link to ${neededCompiler}"
        echo -e  "\t  Please install ${compatibleVersions} first.${NC}"

        echo -en  "Terminating compilation"
        echo -e "\n\n"
        exit
    fi

elif command -v $fortranVersion &>/dev/null
then
    echo -e  "\t- ${GREEN}Fortran compiler ${fortranVersion} is in place${NC}"
    echo -e  "\t  Located under"
    echo -e  "\t ${fortranLocation}"

else
    echo -e  "\t- ${ORANGE}${fortranVersion} compiler not found."
    echo -e  "\t  The compilation wont work without.${NC}"

    echo -en  "Terminating compilation"
    echo -e "\n\n"
    exit
fi


liblapackLocation=`dpkg -L liblapack-dev | grep "liblapack.so" \
                   | sed  's/^/\t  /'`

if [ ! -z "$liblapackLocation" ]
then
    echo -e  "\t- ${GREEN}liblapack is in place${NC}"
    echo -e  "\t  Located under"
    echo -e  "${liblapackLocation}"

else
    echo -e  "\t- ${ORANGE}liblapack not found."
    echo -e  "\t  The compilation wont work without.${NC}"

    echo -en  "Terminating compilation"
    echo -e "\n\n"
    exit
fi

# Check the OpenFOAM version
export foamESIVersion="OpenFOAM-v2212"

userFoamVersion=`echo "$WM_PROJECT-$WM_PROJECT_VERSION"`

if [ "$userFoamVersion" == "$foamESIVersion" ]
then
    echo -e  "\t- ${GREEN}$foamESIVersion is sourced${NC}"
    echo -e  "\t  Located under"
    echo -e  "\t  ${WM_PROJECT_DIR}"

else
    echo -e  "\t- ${ORANGE}You have either not sourced any or not the correct "
    echo -e  "\t  OpenFOAM version.${NC}"
    echo -en "\t  The found version is ${ORANGE}$userFoamVersion${NC} "
    echo -e  "and we need ${GREEN}$foamESIVersion${NC}\n"

    while true
    do
        echo -en  "\t- Proceed with your OpenFOAM version (y/n): "
        read proceed

        if [ "$proceed" == "y" ]
        then
            break

        elif [ "$proceed" == "n" ]
        then
            echo -e "\n\n"
            exit

        fi
    done
fi

# Check the openMPI settings
if [ "$WM_MPLIB" == "SYSTEMOPENMPI" ]
then
    echo -e  "\t- ${GREEN}You are using the systems OpenMPI installation.${NC}"
    echo -e  "\t  Located under"
    echo -e  "\t  ${MPI_ARCH_PATH}"

else
    echo -e  "\t- ${ORANGE}You are not using the systems OpenMPI installation"
    echo -e  "\t  but instead are using WM_MPLIB=${WM_MPLIB}${NC}"
    echo -e  "\t  with openMPI being located under ${MPI_ARCH_PATH}"

    while true
    do
        echo -en  "\t- Proceed with your OpenMPI version (y/n): "
        read proceed

        if [ "$proceed" == "y" ]
        then
            break

        elif [ "$proceed" == "n" ]
        then
            echo -e "\n\n"
            exit

        fi
    done
fi

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