#!/bin/busybox sh
#
# Copyright (c) 2019 David Demelier <markand@malikania.fr>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

PKGNAME=pulseaudio
PKGVERSION=12.2
PKGREVISION=1
PKGLICENSE="LGPLv21+"
PKGSUMMARY="general-purpose sound server"
PKGDOWNLOAD="https://www.freedesktop.org/software/$PKGNAME/releases/$PKGNAME-$PKGVERSION.tar.xz"
PKGDEPENDS="gdbm glib libtool libsndfile"
PKGOPTIONS="ALSA
            AVAHI
            BLUETOOTH
            CAP
            DBUS
            GTK
            IPV6
            JACK
            NLS
            OSS
            SOXR
            SPEEX
            SSL
            UDEV
            X"
PKGUIDS="pulse:102"
PKGGIDS="pulse:102 pulse-rt:103 pulse-access:104"

: ${CHOST:=$(uname -m)-linux-musl}
: ${CBUILD:=$(uname -m)-linux-musl}
: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${LDFLAGS:=}
: ${LIBS:=}
: ${ALSA:=yes}
: ${AVAHI:=yes}
: ${BLUETOOTH:=no}      # TODO: set to on once sbc is there (for bluez)
: ${CAP:=yes}
: ${DBUS:=yes}
: ${GTK:=yes}
: ${IPV6:=yes}
: ${JACK:=yes}
: ${NLS:=yes}
: ${OSS:=yes}
: ${SOXR:=yes}
: ${SPEEX:=yes}
: ${SSL:=yes}
: ${UDEV:=yes}
: ${X:=yes}

if [ "$ALSA" = "yes" ]; then
	PKGDEPENDS="alsa-lib $PKGDEPENDS"
	with_alsa="--enable-alsa"
else
	with_alsa="--disable-alsa"
fi

if [ "$AVAHI" = "yes" ]; then
	PKGDEPENDS="avahi $PKGDEPENDS"
	with_avahi="--enable-avahi"
else
	with_avahi="--disable-avahi"
fi

if [ "$BLUETOOTH" = "yes" ]; then
	PKGDEPENDS="bluez $PKGDEPENDS"
	with_bluetooth="--enable-bluez5"
else
	with_bluetooth="--disable-bluez5"
fi

if [ "$CAP" = "yes" ]; then
	PKGDEPENDS="libcap $PKGDEPENDS"
	with_cap="--with-caps"
else
	with_cap="--without-caps"
fi

if [ "$DBUS" = "yes" ]; then
	PKGDEPENDS="dbus $PKGDEPENDS"
	with_dbus="--enable-dbus"
else
	with_dbus="--disable-dbus"
fi

if [ "$GTK" = "yes" ]; then
	PKGDEPENDS="gtk at-spi2-atk $PKGDEPENDS"
	with_gtk="--enable-gtk3"
else
	with_gtk="--disable-gtk3"
fi

if [ "$IPV6" = "yes" ]; then
	with_ipv6="--enable-ipv6"
else
	with_ipv6="--disable-ipv6"
fi

if [ "$JACK" = "yes" ]; then
	PKGDEPENDS="jack2 $PKGDEPENDS"
	with_jack="--enable-jack"
else
	with_jack="--disable-jack"
fi

if [ "$NLS" = "yes" ]; then
	PKGDEPENDS="gettext $PKGDEPENDS"
	with_nls="--enable-nls"
else
	with_nls="--disable-nls"
fi

if [ "$OSS" = "yes" ]; then
	with_oss="--enable-oss-output --enable-oss-wrapper"
else
	with_oss="--disable-oss-output --disable-oss-wrapper"
fi

if [ "$SOXR" = "yes" ]; then
	PKGDEPENDS="soxr $PKGDEPENDS"
	with_soxr="--with-soxr"
else
	with_soxr="--without-soxr"
fi

if [ "$SPEEX" = "yes" ]; then
	PKGDEPENDS="speex $PKGDEPENDS"
	with_speex="--with-speex"
else
	with_speex="--without-speex"
fi

if [ "$SSL" = "yes" ]; then
	PKGDEPENDS="libressl $PKGDEPENDS"
	with_ssl="--enable-openssl"
else
	with_ssl="--disable-openssl"
fi

if [ "$UDEV" = "yes" ]; then
	PKGDEPENDS="eudev $PKGDEPENDS"
	with_udev="--enable-udev"
else
	with_udev="--disable-udev"
fi

if [ "$X" = "yes" ]; then
	PKGDEPENDS="libx11 libxcb libice libsm libxtst $PKGDEPENDS"
	with_x="--enable-x11"
else
	with_x="--disable-x11"
fi

build()
{
	rm -rf $PKGNAME-$PKGVERSION
	tar xvf $PKGNAME-$PKGVERSION.tar.xz
	cd $PKGNAME-$PKGVERSION

	CC="$CC" \
	CFLAGS="$CFLAGS" \
	LDFLAGS="$LDFLAGS" \
	LIBS="$LIBS" \
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix= \
		--disable-bluez4 \
		$with_alsa \
		$with_avahi \
		$with_bluetooth \
		$with_cap \
		$with_dbus \
		$with_gtk \
		$with_ipv6 \
		$with_jack \
		$with_nls \
		$with_oss \
		$with_soxr \
		$with_speex \
		$with_ssl \
		$with_udev \
		$with_x
	make
	make install DESTDIR=$DESTDIR
	find $DESTDIR -type f -name "*.la" -delete

	cd ..
	rm -rf $PKGNAME-$PKGVERSION
}
