# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Contributor: Johannes Matheis <jomat+alpinebuild@jmt.gr>
# Maintainer: Patrycja Rosa <alpine@ptrcnull.me>
pkgname=mumble
pkgver=1.4.287
_libver=1.4.0
pkgrel=12
pkgdesc="Low-latency, high quality voice chat software"
url="https://wiki.mumble.info/"
arch="all !x86" # since v1.4.274 fail to build on 32bit x86
license="BSD-3-Clause"
options="!check"
depends="qt5-qtbase-sqlite"
makedepends="
	alsa-lib-dev
	avahi-dev
	boost-dev
	cmake
	libcap-dev
	libsndfile-dev
	libxi-dev
	openssl-dev>3
	opus-dev
	poco-dev
	protobuf-dev
	qt5-qtbase-dev
	qt5-qtsvg-dev
	qt5-qttools-dev
	samurai
	speex-dev
	speexdsp-dev
	rnnoise-dev
	"
install="murmur.pre-install"
pkgusers="murmur"
pkggroups="murmur"
subpackages="
	$pkgname-doc
	murmur
	murmur-openrc:murmur_openrc:noarch
	murmur-doc:murmur_doc:noarch
	"
source="https://github.com/mumble-voip/mumble/releases/download/v$pkgver/mumble-$pkgver.tar.gz
	murmur.initd

	pipewire.patch
	poll.patch
	libgen.patch
	musl-1.2.3.patch
	openssl3.patch
	"
builddir="$srcdir/$pkgname-$pkgver.src"

# secfixes:
#   1.3.4-r0:
#     - CVE-2021-27229
#   1.2.19-r9:
#     - CVE-2018-20743

build() {
	CFLAGS="$CFLAGS -flto=auto" \
	CXXFLAGS="$CXXFLAGS -flto=auto" \
	cmake -B build -G Ninja \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DBUILD_SHARED_LIBS=True \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-Doverlay-xcompile=OFF \
		-Dspeechd=OFF \
		-Dice=OFF \
		-Drnnoise=ON \
		-Dbundled-celt=ON \
		-Dbundle-qt-translations=OFF \
		-Dbundled-speex=OFF \
		-Dbundled-opus=OFF \
		-Dbundled-rnnoise=OFF \
		-Dupdate=OFF \
		-DBUILD_NUMBER="${pkgver##*.}"
	cmake --build build
}

package() {
	DESTDIR="$pkgdir" cmake --install build

	# license
	install -Dm644 ./LICENSE \
		"$pkgdir"/usr/share/licenses/$pkgname/LICENSE
	# openrc service
	install -Dm755 "$srcdir"/murmur.initd "$pkgdir"/etc/init.d/murmur
	# server binary is in the murmur package
	rm "$pkgdir"/usr/bin/mumble-server
	rm "$pkgdir"/usr/share/man/man1/mumble-server*
}

murmur() {
	pkgdesc="Murmur is the server component of Mumble"
	cd "$builddir"

	# Regarding the murmurd <-> mumble-server naming
	# See: https://github.com/mumble-voip/mumble
	# Currently, we continue using murmurd everywhere
	# but provide a mumble-server -> murmurd symlink.
	install -o murmur -g murmur -dm755 \
		"$subpkgdir"/var/lib/murmur
	install -Dm755 ./build/mumble-server \
		"$subpkgdir"/usr/bin/murmurd
	ln -s /usr/bin/murmurd "$subpkgdir"/usr/bin/mumble-server
	install -Dm644 ./scripts/murmur.ini \
		"$subpkgdir"/etc/murmur.ini
	install -Dm755 ./scripts/mumble-server-user-wrapper \
		"$subpkgdir"/usr/bin/mumble-server-user-wrapper

	sed 's/^database=$/database=\/var\/lib\/murmur\/murmur.sqlite/;\
		s/^;uname=$/uname=murmur/;\
		s/^;logfile=murmur.log$/logfile=\/var\/log\/murmur.log/;\
		s/^;pidfile=$/pidfile=\/var\/run\/murmur\/murmur.pid/'\
		-i "$subpkgdir"/etc/murmur.ini

	install -Dm644 ./scripts/murmur.conf \
		"$subpkgdir"/usr/share/dbus-1/system.d/murmurd.conf
	sed "s/mumble-server/murmur/" \
		-i "$subpkgdir"/usr/share/dbus-1/system.d/murmurd.conf
}

murmur_doc() {
	pkgdesc="Documentation for the server component of Mumble"

	cd "$builddir"
	install -Dm644 ./man/mumble-server.1 \
		"$subpkgdir"/usr/share/man/man1/murmurd.1
	ln -s /usr/share/man/man1/murmurd.1 \
		"$subpkgdir"/usr/share/man/man1/mumble-server.1
	install -Dm644 ./man/mumble-server-user-wrapper.1 \
		"$subpkgdir"/usr/share/man/man1/mumble-server-user-wrapper.1
	install -Dm644 ./LICENSE \
		"$subpkgdir"/usr/share/licenses/murmur/LICENSE
	default_doc
}

murmur_openrc() {
	default_openrc
	install_if="murmur=$pkgver-r$pkgrel openrc"
}

sha512sums="
34ed30c18257ba8deae6938009a90147c8bc3a0aca28e69bea7ec0262e8d2cdacb9a840fac7d3dd623a52ef8d5903ed5424b62b483af21d6df6aa9632eae9d82  mumble-1.4.287.tar.gz
1edccadd87446a6b9f5b87f19405f92e3450c48334c79fba4bc2c31224be767be084488c4352f14e64b7506b5963d1d6f3d8d4c7ea8e8276104d19a4a03892c6  murmur.initd
6cf05eea35e04def66c263c5134f459cfc8157ed7b4662acb1e99da062a6bcc61c59c9f7424eb0347ae5d545ab0489c21825cf71a305036a633dcffeccec0860  pipewire.patch
2d35b71ce9b537fc3c1d9ec1a280a20209aa0e751b4a4bc8b3ea0d97418787ec19c708d88b51b3acab69f51a601dfa3ae49956afd78bbbe1122f879a58284f5e  poll.patch
2dac9e8e321c7a9837a0e4e2b368c39ffa525012316063e02ae5286c215967e88007a4d89590c251e11020e3d50f8f08eb050a3a02526f758d128b7b061580d2  libgen.patch
2cc0a17a31bf9a436ea97447f958b7a93e96b0fcd020e5389ccfc413b6498b5176294d2b9c788a68cdfac47dabca8e6e28699f5ad184dcde4d30feb88d2e99b3  musl-1.2.3.patch
47560d4f2710b0286b6e9c08cb9d0271636987194e982b9e63690cd9a4bb8a6d4390d3651c2f48f307cf11b96b0cf5213f71c05fb00b07f655f13339bec53ecf  openssl3.patch
"
