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

pkgbase=libxml2
pkgname=('libxml2' 'libxml2-devel')
pkgver=2.15.0
pkgrel=1
pkgdesc="XML parsing library, version 2"
arch=(i686 x86_64)
license=('spdx:MIT')
makedepends=('gcc' 'libreadline-devel' 'zlib-devel' 'autotools' 'libxslt' 'docbook-xsl' 'doxygen' 'libiconv-devel')
url="https://gitlab.gnome.org/GNOME/libxml2/-/wikis/"
msys2_repository_url="https://gitlab.gnome.org/GNOME/libxml2"
msys2_references=(
  "cpe: cpe:/a:xmlsoft:libxml2"
)
source=("https://download.gnome.org/sources/libxml2/${pkgver%.*}/${pkgbase}-${pkgver}.tar.xz"
        https://www.w3.org/XML/Test/xmlts20130923.tar.gz)
sha256sums=('5abc766497c5b1d6d99231f662e30c99402a90d03b06c67b62d6c1179dedd561'
            '9b61db9f5dbffa545f4b8d78422167083a8568c59bd1129f94138f936cf6fc1f')

# 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}

  autoreconf -vfi
}

build() {
  export lt_cv_deplibs_check_method='pass_all'

  cd ${pkgbase}-${pkgver}
  export MSYSTEM=CYGWIN
  ./configure \
    --prefix=/usr \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --build=${CHOST} \
    --host=${CHOST} \
    --target=${CHOST} \
    --with-history \
    --with-iconv \
    --with-legacy \
    --with-http \
    --with-docs \
    --without-icu \
    --without-python \
    --enable-shared \
    --enable-static
  make
  make DESTDIR=${srcdir}/dest install

}

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

package_libxml2() {
  depends=('coreutils' 'libreadline' '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}/Copyright "${pkgdir}/usr/share/licenses/${pkgbase}/Copyright"
}

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

  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/
  cp -rf ${srcdir}/dest/usr/share/doc ${pkgdir}/usr/share/
}
