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

_realname=lxml
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" 
         "${MINGW_PACKAGE_PREFIX}-python3-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python-${_realname}-docs")
pkgver=4.3.0
pkgrel=1
arch=('any')
license=('BSD' 'custom')
url="http://lxml.de/"
makedepends=("${MINGW_PACKAGE_PREFIX}-cython"
             "${MINGW_PACKAGE_PREFIX}-cython2"
             "${MINGW_PACKAGE_PREFIX}-libxml2"
             "${MINGW_PACKAGE_PREFIX}-libxslt"
             "${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python2-beautifulsoup4"
             "${MINGW_PACKAGE_PREFIX}-python2-cssselect"
             "${MINGW_PACKAGE_PREFIX}-python2-html5lib"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-python3-beautifulsoup4"
             "${MINGW_PACKAGE_PREFIX}-python3-cssselect"
             "${MINGW_PACKAGE_PREFIX}-python3-html5lib")
source=("https://pypi.io/packages/source/l/lxml/${_realname}-${pkgver}.tar.gz"
        "mingw-python-fix.patch"
        "use-distutils-get_platform.patch")
sha256sums=('d1e111b3ab98613115a208c1017f266478b0ab224a67bc8eac670fa0bad7d488'
            'd5811e7c0be65d7e9ce59ad925466ac7dc95bdca05439be0becf2cca7bb9602a'
            'd50fefc47295d8c6eecf1ca42d03af43dc79d3debb52caf8edbed3b56df2f672')

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

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

  cd "${srcdir}"
  for builddir in python{2,3}-build-${CARCH}; do
    rm -rf ${builddir} | true
    cp -r "${_realname}-${pkgver}" "${builddir}"
  done
}

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"

  for pver in {2,3}; do
    msg "Python ${pver} build for ${CARCH}"  
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py build \
         --with-xslt-config=${MINGW_PREFIX}/bin/xslt-config \
         --with-xml2-config=${MINGW_PREFIX}/bin/xml2-config \
         --with-cython --with-unicode-strings
  done 
}

package_python2-lxml() {
  pkgdesc="Python2 binding for the libxml2 and libxslt libraries (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-libxml2" "${MINGW_PACKAGE_PREFIX}-libxslt" "${MINGW_PACKAGE_PREFIX}-python2")
  optdepends=("${MINGW_PACKAGE_PREFIX}-python2-beautifulsoup4: support for parsing not well formed HTML"
              "${MINGW_PACKAGE_PREFIX}-python2-cssselect: support for cssselect"
              "${MINGW_PACKAGE_PREFIX}-python2-html5lib: support for html5lib parser"
              "${MINGW_PACKAGE_PREFIX}-python-lxml-docs: offline docs")

  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python2 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/python2-${_realname}/LICENSE
  install -Dm644 doc/licenses/BSD.txt \
    "${pkgdir}${MINGW_PREFIX}"/share/licenses/python2-${_realname}/BSD.txt
  install -Dm644 doc/licenses/elementtree.txt \
    "${pkgdir}${MINGW_PREFIX}"/share/licenses/python2-${_realname}/elementtree.txt
}

package_python3-lxml() {
  pkgdesc="Python3 binding for the libxml2 and libxslt libraries (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-libxml2" "${MINGW_PACKAGE_PREFIX}-libxslt" "${MINGW_PACKAGE_PREFIX}-python3")
  optdepends=("${MINGW_PACKAGE_PREFIX}-python3-beautifulsoup4: support for parsing not well formed HTML"
              "${MINGW_PACKAGE_PREFIX}-python3-cssselect: support for cssselect"
              "${MINGW_PACKAGE_PREFIX}-python3-html5lib: support for html5lib parser"
              "${MINGW_PACKAGE_PREFIX}-python-lxml-docs: offline docs")

  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python3 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/python3-${_realname}/LICENSE
  install -Dm644 doc/licenses/BSD.txt \
    "${pkgdir}${MINGW_PREFIX}"/share/licenses/python3-${_realname}/BSD.txt
  install -Dm644 doc/licenses/elementtree.txt \
    "${pkgdir}${MINGW_PREFIX}"/share/licenses/python3-${_realname}/elementtree.txt
}

package_python-lxml-docs() {
  pkgdesc="Python binding for the libxml2 and libxslt libraries (docs) (mingw-w64)"
  options=('docs')

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

package_mingw-w64-i686-python2-lxml() {
  package_python2-lxml
}

package_mingw-w64-i686-python3-lxml() {
  package_python3-lxml
}

package_mingw-w64-x86_64-python2-lxml() {
  package_python2-lxml
}

package_mingw-w64-x86_64-python3-lxml() {
  package_python3-lxml
}

package_mingw-w64-i686-python-lxml-docs() {
  package_python-lxml-docs
}

package_mingw-w64-x86_64-python-lxml-docs() {
  package_python-lxml-docs
}
