# Maintainer: Peter Budai <peterbudai at hotmail dot com>

_realname=xmlsec
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.2.31
pkgrel=1
pkgdesc="XML Security Library is a C library based on LibXML2 (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.aleksey.com/xmlsec/"
license=("MIT")
depends=("${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libxslt"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-gnutls"
         "${MINGW_PACKAGE_PREFIX}-nss"
         "${MINGW_PACKAGE_PREFIX}-libtool")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-libtool")
source=(https://github.com/lsh123/xmlsec/archive/${_realname}-${pkgver//./_}.tar.gz
        01-include-openssl-last.patch
        02-make-new-wincrypt-functions-available.patch)
sha256sums=('3d975c7c945b6f8f84e956934b562f794fa6b469d78d913190d3a1c32d1a3ddb'
            '015473ce9ad0110ebf4a43be3d3046b5616345799879512d5da22a71b4cecc35'
            'c3297ca54a8baa50a880da35d546868faf7579d0b60b143385892034927fb9f3')

prepare() {
  cd "${srcdir}/${_realname}-${_realname}-${pkgver//./_}"
  patch -p1 -i ${srcdir}/01-include-openssl-last.patch
  patch -p1 -i ${srcdir}/02-make-new-wincrypt-functions-available.patch
  autoreconf -fiv 
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}
  ../${_realname}-${_realname}-${pkgver//./_}/configure \
    --prefix="${MINGW_PREFIX}" \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST}

  make
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make check || true
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install

  # copy manually copyright files
  install -Dm644 "${srcdir}/${_realname}-${_realname}-${pkgver//./_}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${_realname}-${pkgver//./_}/Copyright" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/Copyright"

  # Remove hard coded library path from pkg-config files
  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})

  pushd "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig" > /dev/null
  sed -s "s|${PREFIX_DEPS}\/lib|\${libdir}|g" -i ./xmlsec1-gcrypt.pc
  sed -s "s|${PREFIX_DEPS}\/lib|\${libdir}|g" -i ./xmlsec1-gnutls.pc
  sed -s "s|${PREFIX_DEPS}\/include|\${includedir}|g" -i ./xmlsec1-gnutls.pc
  sed -s "s|${PREFIX_DEPS}\/lib|\${libdir}|g" -i ./xmlsec1-openssl.pc
  sed -s "s|${PREFIX_DEPS}\/include|\${includedir}|g" -i ./xmlsec1-openssl.pc
  sed -s "s|${PREFIX_DEPS}\/lib|\${libdir}|g" -i ./xmlsec1.pc
  sed -s "s|${PREFIX_DEPS}\/lib|\${libdir}|g" -i ../xmlsec1Conf.sh
  sed -s "s|${PREFIX_DEPS}\/include|\${includedir}|g" -i ../xmlsec1Conf.sh
  popd > /dev/null
  pushd "${pkgdir}/${MINGW_PREFIX}/bin" > /dev/null
  sed -s "s|${PREFIX_DEPS}\/lib|\${libdir}|g" -i xmlsec1-config
  sed -s "s|${PREFIX_DEPS}\/include|\${includedir}|g" -i xmlsec1-config
  popd
}
