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

_realname=tblite
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.5.0
pkgrel=2
pkgdesc="Light-weight tight-binding framework. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64')
url='https://tblite.readthedocs.io/'
msys2_repository_url="https://github.com/tblite/tblite"
license=('spdx:LGPL-3.0-or-later')
depends=($([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         "${MINGW_PACKAGE_PREFIX}-dftd4"
         "${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-mctc-lib"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-s-dftd3"
         "${MINGW_PACKAGE_PREFIX}-toml-f")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
checkdepends=("${MINGW_PACKAGE_PREFIX}-mstore")
source=("https://github.com/tblite/tblite/releases/download/v${pkgver}/tblite-${pkgver}.tar.xz"
        "001-fix-install-dll.patch"
        "002-fix-build-with-dftd4-4.patch::https://github.com/tblite/tblite/pull/237.patch")
sha256sums=('e8a70b72ed0a0db0621c7958c63667a9cd008c97c868a4a417ff1bc262052ea8'
            '45f7108ef51ab31ca0956e0ce7b896a0e98cea2a32bb1240d349cf2f3c202418'
            '34a450854e261c68056f356d8ed43812e18f33d4664869c7a591af8457d1ec03')

prepare() {
  cd "${_realname}-${pkgver}"

  patch -Np1 -i "${srcdir}"/001-fix-install-dll.patch
  patch -Np1 -i "${srcdir}"/002-fix-build-with-dftd4-4.patch
}

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=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DWITH_API=ON \
      -DWITH_OpenMP=ON \
      -DWITH_TESTS=OFF \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  cmake --build "build-${MSYSTEM}"
}

check() {
  cmake -DWITH_TESTS=ON -S"${_realname}-${pkgver}" -B"build-${MSYSTEM}"
  cmake --build "build-${MSYSTEM}"

  ctest --test-dir "build-${MSYSTEM}" --output-on-failure
}

package() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}"
}
