# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgbase=libxml2
pkgname=('libxml2' 'libxml2-devel' 'libxml2-python')
pkgver=2.9.12
pkgrel=2
pkgdesc="XML parsing library, version 2"
arch=(i686 x86_64)
license=('MIT')
makedepends=('python-devel' 'icu-devel>=68.1' 'libreadline-devel' 'ncurses-devel' 'liblzma-devel' 'zlib-devel')
url="http://www.xmlsoft.org/"
source=(http://xmlsoft.org/sources/${pkgbase}-${pkgver}.tar.gz
        https://www.w3.org/XML/Test/xmlts20130923.tar.gz
        libxml2-2.9.8-python3-unicode-errors.patch
        libxml2-2.9.12-fix-lxml-corrupted-tree.patch
        libxml2-2.9.12-fix-formatting-regression.patch
        libxml2-2.9.0-do-not-check-crc.patch
        libxml2-2.7.3-doc-install.patch
        libxml2-2.9.1-msys2.patch)
sha256sums=('c8d6681e38c56f172892c85ddc0852e1fd4b53b4209e7f4ebf17f7e2eae71d92'
            '9b61db9f5dbffa545f4b8d78422167083a8568c59bd1129f94138f936cf6fc1f'
            '37eb81a8ec6929eed1514e891bff2dd05b450bcf0c712153880c485b7366c17c'
            '5b64aa7f2411eb7650491bf42e4a3531fc3158257b4745956975b9b9bc605f7d'
            'a0bb996bc73a13f3c3758f74d0a4198757e703710c5e3e462fd7a8e58a561563'
            '5a53d640ebce5a96e35fbdf5f644c37642dfa3345a1dc4fc355728897c19b7e7'
            'bff6790eadec6d7b3affd2d3a7385ce7f80cec2cf4abbd2ed83e9cb297a614a9'
            '19be37833b77a5c46cf266fdb4b9bcd068e301e4d26583d307ce22688ffb7587')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ] || [ -d $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #


prepare() {
  cd ${srcdir}
  del_file_exists ${pkgbase}-${pkgver}/xmlconf
  mv xmlconf -t ${pkgbase}-${pkgver}

  cd ${pkgbase}-${pkgver}
  
  # From https://src.fedoraproject.org/rpms/libxml2/tree/main
  apply_patch_with_msg \
    libxml2-2.9.8-python3-unicode-errors.patch \
    libxml2-2.9.12-fix-lxml-corrupted-tree.patch \
    libxml2-2.9.12-fix-formatting-regression.patch

  apply_patch_with_msg \
    libxml2-2.9.0-do-not-check-crc.patch \
    libxml2-2.7.3-doc-install.patch \
    libxml2-2.9.1-msys2.patch 

  autoreconf -fi
}

build() {
  export lt_cv_deplibs_check_method='pass_all'
  CFLAGS+=" -fcommon"
  cd ${pkgbase}-${pkgver}
  ./configure \
    --prefix=/usr \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --build=${CHOST} \
    --host=${CHOST} \
    --target=${CHOST} \
    --enable-ipv6 \
    --with-history \
    --with-icu \
    --with-python=/usr/bin/python \
    --with-html-dir=/usr/share/doc \
    --with-html-subdir=libxml2/html
  make
  make DESTDIR=${srcdir}/dest install

  PYTHON_SITELIB="$(python -c 'import site, sys; sys.stdout.write(site.getsitepackages()[0])')"
  mv ${srcdir}/dest${PYTHON_SITELIB}/${pkgbase/lib/msys-}mod.dll ${srcdir}/dest${PYTHON_SITELIB}/${pkgbase}mod.dll
  sed -i -e "s:${pkgbase/lib/msys-}mod:${PN}mod:g" ${srcdir}/dest${PYTHON_SITELIB}/${pkgbase}mod.la
}

check() {
  cd ${pkgbase}-${pkgver}
  make check
}

package_libxml2() {
  depends=('coreutils' 'icu>=68.1' 'liblzma' 'libreadline' 'ncurses' 'zlib')
  groups=('libraries')
  install=libxml2.install

  mkdir -p ${pkgdir}/usr
  cp -rf ${srcdir}/dest/usr/bin ${pkgdir}/usr/
  rm -f ${pkgdir}/usr/bin/*-config
  mkdir -p ${pkgdir}/usr/share
  cp -rf ${srcdir}/dest/usr/share/man ${pkgdir}/usr/share/
  install -Dm644 ${srcdir}/${pkgbase}-${pkgver}/COPYING "${pkgdir}/usr/share/licenses/${pkgbase}/COPYING"
}

package_libxml2-devel() {
  pkgdesc="Libxml2 headers and libraries"
  groups=('development')
  options=('staticlibs')
  depends=("libxml2=${pkgver}" 'icu-devel>=68.1' 'libreadline-devel' 'ncurses-devel' 'liblzma-devel' 'zlib-devel')

  PYTHON_SITELIB="$(python -c 'import site, sys; sys.stdout.write(site.getsitepackages()[0])')"
  mkdir -p ${pkgdir}/usr/{bin,share}
  cp -f ${srcdir}/dest/usr/bin/*-config ${pkgdir}/usr/bin/
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/
  rm -rf ${pkgdir}${PYTHON_SITELIB}
  cp -rf ${srcdir}/dest/usr/share/aclocal ${pkgdir}/usr/share/
  cp -rf ${srcdir}/dest/usr/share/doc ${pkgdir}/usr/share/
  rm -rf ${pkgdir}/usr/share/doc/python-libxml2
  cp -rf ${srcdir}/dest/usr/share/gtk-doc ${pkgdir}/usr/share/
}

package_libxml2-python(){
  pkgdesc="Libxml2 python modules"
  depends=('libxml2')
  groups=('python-modules')
  options=('staticlibs')

  PYTHON_SITELIB="$(python -c 'import site, sys; sys.stdout.write(site.getsitepackages()[0])')"
  mkdir -p ${pkgdir}/${PYTHON_SITELIB}
  mkdir -p ${pkgdir}/usr/share/doc
  cp -rf ${srcdir}/dest${PYTHON_SITELIB}/* ${pkgdir}${PYTHON_SITELIB}/
  cp -rf ${srcdir}/dest/usr/share/doc/python-libxml2 ${pkgdir}/usr/share/doc/
}
