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

_realname=dftbplus
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=24.1
pkgrel=1
pkgdesc="General package for performing fast atomistic simulations. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64')
url='https://www.dftbplus.org'
msys2_repository_url="https://github.com/dftbplus/dftbplus"
license=('spdx:LGPL-3.0-or-later')
depends=($([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         "${MINGW_PACKAGE_PREFIX}-arpack"
         "${MINGW_PACKAGE_PREFIX}-dftd4"
         "${MINGW_PACKAGE_PREFIX}-libmbd"
         "${MINGW_PACKAGE_PREFIX}-libnegf"
         "${MINGW_PACKAGE_PREFIX}-mctc-lib"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-s-dftd3")
         #"${MINGW_PACKAGE_PREFIX}-tblite")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python-fypp")
source=("https://github.com/dftbplus/dftbplus/releases/download/${pkgver}/${_realname}-${pkgver}.tar.xz"
        "001-fix-install-dll.patch"
        "002-fix-build-on-mingw-w64.patch")
sha256sums=('3bc405d1ab834b6b145ca671fb44565ec50a6f576e9e18e7a1ae2c613a311321'
            'b0f1626d38c018fd82fcc75dfde019025c7bb3935634fcaa76963a4bf205fc53'
            '238ceb0c4fa3fd29042a07b73977d6d631fd6a2a98967bb80b2016712bdb832f')
noextract=("${_realname}-${pkgver}.tar.xz")

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

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

  patch -Np1 -i "${srcdir}"/001-fix-install-dll.patch
  patch -Np1 -i "${srcdir}"/002-fix-build-on-mingw-w64.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 \
      -DHYBRID_CONFIG_METHODS=Find \
      -DWITH_API=ON \
      -DWITH_ARPACK=ON \
      -DWITH_MBD=ON \
      -DWITH_OMP=ON \
      -DWITH_SDFTD3=ON \
      -DWITH_TRANSPORT=ON \
      -DWITH_CHIMES=OFF \
      -DWITH_MPI=OFF \
      -DWITH_TBLITE=OFF \
      -DWITH_UNIT_TESTS=OFF \
      -DINSTALL_INCLUDE_FILES=ON \
      -DFYPP=${MINGW_PREFIX}/bin/fypp \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

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

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

  # Fix PkgConfig file
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  sed -e "s|${PREFIX_WIN}|\$\{prefix\}|g" \
    -i "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig/dftbplus.pc
  sed -e "1n;s|${MINGW_PREFIX}|\$\{prefix\}|g" \
    -i "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig/dftbplus.pc

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING* \
    -t "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}
}
