#!/usr/bin/bash
# JOBoRun		: Jwm OpenBox Obarun RUNit
# Maintainer	: Joe Bo Run <joborun@disroot.org>
# PkgSource		: url="https://gittea.disroot.org/joborun-pkg/jobcore/$pkgname"
# Website		: https://pozol.eu
#-----------------------------------------| DESCRIPTION |---------------------------------------

pkgbase=libsecret
pkgname=(libsecret libsecret-docs)
pkgver=0.20.5
pkgrel=01
pkgdesc="Library for storing and retrieving passwords and other secrets"
url="https://wiki.gnome.org/Projects/Libsecret"
arch=(x86_64)
depends=(glib2 libgcrypt tpm2-tss)
makedepends=(gobject-introspection vala git meson gi-docgen bash-completion)
checkdepends=(python-dbus python-gobject gjs swtpm tpm2-abrmd)
_commit=d64530ac70d9723945d68ca079293ea0f9df9e9f  # tags/0.20.5^0
source=("git+https://gitlab.gnome.org/GNOME/libsecret.git#commit=$_commit")

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() {
  arch-meson $pkgbase build -D tpm2=true
  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"
}

#---- license gpg-key sha256sums ----

license=(LGPL)

sha256sums=('SKIP')
