# Packager = Tuxcore <dev@tuxcore.org> 
pkgbase=libsecret
pkgname=(
  libsecret
  libsecret-docs
)
pkgver=0.21.4
pkgrel=1
pkgdesc="Library for storing and retrieving passwords and other secrets"
url="https://gnome.pages.gitlab.gnome.org/libsecret/"
arch=(x86_64)
license=(LGPL-2.1-or-later)
depends=(
  glib2
  libgcrypt
  tpm2-tss
)
makedepends=(
  bash-completion
  gi-docgen
  git
  gobject-introspection
  meson
  vala
)
checkdepends=(
  gjs
  python-dbus
  python-gobject
  swtpm
  tpm2-abrmd
)
_commit=6b5a6c28afc6dd93c232a4907a87c881079ff91b  # tags/0.21.4^0
source=("git+https://gitlab.gnome.org/GNOME/libsecret.git#commit=$_commit")
b2sums=('SKIP')

pkgver() {
  cd $pkgbase
  git describe --tags | sed 's/[^-]*-g/r&/;s/-/+/g'
}

prepare() {
  cd $pkgbase

  # 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() (
  mkdir -p "${TPM_PATH:=$PWD/tpm}"
  export 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:
