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

_realname=xmlsec
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.2.42
pkgrel=3
pkgdesc="XML Security Library is a C library based on LibXML2 (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.aleksey.com/xmlsec/"
msys2_repository_url="https://github.com/lsh123/xmlsec"
msys2_references=(
  "cpe: cpe:/a:xmlsec_project:xmlsec"
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-gnutls"
         "${MINGW_PACKAGE_PREFIX}-libgcrypt"
         "${MINGW_PACKAGE_PREFIX}-libltdl"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libxslt"
         "${MINGW_PACKAGE_PREFIX}-nss"
         "${MINGW_PACKAGE_PREFIX}-openssl")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools")
source=(https://github.com/lsh123/xmlsec/releases/download/xmlsec-${pkgver//./_}/${_realname}1-${pkgver}.tar.gz
        01-include-openssl-last.patch
        02-make-new-wincrypt-functions-available.patch)
sha256sums=('5359e3c19016ab164b505c1222ef5a2d87b34f50f85ff91d765f4aaa0df42db2'
            '015473ce9ad0110ebf4a43be3d3046b5616345799879512d5da22a71b4cecc35'
            'c3297ca54a8baa50a880da35d546868faf7579d0b60b143385892034927fb9f3')

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

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  CPPFLAGS+=" -DLIBXML_STATIC -DLIBXSLT_STATIC -DXMLSEC_STATIC" \
  ../${_realname}1-${pkgver}/configure \
    --prefix="${MINGW_PREFIX}" \
    --disable-shared \
    --enable-static \
    --enable-static-linking \
    --enable-mscrypto \
    --enable-mscng

  make

  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  ../${_realname}1-${pkgver}/configure \
    --prefix="${MINGW_PREFIX}" \
    --enable-shared \
    --disable-static \
    --enable-mscrypto \
    --enable-mscng

  make
}

check() {
  cd "${srcdir}/build-${MSYSTEM}-static"
  make check || warning "Tests failed"

  cd "${srcdir}/build-${MSYSTEM}-shared"
  make check || warning "Tests failed"
}

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

  cd "${srcdir}/build-${MSYSTEM}-shared"
  make DESTDIR="${pkgdir}" install

  # copy manually copyright files
  install -Dm644 "${srcdir}/${_realname}1-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 "${srcdir}/${_realname}1-${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}\/include|\${includedir}|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
}
