# Maintainer: ImperatorS79 <fevrier.simon@gmail.com>

_realname=mpi4py
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=4.1.1
pkgrel=1
pkgdesc="Python bindings for the Message Passing Interface (MPI) standard (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
url='https://mpi4py.github.io/mpi4py/stable/'
msys2_repository_url="https://github.com/mpi4py/mpi4py"
msys2_changelog_url='https://mpi4py.readthedocs.io/en/stable/changes.html'
msys2_references=(
  'archlinux: python-mpi4py'
  'gentoo: dev-python/mpi4py'
  'purl: pkg:pypi/mpi4py'
)
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-cython"
             "${MINGW_PACKAGE_PREFIX}-msmpi")
source=("https://github.com/mpi4py/mpi4py/releases/download/${pkgver}/mpi4py-${pkgver}.tar.gz")
sha256sums=('eb2c8489bdbc47fdc6b26ca7576e927a11b070b6de196a443132766b3d0a2a22')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}/"
  export MINGW_PATH=$(cygpath -m ${MINGW_PREFIX})
  sed -i 's@## mpi_dir              = /usr/local/mpi@mpi_dir              = '"${MINGW_PATH}"'@g' mpi.cfg
  sed -i 's@## mpicc                = %(mpi_dir)s/bin/mpicc@mpicc                = %(mpi_dir)s/bin/mpicc.exe@g' mpi.cfg
  sed -i 's@## mpicxx               = %(mpi_dir)s/bin/mpicxx@mpicxx               = %(mpi_dir)s/bin/mpicxx.exe@g' mpi.cfg
  sed -i 's@## include_dirs         = %(mpi_dir)s/include@include_dirs         = %(mpi_dir)s/include@g' mpi.cfg
  sed -i 's@## libraries            = mpi@libraries            = msmpi@g' mpi.cfg
  sed -i 's@## library_dirs         = %(mpi_dir)s/lib@library_dirs         = %(mpi_dir)s/lib@g' mpi.cfg
  #sed -i 's@## runtime_library_dirs = %(mpi_dir)s/lib@runtime_library_dirs = %(mpi_dir)s/bin@g' mpi.cfg
}

build() {
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}" && cd "python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python -m build --wheel --no-isolation
}

package() {
  cd "${srcdir}"/python-build-${MSYSTEM}
  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} --destdir="${pkgdir}" dist/*.whl
  install -Dm644 LICENSE.rst -t "${pkgdir}"${MINGW_PREFIX}/share/licenses/python-${_realname}/
}
