#!/bin/bash

rm -f CMakeCache.txt

TRI_INSTALL_PREFIX=$1
# $2 = C compiler
# $3 = C++ compiler
# $4 = OpenMP flag (not used, controlled by Trilinos_ENABLE_OpenMP)
# $5 = Trilinos source directory
# $6 = METIS enable (ON/OFF)
# $7 = METIS include path
# $8 = METIS library path
# $9 = ParMETIS enable (ON/OFF)
# ${10} = ParMETIS include path
# ${11} = ParMETIS library path
# ${12} = Scotch enable (ON/OFF)
# ${13} = Scotch include path
# ${14} = Scotch library path

METIS_ENABLE=$6
METIS_INC=$7
METIS_LIB=$8
PARMETIS_ENABLE=$9
PARMETIS_INC=${10}
PARMETIS_LIB=${11}
SCOTCH_ENABLE=${12}
SCOTCH_INC=${13}
SCOTCH_LIB=${14}

# Build METIS cmake options
METIS_OPTS="-D TPL_ENABLE_METIS=${METIS_ENABLE}"
if [ "${METIS_ENABLE}" = "ON" ] && [ -n "${METIS_INC}" ]; then
    METIS_OPTS="${METIS_OPTS} -D TPL_METIS_INCLUDE_DIRS=${METIS_INC}"
    # Use .dylib on macOS, .so on Linux
    if [ "$(uname -s)" = "Darwin" ]; then
        METIS_OPTS="${METIS_OPTS} -D TPL_METIS_LIBRARIES=${METIS_LIB}/libmetis.dylib"
    else
        METIS_OPTS="${METIS_OPTS} -D TPL_METIS_LIBRARIES=${METIS_LIB}/libmetis.so"
    fi
fi

# Build ParMETIS cmake options
PARMETIS_OPTS="-D TPL_ENABLE_ParMETIS=${PARMETIS_ENABLE}"
if [ "${PARMETIS_ENABLE}" = "ON" ] && [ -n "${PARMETIS_INC}" ]; then
    PARMETIS_OPTS="${PARMETIS_OPTS} -D TPL_ParMETIS_INCLUDE_DIRS=${PARMETIS_INC}"
    # Use .dylib on macOS, .so on Linux
    if [ "$(uname -s)" = "Darwin" ]; then
        PARMETIS_OPTS="${PARMETIS_OPTS} -D TPL_ParMETIS_LIBRARIES=${PARMETIS_LIB}/libparmetis.dylib"
    else
        PARMETIS_OPTS="${PARMETIS_OPTS} -D TPL_ParMETIS_LIBRARIES=${PARMETIS_LIB}/libparmetis.so"
    fi
fi

# Build Scotch cmake options (includes PT-Scotch for parallel)
SCOTCH_OPTS="-D TPL_ENABLE_Scotch=${SCOTCH_ENABLE}"
if [ "${SCOTCH_ENABLE}" = "ON" ] && [ -n "${SCOTCH_INC}" ]; then
    SCOTCH_OPTS="${SCOTCH_OPTS} -D TPL_Scotch_INCLUDE_DIRS=${SCOTCH_INC}"
    # PT-Scotch provides both serial and parallel libraries
    if [ "$(uname -s)" = "Darwin" ]; then
        SCOTCH_OPTS="${SCOTCH_OPTS} -D TPL_Scotch_LIBRARIES=${SCOTCH_LIB}/libptscotch.dylib;${SCOTCH_LIB}/libptscotcherr.dylib;${SCOTCH_LIB}/libscotch.dylib;${SCOTCH_LIB}/libscotcherr.dylib"
    else
        SCOTCH_OPTS="${SCOTCH_OPTS} -D TPL_Scotch_LIBRARIES=${SCOTCH_LIB}/libptscotch.so;${SCOTCH_LIB}/libptscotcherr.so;${SCOTCH_LIB}/libscotch.so;${SCOTCH_LIB}/libscotcherr.so"
    fi
fi

cmake \
      -D CMAKE_INSTALL_PREFIX=$TRI_INSTALL_PREFIX\
      -D CMAKE_CXX_STANDARD=20 \
      -D Trilinos_ENABLE_Fortran=OFF \
      -D CMAKE_C_COMPILER=$2 \
      -D CMAKE_C_FLAGS=" -Wno-error=implicit-function-declaration " \
      -D CMAKE_CXX_COMPILER=$3 \
      -D CMAKE_CXX_FLAGS=" -Wno-unused-parameter -Wno-error=implicit-function-declaration " \
      -D CMAKE_EXE_LINKER_FLAGS=" -L/opt/homebrew/opt/llvm/lib/c++ -Wl,-rpath,/opt/homebrew/opt/llvm/lib/c++ " \
      -D CMAKE_SHARED_LINKER_FLAGS=" -L/opt/homebrew/opt/llvm/lib/c++ -Wl,-rpath,/opt/homebrew/opt/llvm/lib/c++ " \
      -D BUILD_SHARED_LIBS=ON \
      -D TPL_ENABLE_BLAS=ON \
      -D TPL_ENABLE_Boost=ON \
      -D TPL_ENABLE_Cholmod=OFF \
      -D TPL_ENABLE_SCALAPACK=OFF \
      ${METIS_OPTS} \
      ${PARMETIS_OPTS} \
      ${SCOTCH_OPTS} \
      -D TPL_ENABLE_MUMPS=OFF \
      -D TPL_ENABLE_SuperLU=OFF \
      -D TPL_ENABLE_UMFPACK=OFF \
      -D Trilinos_ENABLE_Amesos2=ON \
      -D Trilinos_ENABLE_Belos=ON \
      -D Trilinos_ENABLE_Ifpack2=ON \
      -D Trilinos_ENABLE_Kokkos=ON \
      -D Trilinos_ENABLE_MueLu=ON \
      -D Trilinos_ENABLE_Teuchos=ON \
      -D Trilinos_ENABLE_Tpetra=ON \
      -D Trilinos_ENABLE_ALL_OPTIONAL_PACKAGES=OFF \
      -D Kokkos_ENABLE_AGGRESSIVE_VECTORIZATION=ON \
      -D Tpetra_INST_COMPLEX_DOUBLE=ON \
      -D Trilinos_ENABLE_COMPLEX_DOUBLE=ON \
      -D Teuchos_ENABLE_COMPLEX=ON \
      -D Teuchos_ENABLE_THREAD_SAFE=ON \
      -D Tpetra_INST_INT_INT=ON \
      -D Trilinos_ENABLE_OpenMP=ON \
      -D TPL_ENABLE_MPI=ON \
      -D Trilinos_ENABLE_EXPLICIT_INSTANTIATION=ON \
      -D Kokkos_ENABLE_COMPILER_WARNINGS=ON \
      -D Amesos2_ENABLE_Basker=ON \
      -D Tpetra_INST_SERIAL:BOOL=ON \
      -D Trilinos_ENABLE_TESTS=OFF \
      -D Trilinos_ENABLE_EXAMPLES=OFF \
      $5
