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

_realname=libxslt
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.1.34
pkgrel=4
pkgdesc="XML stylesheet transformation library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="http://xmlsoft.org/XSLT/"
license=('custom')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libgcrypt")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
options=('strip' 'staticlibs')
source=("http://xmlsoft.org/sources/${_realname}-${pkgver}.tar.gz"
        "libxslt-1.1.27-disable_static_modules.patch"
        "libxslt-1.1.32-win32-shared.patch"
        "libxslt.m4-libxslt-1.1.26.patch"
        '0002-python-linking-on.mingw.patch'
        '0003-fix-concurrent-directory-creation.all.patch'
        '0004-add-missing-include-for-python.all.patch'
        '0008-fix-warnings.patch'
        '0009-fix-xml2-config-check-in-configure.patch')
sha256sums=('98b1bd46d6792925ad2dfe9a87452ea2adebf69dcb9919ffd55bf926a7f93f7f'
            '3d56e8ae1fbf817f495ecb0afe223d7a001ef4787ce52e1c57cc7b32499e95d6'
            '76029b76c6a1670f85ecb3db6b299cfe468a65b1718880cb8b82c6813cc63ea9'
            'c02cd8437064db0921c08e63245c403dad12410d2997715aa7aa36600bfaa895'
            '152f1f549406d049648020246bf770deea612b26835f4cfee79611cd3ac99763'
            'fc23bd3c9e62a28cb1c020cebc83ca7532a5a7a21c2e4748b72d13ac7716a851'
            '1da234751e2bd337a0991a0f90272bbf83b6cbd8945208825fa3d9dd580020e6'
            '7578654016d89e9a292860201199e8bffeb78b3aa819e8baecad7cf9305bdb67'
            '27bd05d3dab2a9848fc70e6894e904b15f260d3ced6ae4e0f4a616711f87c1c3')

# 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}"
  apply_patch_with_msg \
    "libxslt-1.1.27-disable_static_modules.patch" \
    "libxslt-1.1.32-win32-shared.patch" \
    "libxslt.m4-libxslt-1.1.26.patch" \
    "0002-python-linking-on.mingw.patch" \
    "0003-fix-concurrent-directory-creation.all.patch" \
    "0004-add-missing-include-for-python.all.patch" \
    "0008-fix-warnings.patch" \
    "0009-fix-xml2-config-check-in-configure.patch"

  autoreconf -fi
}

build() {
  [[ -d "${srcdir}/build-${CARCH}" ]] && rm -rf "${srcdir}/build-${CARCH}"
  mkdir -p "${srcdir}/build-${CARCH}" && cd "${srcdir}/build-${CARCH}"
 ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-shared \
    --with-crypto \
    --with-plugins \
    --with-libxml-prefix=${MINGW_PREFIX} \
    --without-python

  CC=${MINGW_PREFIX}/bin/gcc make
}

package() {
  export MSYS2_ARG_CONV_EXCL="-//OASIS"
  cd "${srcdir}/build-${CARCH}"
  make DESTDIR="${pkgdir}" install

  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})
  pushd "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig" > /dev/null
  sed -s "s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" -i ./libexslt.pc
  sed -s "s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" -i ./libxslt.pc
  popd > /dev/null
}
