# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=libxml2
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.9.9
pkgrel=1
arch=('any')
pkgdesc="XML parsing library, version 2 (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gettext"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config" "${MINGW_PACKAGE_PREFIX}-python2")
options=('strip' 'staticlibs')
license=(MIT)
url="http://www.xmlsoft.org/"
install=${_realname}-${CARCH}.install
source=("http://xmlsoft.org/sources/${_realname}-${pkgver}.tar.gz"
        0015-fix-unused-parameters-warning.all.patch
        0016-WARNING-to-be-fixed.all.patch
        0017-fix-const-warning.all.patch
        0018-function-declaration-isnt-prototype.all.patch
        0019-unused-flags.all.patch
        0020-fix-warnings.patch
        0023-fix-sitedir-detection.mingw.patch
        0024-shrext-pyd.mingw.patch
        0025-mingw-python-dont-remove-dot.patch
        0026-mingw-relocate.patch)
sha256sums=('94fb70890143e3c6549f265cee93ec064c80a84c42ad0f23e85ee1fd6540a871'
            'a9457b1a5e7d9499270ccfdeabbf0ca75b0e4643c111a42a5da0d113bae485c5'
            '81811dc3a497025b1f7c8fe78b7510ab080be558a646c26b89b281a9348ad195'
            'd8f81b074420a5082a2f64fda613974ffc43a04867d521dccdda9b6ddeb3422b'
            '6e9491dcaba7e7605ddd66a152317beb3f76087f406c0fccb60a051ad24a1dd8'
            '3f025f34bf0c9b084905d24a7195315bccba45a617a02cd051cd5ee791fad098'
            '4e8377b8bd163cc29056e45f59598e36dc6f00029166bdce4c78a88f1f9b3ed4'
            'e93902af20bee41ca7708280f30a1d087ab8c6f86d18939bdd56789cd1a73531'
            'bc182ef5108f0d317543e7d3ca309bbe2fc1faf832a49b3f56acc2795de2b626'
            '2d177b78b348be6466e545ef2df533c860983bf0811d1d14cdd04e1f3afef83f'
            'b66fcd4feccf6995289353d78f967be598928e66a8bc6fa910bf331402c53dc0')

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

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

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  del_file_exists pathtools.c pathtools.h

  apply_patch_with_msg \
    0015-fix-unused-parameters-warning.all.patch \
    0016-WARNING-to-be-fixed.all.patch \
    0017-fix-const-warning.all.patch \
    0018-function-declaration-isnt-prototype.all.patch \
    0019-unused-flags.all.patch \
    0020-fix-warnings.patch \
    0023-fix-sitedir-detection.mingw.patch \
    0024-shrext-pyd.mingw.patch \
    0025-mingw-python-dont-remove-dot.patch \
    0026-mingw-relocate.patch

  libtoolize --copy --force
  aclocal
  automake --add-missing
  autoconf
}

build() {
  
  # Static build
  mkdir -p ${srcdir}/build-static-${CARCH} && cd ${srcdir}/build-static-${CARCH}
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build="${MINGW_CHOST}" \
    --without-python \
    --with-modules \
    --enable-static \
    --disable-shared \
    --with-threads=win32 \
    CFLAGS="${CFLAGS} -DLIBXML_STATIC_FOR_DLL -DNOLIBTOOL"
  make

  # Shared build
  mkdir -p ${srcdir}/build-shared-${CARCH} && cd ${srcdir}/build-shared-${CARCH}
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build="${MINGW_CHOST}" \
    --with-python=${MINGW_PREFIX}/bin/python2 \
    --with-modules \
    --disable-static \
    --enable-shared \
    --with-threads=win32

  # Hack for mingw python
  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})
  find . -type f -name "Makefile" -exec sed -i "s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" {} \;
  make
}

package() {
  # First install shared
  cd ${srcdir}/build-shared-${CARCH}

  make install DESTDIR="${pkgdir}"

  # then manually install static
  cd ${srcdir}/build-static-${CARCH}

  install -m 0644 .libs/libxml2.a "${pkgdir}${MINGW_PREFIX}"/lib/

  rm "${pkgdir}${MINGW_PREFIX}"/lib/python2.7/site-packages/*.a

  # License
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  rm "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}-${pkgver}/Copyright" # This is the same file as COPYING

  # Remove hard coded library path from xlm2-config and libxml-2.0.pc files
  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})
  pushd "${pkgdir}${MINGW_PREFIX}/bin" > /dev/null
  sed -s "s|${PREFIX_DEPS}\/lib|\${libdir}|g" -i ./xml2-config
  popd > /dev/null

  pushd "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig" > /dev/null
  sed -s "s|${PREFIX_DEPS}\/lib|\${libdir}|g" -i ./libxml-2.0.pc
  popd > /dev/null

}
