# Contributor: Alexey Min <alexey.min@gmail.com>
# Maintainer: Alexey Min <alexey.min@gmail.com>
pkgname="pulseaudio-modules-droid"
# pkgver should match MAJOR.MINOR pulseaudio version
pkgver=13.0
# _pkgver used in URL to download tarball
# Yes, we can build slightly older modules version against newer pulseaudio, if we are lucky
_pkgver=12.2.79
pkgrel=1
pkgdesc="PulseAudio Droid modules by Mer project"
url="https://github.com/mer-hybris/pulseaudio-modules-droid"
arch="x86 armv7 aarch64"
license="LGPL-2.1-or-later"
depends="pulseaudio"
makedepends="autoconf automake libtool m4 intltool dbus-dev expat-dev pulseaudio-dev pulsecore-private-headers android-headers-7.1 android-headers-7.1-caf libhybris-dev libhybris-7.1"
options="!check"  # No test suite available
source="
	$pkgname-$_pkgver.tar.gz::https://github.com/mer-hybris/$pkgname/archive/$_pkgver.tar.gz
	0001-configure-ac-compat.patch
	0002-Fix-compatibility-with-pulseaudio-13.0.patch
"
install="$pkgname.post-install"
subpackages=""
_headers_variants="7.1 7.1-caf"
for _ver in $_headers_variants; do
	subpackages="$subpackages $pkgname-$_ver:_variant"
done

builddir="$srcdir/$pkgname-$_pkgver"
_tmppkgdir="$srcdir/tmpinstall"
_list_of_libs="
	libdroid-sink.so
	libdroid-source.so
	libdroid-util.so
	module-droid-card.so
	module-droid-keepalive.so
	module-droid-sink.so
	module-droid-source.so
"

prepare() {
	default_prepare
	echo $pkgver > .tarball-version
	autoreconf --force --install
}

build() {
	# Hack to build against CAF android headers:
	#   use our own local pkg-config path folder
	mkdir pkgconfig
	ln -s /usr/lib/pkgconfig/android-headers-7.1.pc pkgconfig/android-headers.pc
	export PKG_CONFIG_PATH="$builddir/pkgconfig"

	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--localstatedir=/var \
		--enable-shared \
		--disable-static

	# build several times against different android headers variation
	for _ver in $_headers_variants; do
		msg "building $pkgname-$_ver"

		# Before each build make sure that include path points to correct location:
		# instead of re-symlinking android-headers.pc file and re-running
		# configure every time, just rewrite include paths in every Makefile
		for _makefile in "Makefile src/Makefile src/common/Makefile src/droid/Makefile"; do
			sed -i -E "s~/usr/include/android-[^ ]+~/usr/include/android-$_ver~g" $_makefile
		done

		make clean
		make

		# each variation is installed into a unique temporary directory
		make DESTDIR="${_tmppkgdir}/$pkgname-$_ver" install
	done
}

package() {
	# here we should run install for some common files
	# for both subpackages, but we don't have anything like that
	# pkgdir is still needed, otherwise "rootpkg failed"
	mkdir -p "$pkgdir"
}

_variant() {
	depends="$pkgname"
	mkdir -p "$subpkgdir"
	# Copy libs from temp install folder to a proper subpackage dir
	for _lib in $_list_of_libs; do
		install -Dm644 ${_tmppkgdir}/${subpkgname}/usr/lib/pulse-${pkgver}/modules/${_lib} \
			${subpkgdir}/usr/lib/pulse-${pkgver}/modules/${_lib}
	done
}

sha512sums="9190dfd5891e0ed580e3135eebb322a00ec99cd3af708047e75c23bd5bf6681151ac1facda08f933679464afff15d421168e549b0ae84756b0e3c97d52693865  pulseaudio-modules-droid-12.2.79.tar.gz
2aa283c64d8623c035522b89b62c80130d14c145a73e6193e2e98c26525cc12670e50f8db4649029e731b61d08c1e5ab2bd4e1a3dae8ca077bbdabe3a69eaa17  0001-configure-ac-compat.patch
e5cf7c3c11fa096bcd2fbe95c9e4135c0b9c1383b2c8c2c0cf43a554122849cc01d9e2b99cab9ec0ef51375ca013e653c6bdfd5c86145a18b3a70cef8bdf7b3e  0002-Fix-compatibility-with-pulseaudio-13.0.patch"
