# Contributor: Carlo Landmeter
# Contributor: Rasmus Thomsen <oss@cogitri.dev>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=polkit
pkgver=125
pkgrel=0
pkgdesc="Application development toolkit for controlling system-wide privileges"
url="https://github.com/polkit-org/polkit"
arch="all"
license="GPL-2.0-or-later"
options="suid !check"  # Needs a functioning dbus daemon
makedepends="
	duktape-dev
	elogind-dev
	expat-dev
	glib-dev
	gobject-introspection-dev
	linux-pam-dev
	meson
	perl
	"
pkgusers="polkitd"
pkggroups="polkitd"
install="$pkgname-common.pre-install $pkgname-common.pre-upgrade"
subpackages="
	$pkgname-dev
	$pkgname-noelogind-libs:libs
	$pkgname-doc
	$pkgname-lang
	$pkgname-openrc
	$pkgname-common
	$pkgname-elogind
	$pkgname-elogind-dev:_elogind_dev
	$pkgname-elogind-libs:_elogind_libs
	"
source="https://github.com/polkit-org/polkit/archive/refs/tags/$pkgver/polkit-$pkgver.tar.gz
	alpine-polkit.pam
	polkit.initd
	args.patch
	"

# secfixes:
#   0.120-r2:
#     - CVE-2021-4034
#   0.119-r0:
#     - CVE-2021-3560

build() {
	msg 'Building without elogind'
	_build no-elogind -Dsession_tracking="ConsoleKit"

	msg 'Building with elogind'
	_build elogind -Dsession_tracking="elogind"
}

_build() {
	local outdir="$1"
	shift
	abuild-meson \
		-Db_lto=true \
		-Dtests="$(want_check && echo true || echo false)" \
		-Dman=true \
		-Dsystemdsystemunitdir=/trash \
		-Dpam_prefix=/etc/pam.d \
		"$@" \
		. "$outdir"
	meson compile -C "$outdir"
}

check() {
	meson test --print-errorlogs --no-rebuild -C elogind
}

package() {
	provider_priority=100  # highest

	depends="
		$pkgname-common=$pkgver-r$pkgrel
		$pkgname-noelogind-libs=$pkgver-r$pkgrel
		"

	DESTDIR="$pkgdir" meson install --no-rebuild -C no-elogind
	DESTDIR="$builddir/elogind/dest" meson install --no-rebuild -C elogind

	cd "$pkgdir"

	# systemd files
	rm -rf ./trash

	# Use our own polkit rules, upstream may change them
	install -m644 "$srcdir"/alpine-polkit.pam etc/pam.d/polkit-1

	# See polkit's configure script which tells us what permissions to set
	chown -R polkitd:polkitd etc/polkit-1/rules.d usr/share/polkit-1/rules.d
	chmod -R 700 etc/polkit-1/rules.d usr/share/polkit-1/rules.d
	chmod 4755 usr/lib/polkit-1/polkit-agent-helper-1
	chmod 4755 usr/bin/pkexec

	install -Dm755 "$srcdir"/polkit.initd etc/init.d/polkit
}

libs() {
	provides="$pkgname-libs"
	provider_priority=100  # highest

	replaces="$pkgname-elogind-libs"

	depends=""

	default_libs
}

dev() {
	default_dev

	# XXX: We have to disable tracedeps to avoid abuild adding both
	# polkit-libs and polkit-elogind-libs to depends - they are autodetected
	# via symlinks in /usr/lib.
	options="!tracedeps"
	# XXX: Since we disabled tracedeps, all depends and provides must be
	# specified. This sucks, but I don't know of a better solution (@jirutka).
	depends="$depends_dev
		$pkgname-common=$pkgver-r$pkgrel
		$pkgname-libs
		dbus-glib-dev
		pc:gio-2.0>=2.18
		pc:glib-2.0>=2.18
		pkgconfig
		"
	provides="pc:polkit-agent-1=$pkgver pc:polkit-gobject-1=$pkgver"
}

elogind() {
	pkgdesc="Polkit with elogind session tracking"
	depends="$pkgname-elogind-libs=$pkgver-r$pkgrel $pkgname-common=$pkgver-r$pkgrel"
	provides="$pkgname=$pkgver-r$pkgrel"
	provider_priority=10  # lowest
	replaces="$pkgname"  # for backward compatibility

	cd "$builddir"/elogind/dest

	mkdir -p "$subpkgdir"/usr/lib/polkit-1
	mv usr/lib/polkit-1/polkitd "$subpkgdir"/usr/lib/polkit-1/
}

_elogind_dev() {
	pkgdesc="Polkit with elogind session tracking (development files)"
	depends="$pkgname-dev=$pkgver-r$pkgrel $pkgname-elogind-libs=$pkgver-r$pkgrel"
	replaces="$pkgname-dev"

	cd "$builddir"/elogind/dest

	# *.gir files for the default and the elogind variant differ, the rest
	# is the same.
	mkdir -p "$subpkgdir"/usr/share
	mv usr/share/gir-1.0 "$subpkgdir"/usr/share/
}

_elogind_libs() {
	pkgdesc="Polkit with elogind session tracking (libraries)"
	depends=""
	provides="$pkgname-libs"
	provider_priority=10  # lowest

	# for backward compatibility/overwrite order
	replaces="$pkgname-libs $pkgname-noelogind-libs $pkgname-elogind<=0.118"

	cd "$builddir"/elogind/dest

	mkdir -p "$subpkgdir"/usr/lib
	mv usr/lib/lib*.so.* "$subpkgdir"/usr/lib/
}

common() {
	pkgdesc="Common files for polkit and polkit-elogind"
	depends=""
	replaces="$pkgname $pkgname-elogind"  # for backward compatibility

	# Move all that left to subpackage.
	amove etc/*
	amove usr/*

	# Move polkitd back to the base package (that's the only file from the
	# polkit package that differs between default and elogind variant).
	mkdir -p "$pkgdir"/usr/lib/polkit-1
	mv "$subpkgdir"/usr/lib/polkit-1/polkitd "$pkgdir"/usr/lib/polkit-1/
}

openrc() {
	default_openrc
	# polkit-elogind won't pull it otherwise
	install_if="$pkgname-common=$pkgver-r$pkgrel openrc"
}

sha512sums="
64d85c1557355d6de6483beeb855b74a99dbb30cf9968206dc0aaf147156072ca2604bf667533099ee3972b3eed0421ec0a1ff8bea35a1e4c54da7b9688e0953  polkit-125.tar.gz
f5102dc00d390b3a3c957b62e1712db778c7ffb7378f3d8e816c0757c11a308c5d5303e42595b0d6add9839247c773880cd34e56afacc89eb6efaadf9aae7826  alpine-polkit.pam
f6e5ac0ed41feb392dfd104979ec577c5936f3db2bd252b12b7b9b2609a0901dae38bebec1ea65ccf4f427860b520383ae4d2c66fb74ab986c715f6b0ad50473  polkit.initd
0605c528e25ea1f281ea7708e0185df971174e809ef112c24caa18658b78c6b819890897a4e5d5353098c52be2fccea6a902049c846b3b6b713cb7501f431703  args.patch
"
