# Contributor: Raed Rizqie <raed.rizqie@gmail.com>

_realname=maturin
pkgbase=mingw-w64-python-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-python-${_realname}
pkgver=1.4.0
pkgrel=1
pkgdesc='Build and publish crates with pyo3, rust-cpython and cffi bindings as well as rust binaries as python packages (mingw-w64)'
url='https://github.com/pyo3/maturin'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
msys2_references=(
  'pypi: maturin'
)
license=('spdx:MIT OR Apache-2.0')
depends=(
    "${MINGW_PACKAGE_PREFIX}-python")
makedepends=(
    "${MINGW_PACKAGE_PREFIX}-python-build"
    "${MINGW_PACKAGE_PREFIX}-python-installer"
    "${MINGW_PACKAGE_PREFIX}-python-wheel"
    "${MINGW_PACKAGE_PREFIX}-python-setuptools-rust")
options=(!strip)
source=("https://pypi.io/packages/source/${_realname:0:1}/${_realname}/${_realname}-${pkgver}.tar.gz"
        "001-fix-default-target-on-mingw-32bit.patch")
sha256sums=('ed12e1768094a7adeafc3a74ebdb8dc2201fa64c4e7e31f14cfc70378bf93790'
            '5d363f2540f84651439f47782c9d60ababde58f7187b09945040f3795745bf45')

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

  patch -Np1 -i "${srcdir}/001-fix-default-target-on-mingw-32bit.patch"
}

build() {
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}" && cd "python-build-${MSYSTEM}"

  export MATURIN_SETUP_ARGS="--features full,rustls"
  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --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-apache "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/license-apache"
  install -Dm644 license-mit "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/license-mit"
}
