# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=lxml
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=6.0.2
pkgrel=2
pkgdesc="Python binding for the libxml2 and libxslt libraries (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'mingw32' 'ucrt64' 'clang64' 'clangarm64')
url="https://lxml.de/"
msys2_repository_url="https://github.com/lxml/lxml"
msys2_references=(
  'purl: pkg:pypi/lxml'
  "cpe: cpe:/a:lxml:lxml"
)
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libxslt"
         "${MINGW_PACKAGE_PREFIX}-python")
if [[ ${CARCH} != i686 ]]; then
optdepends=("${MINGW_PACKAGE_PREFIX}-python-beautifulsoup4: support for parsing not well formed HTML"
            "${MINGW_PACKAGE_PREFIX}-python-cssselect: support for cssselect"
            "${MINGW_PACKAGE_PREFIX}-python-html5lib: support for html5lib parser")
fi
makedepends=("${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-cython"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-pkgconf")
source=("https://pypi.org/packages/source/${_realname::1}/${_realname}/${_realname}-${pkgver}.tar.gz"
        "use-distutils-get_platform.patch"
        "0001-setupinfo-fix-library-name.patch")
sha256sums=('cd79f3367bd74b317dda655dc8fcfa304d9eb6e4fb06b7168c5cf27f96e0cd62'
            '6e58bd3072a6a77bf50c4c203beef589c97873e9902d4c8c6151abe71035f186'
            '7d9e5380a8461e3c2bb4b8e0ece52011b26e3112b94b97e548c927524cfc93de')

_apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${_realname}-${pkgver}"
  _apply_patch_with_msg \
    use-distutils-get_platform.patch \
    0001-setupinfo-fix-library-name.patch
}

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

  python -m build --wheel --skip-dependency-check --no-isolation \
    -C--build-option=--with-cython -C--build-option=--with-unicode-strings
}

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

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

  install -Dm644 LICENSES.txt \
    "${pkgdir}${MINGW_PREFIX}"/share/licenses/python-${_realname}/LICENSE
  install -Dm644 doc/licenses/BSD.txt \
    "${pkgdir}${MINGW_PREFIX}"/share/licenses/python-${_realname}/BSD.txt
  install -Dm644 doc/licenses/elementtree.txt \
    "${pkgdir}${MINGW_PREFIX}"/share/licenses/python-${_realname}/elementtree.txt
}
