pkgname=p11-kit
pkgver=0.25.5
pkgdesc="Loads and enumerates PKCS#11 modules"
url="https://p11-glue.freedesktop.org"
arch=(x86_64)
license=(BSD-3-Clause)
sources=("${pkgname}-${pkgver}.tar.xz")
urls=("https://github.com/p11-glue/p11-kit/releases/download/${pkgver}/${sources[0]}")
md5sums=("e9c5675508fcd8be54aa4c8cb8e794fc")

src_prepare() {
  tar -xf ${distdir}/${sources[0]} --strip-components=1
  sed '20,$ d' -i trust/trust-extract-compat &&

  cat >> trust/trust-extract-compat << "EOF"
# Copy existing anchor modifications to /etc/ssl/local
/usr/libexec/make-ca/copy-trust-modifications

# Update trust stores
/usr/sbin/make-ca -r
EOF
}

src_build() {
  meson setup               \
        --prefix=/usr       \
        --buildtype=release \
        -D trust_paths=/etc/pki/anchors \
        build &&
  meson compile -C build
}

src_check() {
  meson test -C build --print-errorlogs
}

src_install() {
  meson install -C build --destdir "$pkgdir"
  ln -sfv ../libexec/p11-kit/trust-extract-compat \
        ${pkgdir}/usr/bin/update-ca-certificates
  install -d -m755 ${pkgdir}/usr/lib/
  ln -sfv ./pkcs11/p11-kit-trust.so ${pkgdir}/usr/lib/libnssckbi.so
}
