# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=nh3
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=0.3.2
pkgrel=1
pkgdesc='Ammonia HTML sanitizer Python binding (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://nh3.readthedocs.io/'
msys2_repository_url='https://github.com/messense/nh3'
msys2_references=(
  'archlinux: python-nh3'
  'gentoo: dev-python/nh3'
  'purl: pkg:pypi/nh3'
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-maturin")
options=('!strip')
source=("https://pypi.org/packages/source/${_realname::1}/${_realname}/${_realname}-${pkgver}.tar.gz")
sha256sums=('f394759a06df8b685a4ebfb1874fb67a9cbfd58c64fc5ed587a663c0e63ec376')

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

  cargo fetch --locked
}

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

  export WINAPI_NO_BUNDLED_LIBRARIES=1
  export _PYTHON_HOST_PLATFORM=$(python -c "import sysconfig, sys; sys.stdout.write(sysconfig.get_platform())")
  python -m build --wheel --skip-dependency-check --no-isolation
}

package() {
  cd "python-build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
}
