# POWER Maintainer: Alexander Baldeck <alex.bldck@gmail.com>
# Maintainer: Jan Alexander Steffens (heftig) <heftig@archlinux.org>
# Contributor: Jan de Groot <jgc@archlinux.org>

pkgbase=libsecret
pkgname=(
  libsecret
  libsecret-docs
)
pkgver=0.21.6
pkgrel=1
pkgdesc="Library for storing and retrieving passwords and other secrets"
url="https://gnome.pages.gitlab.gnome.org/libsecret/"
arch=(x86_64 powerpc64le powerpc64 powerpc riscv64)
license=(LGPL-2.1-or-later)
depends=(
  glib2
  glibc
  libgcrypt
  tpm2-tss
)
makedepends=(
  bash-completion
  gi-docgen
  git
  glib2-devel
  gobject-introspection
  meson
  vala
)
checkdepends=(
  gjs
  python-dbus
  python-gobject
  swtpm
  tpm2-abrmd
)
source=("git+https://gitlab.gnome.org/GNOME/libsecret.git?signed#tag=$pkgver")
b2sums=('6e3820bd3d92f7eb5700ae8a365ac4aa61e5c6beb23072a805674ac98fed22b33d82c54addb92f88c02d90df2926ddb2cac41bc6df945c0864641800df8f9aed')
validpgpkeys=(
  A7C626E13F9AD776776BD9CA1D8A57CF2E8D36A3 # Niels De Graef <nielsdegraef@gmail.com>
)

prepare() {
  cd $pkgbase

  # Use our test dbus-run-session; needed for communication with Tabrmd
  git revert -n 8ab88d18af05d2c9c79244166f8e4ff3d6cae051
  git revert -n 35381ee638ed9c7e01cdacd24230ca39a6e6b96c

  # Secure memory tests fail in containers
  sed -i '/test-secmem/d' egg/meson.build
}

build() {
  local meson_options=(
    -D tpm2=true
  )

  arch-meson $pkgbase build "${meson_options[@]}"
  meson compile -C build
}

_check() (
  export TPM_PATH="$PWD/tpm"
  mkdir -p "$TPM_PATH"

  swtpm_setup --create-config-files
  swtpm_setup --tpm2 --tpm-state "$TPM_PATH" --createek --allow-signing \
    --decryption --overwrite --display

  swtpm socket --tpm2 --flags startup-clear \
    --server type=unixio,path="$TPM_PATH/socket" \
    --ctrl type=unixio,path="$TPM_PATH/socket.ctrl" &
  _p1=$!

  tpm2-abrmd --tcti=swtpm:path="$TPM_PATH/socket" \
    --session --flush-all &
  _p2=$!

  trap "kill $_p1 $_p2; wait" EXIT

  export TCTI=tabrmd:bus_type=session

  meson test -C build --print-errorlogs
)

check() {
  dbus-run-session bash -c "$(declare -f _check); _check"
}

package_libsecret() {
  optdepends=('org.freedesktop.secrets: secret storage backend')
  provides=(libsecret-1.so)

  meson install -C build --destdir "$pkgdir"

  mkdir -p doc/usr/share
  mv {"$pkgdir",doc}/usr/share/doc
}

package_libsecret-docs() {
  pkgdesc+=" (documentation)"
  depends=()

  mv doc/* "$pkgdir"
}

# vim:set sw=2 sts=-1 et:
