# Maintainer: Jonas Witschel <diabonas@archlinux.org>

pkgname=swtpm
pkgver=0.10.1
pkgrel=1
pkgdesc='Libtpms-based TPM emulator with socket, character device, and Linux CUSE interface'
arch=('x86_64' 'aarch64')
url='https://github.com/stefanberger/swtpm'
license=('BSD-3-Clause')
depends=(
  'fuse2'
  'gcc-libs'
  'glib2'
  'glibc'
  'gmp'
  'gnutls'
  'json-glib'
  'libseccomp'
  'libseccomp.so'
  'libtasn1'
  'libtpms'
  'openssl'
)
makedepends=(
  'expect'
  'git'
  'iproute2'
  'python'
  'socat'
)
checkdepends=('softhsm')
source=(
  "git+$url.git?signed#tag=v$pkgver"
  "$pkgname.sysusers"
  "$pkgname.tmpfiles"
)
sha512sums=('01f5a4c3a3f6b835b464dab336707a3b561123da8f4d8c1a668a3724c5b6de52f01936048cd2b29a3f389d77e7ed164a39365b8958cc09bb472748b030d82af1'
            '4b316aa2947c31f0a34a1f84335e3b9bf8e6c033d1cb562022ff49d0297ed4855491e23f549671655143e8e1288a5ad1e215a25868cf7f0c6963d3dced55e890'
            '2e770313011ca07d3e3c534274897f835e3fa271586d0489f314d25bf2a4ab17f6ee1cfc9539865c0e51ca61f62ae1afd90885bd1ad5144fd4ee1f127a08984d')
validpgpkeys=('B818B9CADF9089C2D5CEC66B75AD65802A0B4211') # Stefan Berger <stefanb@linux.vnet.ibm.com>

prepare() {
  cd "$pkgname"
  # Remove usage of /usr/bin/env to avoid PATH manipulation attacks
  sed --in-place 's/env //' \
    samples/swtpm-create-tpmca \
    samples/swtpm-create-user-config-files.in

  # Disable test failing in CI
  sed -i -e "/test_tpm2_avoid_da_lockout/d" tests/Makefile.am

  autoreconf --install --force
}

build() {
  cd "$pkgname"
  ./configure \
    --prefix=/usr \
    --libexecdir=/usr/lib \
    --with-cuse \
    --with-gnutls \
    --with-seccomp
  make
}

check() {
  cd "$pkgname"
  make check
}

package() {
  cd "$pkgname"
  make DESTDIR="$pkgdir" install
  chmod -v 750 "$pkgdir/var/lib/swtpm-localca"
  install -vDm644 -t "$pkgdir/usr/share/licenses/$pkgname" LICENSE
  install -vDm644 "$srcdir/$pkgname.sysusers" "$pkgdir/usr/lib/sysusers.d/$pkgname.conf"
  install -vDm644 "$srcdir/$pkgname.tmpfiles" "$pkgdir/usr/lib/tmpfiles.d/$pkgname.conf"
}
