# 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=4.6.2
pkgrel=1
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}=${pkgver}"
          "${MINGW_PACKAGE_PREFIX}-python-${_realname}-docs")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}"
           "${MINGW_PACKAGE_PREFIX}-python-${_realname}-docs")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}"
          "${MINGW_PACKAGE_PREFIX}-python-${_realname}-docs")
pkgdesc="Python binding for the libxml2 and libxslt libraries (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
license=('BSD' 'custom')
url="https://lxml.de/"
depends=("${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libxslt"
         "${MINGW_PACKAGE_PREFIX}-python")
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")
makedepends=("${MINGW_PACKAGE_PREFIX}-cython"
             "${MINGW_PACKAGE_PREFIX}-python-docutils"
             "${MINGW_PACKAGE_PREFIX}-python-beautifulsoup4"
             "${MINGW_PACKAGE_PREFIX}-python-cssselect"
             "${MINGW_PACKAGE_PREFIX}-python-html5lib")
source=("https://github.com/lxml/lxml/archive/${_realname}-${pkgver}.tar.gz"
        "mingw-python-fix.patch"
        "use-distutils-get_platform.patch"
        "allow-pass-path-to-rst2s5.patch")
sha256sums=('31eff968b5fb7f0dc6f2dedff071f179c17df5ada2c3fd613f79a5f14a94490a'
            '032bbca0c8cb4d24665162722736e7afcc3c49b84d0bc3d4996a5c9089bd24b4'
            'd50fefc47295d8c6eecf1ca42d03af43dc79d3debb52caf8edbed3b56df2f672'
            'ec4de74d02cc4f0d09c029c7e36063ff833e378b36993e8bb57b852ac5648b7c')

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

prepare() {
  cd "${srcdir}/${_realname}-${_realname}-${pkgver}"
  apply_patch_with_msg mingw-python-fix.patch \
              use-distutils-get_platform.patch \
              allow-pass-path-to-rst2s5.patch

  cd "${srcdir}"
  rm -rf python-build-${CARCH} | true
  cp -r "${_realname}-${_realname}-${pkgver}" "python-build-${CARCH}"
}

build() {
  # It seems to me that CFLAGS and LDFLAGS aren't getting
  # converted to Windows versions when calling Python for
  # some reason?

  # Tried $(cygpath -m) to get -IC:/msys64/mingw32/
  # but that gets mangled to -IC;C:msys64msys64mingw32
  # PREFIX_WIN=$(cygpath -m $MINGW_PREFIX)
  # CPPFLAGS="$CPPFLAGS -I${PREFIX_WIN}/include/libxml2"
  # LDFLAGS="$LDFLAGS -L${PREFIX_WIN}/lib"

  # This horrible version works; needs the right amount of \\ to
  # escape the escapes. This stuff makes some of xslt-config
  # redundant (xslt-config *should* return C:/ paths but doesn't)
  PREFIX_WIN=$(cygpath -w $MINGW_PREFIX)
  PREFIX_WIN=${PREFIX_WIN//\\/\\\\}
  CPPFLAGS="$CPPFLAGS -I${PREFIX_WIN}\\\\include\\\\libxml2"
  LDFLAGS="$LDFLAGS -L${PREFIX_WIN}\\\\lib"

  msg "Python build for ${CARCH}"
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python setup.py build \
       --with-xslt-config=${MINGW_PREFIX}/bin/xslt-config \
       --with-xml2-config=${MINGW_PREFIX}/bin/xml2-config \
       --with-cython --with-unicode-strings

  #make PYTHON=${MINGW_PREFIX}/bin/python RST2S5=${MINGW_PREFIX}/bin/rst2s5.py html
}

package() {
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX#\/} \
        --root="${pkgdir}" --optimize=1 --skip-build \
        --with-xslt-config=${MINGW_PREFIX}/bin/xslt-config \
        --with-xml2-config=${MINGW_PREFIX}/bin/xml2-config \
        --with-cython --with-unicode-strings

  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

  #install -d "${pkgdir}${MINGW_PREFIX}"/share/doc/${_realname}
  #cp -r doc/html "${pkgdir}${MINGW_PREFIX}"/share/doc/${_realname}
}
