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

_realname=libxslt
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.1.33
pkgrel=1
pkgdesc="XML stylesheet transformation library (mingw-w64)"
arch=('any')
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}-python2"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
options=('strip' 'staticlibs')
install=${_realname}-${CARCH}.install
source=("http://xmlsoft.org/sources/${_realname}-${pkgver}.tar.gz"
        "libxslt-1.1.32-w64.patch"
        "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'
        '0007-use-mingw-C99-compatible-functions-{v}snprintf.patch'
        '0008-fix-warnings.patch')
sha256sums=('8e36605144409df979cab43d835002f63988f3dc94d5d3537c12796db90e38c8'
            'bd5aefa6807a4dfeb293328acea8a76d52284cf115f9f354af6a36785eb1ce91'
            'e62f27315f2a25db3b6bfe5695d885998aeb3ad87cce6aebbd035c404fe8d378'
            '76029b76c6a1670f85ecb3db6b299cfe468a65b1718880cb8b82c6813cc63ea9'
            'c02cd8437064db0921c08e63245c403dad12410d2997715aa7aa36600bfaa895'
            '68802613794439375a28bb20195a9c366c19cc289d761d8d35091b7576581797'
            'fc23bd3c9e62a28cb1c020cebc83ca7532a5a7a21c2e4748b72d13ac7716a851'
            '1da234751e2bd337a0991a0f90272bbf83b6cbd8945208825fa3d9dd580020e6'
            'e767b371aed6cd09c7b7edac60d2e26644c295081396d551238594d1219e7ecb'
            '3296a8719c86455574a15921ce7ab2f28a98e41d7c3746ae5230dee09653ae00')

# 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.32-w64.patch" \
    "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" \
    "0007-use-mingw-C99-compatible-functions-{v}snprintf.patch"  \
    "0008-fix-warnings.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} \
    --with-python=${MINGW_PREFIX}/bin/python2

  # 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" {} \;
  CC=${MINGW_PREFIX}/bin/gcc make
}

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

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

  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
}

