#!/bin/bash -e

# TODO: Review and if possible fix shellcheck errors.
# shellcheck disable=all

[ "${BASH_SOURCE[0]}" ] && SCRIPT_NAME="${BASH_SOURCE[0]}" || SCRIPT_NAME=$0
SCRIPT_DIR="$(cd "$(dirname "$SCRIPT_NAME")/.." && pwd -P)"

source "${SCRIPT_DIR}"/common_vars.sh
source "${SCRIPT_DIR}"/tool_kit.sh
source "${SCRIPT_DIR}"/signal_trap.sh
source "${INSTALLDIR}"/toolchain.conf
source "${INSTALLDIR}"/toolchain.env

libint_ver="2.13.1"
libint_pkg="libint-v${libint_ver}-cp2k-lmax-${LIBINT_LMAX}.tar.xz"

case "${LIBINT_LMAX}" in
  4)
    libint_sha256="8ff388fbf171635420fdfdbafc7dc949e9cf4c0b6a62f23dac3af8b1c942d407"
    ;;
  5)
    libint_sha256="527000f915bea9879391273b96689a8c2b98beeec37cb64637e3c11dd421a5e8"
    ;;
  6)
    libint_sha256="a7990c4862d549e6328596f20b8a7ece40ded396b953d5d5d7c3200f13e37429"
    ;;
  7)
    libint_sha256="ba19571deefa5c3063e620210c87cf706ef8d75b47f3b0d66547f889f906b3dd"
    ;;
  *)
    report_error "Unsupported value --libint-lmax=${LIBINT_LMAX}."
    exit 1
    ;;
esac

[ -f "${BUILDDIR}/setup_libint" ] && rm "${BUILDDIR}/setup_libint"

! [ -d "${BUILDDIR}" ] && mkdir -p "${BUILDDIR}"
cd "${BUILDDIR}"

case "$with_libint" in
  __INSTALL__)
    echo "==================== Installing LIBINT ===================="
    pkg_install_dir="${INSTALLDIR}/libint-v${libint_ver}-cp2k-lmax-${LIBINT_LMAX}"
    install_lock_file="${pkg_install_dir}/install_successful"
    if verify_checksums "${install_lock_file}"; then
      echo "libint-${libint_ver} is already installed, skipping it."
    else
      retrieve_package "${libint_sha256}" "${libint_pkg}"
      echo "Installing from scratch into ${pkg_install_dir}"
      [ -d libint-v${libint_ver}-cp2k-lmax-${LIBINT_LMAX} ] && rm -rf libint-v${libint_ver}-cp2k-lmax-${LIBINT_LMAX}
      tar -xJf ${libint_pkg}
      cd libint-v${libint_ver}-cp2k-lmax-${LIBINT_LMAX}

      # reduce debug information to level 1 since
      # level 2 (default for -g flag) leads to very large binary size
      LIBINT_CXXFLAGS="$CXXFLAGS -g1"
      LIBINT_FCFLAGS="$FCFLAGS -g1"

      mkdir build
      cd build
      CXXFLAGS="$LIBINT_CXXFLAGS" \
        FCFLAGS="$LIBINT_FCFLAGS" cmake .. \
        -DCMAKE_INSTALL_PREFIX="${pkg_install_dir}" \
        -DCMAKE_CXX_COMPILER="$CXX" \
        -DCMAKE_INSTALL_LIBDIR="lib" \
        -DBUILD_TESTING=OFF \
        -DLIBINT2_ENABLE_FORTRAN=ON \
        -DCMAKE_DISABLE_FIND_PACKAGE_Boost=ON \
        > configure.log 2>&1 || tail_excerpt configure.log
      make install -j $(get_nprocs) > make.log 2>&1 || tail_excerpt make.log

      cd ..
      write_checksums "${install_lock_file}" "${SCRIPT_DIR}/stage3/$(basename ${SCRIPT_NAME})"
    fi
    ;;
  __SYSTEM__)
    echo "==================== Finding LIBINT from system paths ===================="
    check_lib -lint2 "libint"
    pkg_install_dir="$(dirname $(dirname $(find_in_paths "libint2.*" $LIB_PATHS)))"
    ;;
  __DONTUSE__) ;;

  *)
    echo "==================== Linking LIBINT to user paths ===================="
    pkg_install_dir="${with_libint}"
    check_dir "${pkg_install_dir}/lib"
    check_dir "${pkg_install_dir}/include"
    ;;
esac
if [ "$with_libint" != "__DONTUSE__" ]; then
  cat << EOF > "${BUILDDIR}/setup_libint"
export LIBINT2_ROOT="${pkg_install_dir}"
export LIBINT_VER="${libint_ver}"
EOF
  if [ "$with_libint" != "__SYSTEM__" ]; then
    cat << EOF >> "${BUILDDIR}/setup_libint"
prepend_path LD_LIBRARY_PATH "${pkg_install_dir}/lib"
prepend_path LD_RUN_PATH "${pkg_install_dir}/lib"
prepend_path LIBRARY_PATH "${pkg_install_dir}/lib"
prepend_path CMAKE_PREFIX_PATH "${pkg_install_dir}"
EOF
  fi
  filter_setup "${BUILDDIR}/setup_libint" "${SETUPFILE}"
fi

load "${BUILDDIR}/setup_libint"
write_toolchain_env "${INSTALLDIR}"

cd "${ROOTDIR}"
report_timing "libint"
