# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=libint
pkgbase=mingw-w64-${_realname}2
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}2")
pkgver=2.9.0
pkgrel=2
pkgdesc="high-performance library for computing Gaussian integrals in quantum mechanics (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://github.com/evaleev/libint'
msys2_references=(
  'aur: libint2'
)
license=('spdx:LGPL-3.0-or-later')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             "${MINGW_PACKAGE_PREFIX}-gmp"
             "${MINGW_PACKAGE_PREFIX}-mpfr"
             "${MINGW_PACKAGE_PREFIX}-python")
optdepends=("${MINGW_PACKAGE_PREFIX}-boost"
            "${MINGW_PACKAGE_PREFIX}-eigen3"
            "${MINGW_PACKAGE_PREFIX}-gmp"
            "${MINGW_PACKAGE_PREFIX}-mpfr")
source=("https://github.com/evaleev/libint/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        #https://github.com/evaleev/libint/releases/download/v${pkgver}/${_realname}-${pkgver}.tgz
        "001-fix-build-on-mingw.patch"
        "002-cmake-fix-build-on-mingw.patch::https://github.com/evaleev/libint/commit/7720248d.patch")
sha256sums=('4929b2f2d3e53479270be052e366e8c70fa154a7f309e5c2c23b7d394159687d'
            'd60290391aad41601495b0717c53d8b888327e6c67932d633c3e731016626f80'
            '723b3a7520bd4cce149092e5ee104aa19d61ca99ce64b52de18a7215861b3006')
noextract=("${_realname}-${pkgver}.tar.gz")

_generate_source() {
  cd "${srcdir}"
  mkdir -p generated && cd generated

  local LMAX=5
  ../${_realname}-${pkgver}/configure \
    --srcdir=../${_realname}-${pkgver} \
    --with-cxxgen-optflags="${CXXFLAGS}" \
    --enable-eri=1 \
    --enable-eri3=1 \
    --enable-eri2=1 \
    --with-max-am=${LMAX} \
    --with-opt-am=3 \
    --with-eri-max-am=${LMAX},$((LMAX-1)) \
    --with-eri2-max-am=$((LMAX+2)),$((LMAX+1)) \
    --with-eri3-max-am=$((LMAX+2)),$((LMAX+1)) \
    --with-multipole-max-order=4 \
    --enable-generic-code \
    --disable-unrolling

  sed -s "s|\.\.\.|..|g" -i Makefile
  make export
}

prepare() {
  echo "Extracting ${_realname}-${pkgver}.tar.gz..."
  tar -xzf ${_realname}-${pkgver}.tar.gz || true

  cd "${srcdir}"/${_realname}-${pkgver}

  patch -p1 -i "${srcdir}"/001-fix-build-on-mingw.patch
  patch -p1 -i "${srcdir}"/002-cmake-fix-build-on-mingw.patch

  mv lib/basis/6-31g\*.g94 lib/basis/6-31gs.g94
  mv lib/basis/6-31g\*\*.g94 lib/basis/6-31gss.g94
  mv lib/basis/6-311g\*\*.g94 lib/basis/6-311gss.g94

  ./autogen.sh

  _generate_source
  cd "${srcdir}"/generated
  tar -xzf ${_realname}-${pkgver}.tgz
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DENABLE_FORTRAN=ON \
      -DENABLE_MPFR=ON \
      -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
      -S generated/${_realname}-${pkgver} \
      -B build-${MSYSTEM}

  "${MINGW_PREFIX}"/bin/cmake.exe --build "build-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install "build-${MSYSTEM}"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/libint2/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  # Remove some unrequired files
  find "${pkgdir}"${MINGW_PREFIX}/ -type f -name '\._*' -exec rm {} \;

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}2/LICENSE
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}2/COPYING
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING.LESSER \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}2/COPYING.LESSER
}
