# Maintainer: Jonas Witschel <diabonas@archlinux.org>
pkgname=swtpm
pkgver=0.8.2
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')
depends=('fuse2' 'glib2' 'gnutls' 'json-glib' 'libseccomp' 'libtpms' 'openssl' 'libseccomp.so')
makedepends=('git' 'expect' 'iproute2' 'libtasn1' 'python' 'socat')
checkdepends=('softhsm')
source=("git+$url.git?signed#tag=v$pkgver")
sha512sums=('6a55a3495638e73d9f61fbc6766b125328fab9aa8f71e45b810e92789f7db71c2370914f6fb313673db380a0faff058602d79d66f34128b55c69ed945ed88404')
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

	autoreconf --install --force
}

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

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

package() {
	cd "$pkgname"
	make DESTDIR="$pkgdir" install
	install -Dm644 LICENSE -t "$pkgdir/usr/share/licenses/$pkgname"
	echo 'u tss - "tss user for tpm2"' | install -Dm644 /dev/stdin "$pkgdir/usr/lib/sysusers.d/$pkgname.conf"
	echo 'z /var/lib/swtpm-localca 0750 tss root' | install -Dm644 /dev/stdin "$pkgdir/usr/lib/tmpfiles.d/$pkgname.conf"
	chmod 750 "$pkgdir/var/lib/swtpm-localca"
}
