# Maintainer of AUR version: pingplug < aur at pingplug dot me >
# Contributor: xantares < xantares09 at hotmail dot com >
# Contributor: Martchus <martchus@gmx.net>

_commit=b0ebe7555c291808db29377ba79cb8326301f0a6  # 0.23.18.1
_architectures="i686-w64-mingw32 x86_64-w64-mingw32"

pkgname=mingw-w64-p11-kit
pkgver=0.23.18.1
pkgrel=1
pkgdesc="Provides a way to load and enumerate PKCS#11 modules (mingw-w64)"
arch=('any')
url="https://p11-glue.freedesktop.org"
license=('BSD')
depends=('mingw-w64-crt'
         'mingw-w64-libtasn1'
         'mingw-w64-libffi'
         'mingw-w64-libiconv')
makedepends=('mingw-w64-meson'
             'git')
options=('!strip' 'staticlibs' '!buildflags')
source=("git+https://github.com/p11-glue/p11-kit?#commit=${_commit}"
        "0001-Build-and-install-libnssckbi-p11-kit.so.patch")
sha256sums=('SKIP'
            '0736f74cec5ca49d91afa47cd84f9ac0404947bf0064358e22c3e53b69e82798')

pkgver() {
  cd "${srcdir}/p11-kit"
  git describe --tags | sed 's/-/+/g'
}

prepare() {
  cd "${srcdir}"/p11-kit
  # Build and install an additional library (libnssckbi-p11-kit.so) which
  # is a copy of p11-kit-trust.so but uses the same label for root certs as
  # libnssckbi.so ("Builtin Object Token" instead of "Default Trust")
  # https://bugs.freedesktop.org/show_bug.cgi?id=66161
  patch -Np1 -i ../0001-Build-and-install-libnssckbi-p11-kit.so.patch
}

build() {
  export PYTHONPATH=/usr/lib/python3.7/site-packages
  cd "${srcdir}"/p11-kit
  for _arch in ${_architectures}; do
    mkdir -p build-${_arch} && pushd build-${_arch}
    ${_arch}-meson -D gtk_doc=false -D man=false --default-library both
    ninja
    popd
  done
}

package() {
  export PYTHONPATH=/usr/lib/python3.7/site-packages
  for _arch in ${_architectures}; do
    cd "${srcdir}"/p11-kit/build-${_arch}
    DESTDIR="${pkgdir}" ninja install
    rm -r "${pkgdir}"/usr/${_arch}/etc
    ${_arch}-strip --strip-unneeded "${pkgdir}"/usr/${_arch}/bin/*.dll
    ${_arch}-strip -g "${pkgdir}"/usr/${_arch}/lib/*.a

    # see https://github.com/mesonbuild/meson/issues/4138
    ${_arch}-gcc-ranlib ${pkgdir}/usr/${_arch}/lib/*.a
  done
}

# vim:set ts=2 sw=2 et:
