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

_realname=gau2grid
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.0.7
pkgrel=2
pkgdesc="Fast computation of a gaussian and its derivative on a grid. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://gau2grid.readthedocs.io/en/latest'
msys2_repository_url="https://github.com/dgasmith/gau2grid"
msys2_references=(
  'aur: gau2grid'
)
license=('spdx:BSD-3-Clause')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-python-numpy")
source=("https://github.com/dgasmith/gau2grid/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-fix-find-python.patch"
        "002-fix-pragmas.patch")
sha256sums=('66e7205646e1e3685e5dd4eea8281fc92b0b8b45ce97ae24b72a09e15a3fd62f'
            '52ec7e21e0b49548f60a672333aca4b5cf89135ae27ddd84371c5c9bf16a19fe'
            '8c1a8a698913f854c04d59bc461b865ec77c471b7a31ec31bac3779f5cae3e00')

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

  patch -Np1 -i "${srcdir}"/001-fix-find-python.patch
  patch -Np1 -i "${srcdir}"/002-fix-pragmas.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

  _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  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_XHOST=OFF \
      -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python \
      -DPYTHON_LIBRARY=${MINGW_PREFIX}/lib/libpython${_pyver}.dll.a \
      -DPYTHON_INCLUDE_DIR=${MINGW_PREFIX}/include/python${_pyver} \
      -DPYMOD_INSTALL_LIBDIR=python${_pyver}/site-packages \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

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

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

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