### this file is sourced not run
PKGVER=16.1
PKGBUILD=1
PKGARCH=x86_64

# source: BLFS 11.3
TARBALL=pulseaudio-$PKGVER.tar.xz
MD5SUM=2c7b8ceb5d7337565c7314b4d6087ca8
SRC_URL=https://www.freedesktop.org/software/pulseaudio/releases/$TARBALL
BUNDLE=

SLACKREQ="libsndfile alsa-lib dbus glib libspeex libX11 avahi bluez
libfftw3f libsbc libjack2 libtdb eudev openssl gstreamer2
libsamplerate libsoxr"

SLACKDESC="pulseaudio: pulseaudio $PKGVER (Audio Server)
pulseaudio: 
pulseaudio: PulseAudio is a sound system for POSIX OSes, meaning that it is a 
pulseaudio: proxy for sound applications. It allows you to do advanced operations 
pulseaudio: on your sound data as it passes between your application and your 
pulseaudio: hardware.
pulseaudio: 
pulseaudio: EXPERIMENTAL PACKAGE WARNING: This package can break your audio setup.
pulseaudio: 
pulseaudio: 
pulseaudio: 
"
SLACKINSTALL='adduser -D -g "PulseAudio daemon,,," -h /var/run/pulse pulse; addgroup pulse audio;
addgroup pulse-access; addgroup root pulse-access; addgroup spot pulse-access'
SLACKUNINSTALL='deluser --remove-home pulse; delgroup pulse-access'

### default pkg_download
### default pkg_prepare
### default pkg_package
### build
pkg_build() {
	MESONFLAGS="$MESONFLAGS \
		-Dudevrulesdir=/$LIBDIR/udev \
		-Ddoxygen=false      \
		-Dtests=false        \
		-Dsamplerate=enabled \
	" &&
	# -Ddatabase=gdbm # use gdbm instead of tdb
	pkg_build_meson &&
	pkg_build_slackdesc &&

	# Fatdog multi-user configuration

	# The default configuration for PulseAudio is for single-user, single-seat operation.
	# The PulseAudio server is meant to be run under the logged-in user, and is restricted
	# to be only accessible to applications running in the same user.
	# Thus, security is achieved. Nice and good.
	#
	# But this configuration a problem in multi-user system, such as Fatdog.
	# Fatdog routinely runs as "root" and "spot" at the very least, and possibly more.
	# If we follow the PulseAudio philosophy, we would need to run one server for every user.
	# So, one PulseAudio server for "root" and another one for "spot".
	#
	# This would be nice if it works, but it doesn't.
	# PulseAudio hogs the sound hardware, making it unaccessible to other sound apps.
	# Not even to another instance of PulseAudio server.
	# So this scenario obviously won't work in Fatdog.
	#
	# What we need is to run PulseAudio as a "shared" server which is accessible to
	# all users (who have been granted access to it).
	# There are two scenarios for it:
	# 1. Multi-user- multi-seat: every user gets its own separate sound hardware
	# 2. Multi-user with shared hardware (sounds from all users are mixed together
	#    and output to the same hardware)
	#
	# In terms of PulseAudio configuration, (1) & (2) is actually identical.
	# (1) only needs additional configuration, to specify the correct sink/source as
	#     the default, for every seat. This can be done manually, with a script, or
	@     automatically using multi-seat configuration tools like elogind/consolekit etc.
	# (2) is what you get without further configuration or tools.
	#
	# Fatdog64 adopts (2) as its default configuration. 
	#
	# There are multiple ways to run PulseAudio as a "shared" server.
	# 1. As "system-wide" server, running as root ("system-wide" can only be run as root).
	# 2. As "session" server, running as a user (e.g. "pulse"), sharing access to others.
	# 3. As "session" server, running as root, sharing access to other.
	#
	# (1) is the standard and supported by to run PulseAudio as a shared server, but this
	# mode is frowned upon by the authors of PulseAudio themselves, for dubious security reasons.
	# Although, actually when running as root, in system-wide mode, PulseAudio immediately
	# drops privilege to become "pulse" user, so IMHO it's not really as bad as it sounds.
	#
	# (2) and (3) are not officialy supported or endorsed by PulseAudio team, but nonetheless
	# works and are being used by others who needs it.
	# E.g. https://dhole.github.io/post/pulseaudio_multiple_users/
	# and https://xkahn.zoned.net/blog/2021/10/25/multi-user-audio-with-pulseaudio/
	#
	# With (2), PulseAudio is run as a normal "session" server, but we explicitly
	# enable sharing to other users (controlled by group membership, "pulse-access" in
	# Fatdog64 configuration).
	# We can run this as "spot", or as any other user ("pulse" is convenient because it
	# is the same user as the one used by (1) ).
	#
	# (3) is actually (2) when the session user is "root" itself. This works, and in fact
	# is the best in terms of functionality (because it has all access running as root),
	# however obviously security might be a bit of a concern.
	#
	# Fatdog64 supports all the 3 scenarios, with the default being (2).
	# This can be changed in the init script.
	#
	
	# client.conf
	cat >> /etc/pulse/client.conf << EOF &&

# Fatdog64 configuration for shared server operation
autospawn= no                            # do not start server if existing one is not found
default-server = unix:/tmp/pulse-server  # location of fifo to talk to our server
enable-memfd = yes                       # use SHM to do data transfer between client/server
EOF

	cat >> /etc/pulse/daemon.conf << EOF &&

# Fatdog64 configuration for shared server operation
exit-idle-time = -1 # run permanently until we explicitly stop it
EOF

	# system.pa for mode (1) operation
	sed -i -e 's|^load-module module-native-protocol-unix|& auth-cookie=/var/lib/pulse/cookie socket=/tmp/pulse-server|' \
		/etc/pulse/system.pa &&

	# default.pa for mode (2) and (3) operation
	sed -i -e 's|^load-module module-native-protocol-unix|& auth-group=pulse-access socket=/tmp/pulse-server|' \
		/etc/pulse/default.pa &&

	# install our init script - disabled by default
	install -m644 /tmp/pkg/99-pulseaudio /etc/init.d
}
 
