#!/bin/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=bluez
PKGVERSION=5.50
PKGREVISION=1
PKGLICENSE="GPLv2 LGPLv21"
PKGSUMMARY="official bluetooth protocol stack for Linux"
PKGDOWNLOAD="http://www.kernel.org/pub/linux/bluetooth/$PKGNAME-$PKGVERSION.tar.xz"
PKGDEPENDS=""
PKGOPTIONS="AUDIO CUPS LEGACY UDEV"
PKGPROTECT="etc/bluetooth/main.conf
            etc/rc.d/bluetooth"

: ${CHOST:=$(uname -m)-linux-musl}
: ${CBUILD:=$(uname -m)-linux-musl}
: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${CXX:=clang++}
: ${CXXFLAGS:=-O2}
: ${LDFLAGS:=}
: ${LIBS:=}
: ${AUDIO:=yes}
: ${CUPS:=yes}
: ${LEGACY:=no}
: ${UDEV:=yes}

if [ "$AUDIO" = "yes" ]; then
	with_audio="--enable-a2dp"
else
	with_audio="--disable-a2dp"
fi

if [ "$CUPS" = "yes" ]; then
	with_cups="--enable-cups"
else
	with_cups="--disable-cups"
fi

if [ "$LEGACY" = "yes" ]; then
	with_legacy="--enable-deprecated"
else
	with_legacy="--disable-deprecated"
fi

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

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

	# --enable-library: required by many packages.
	CC="$CC" \
	CFLAGS="$CFLAGS" \
	CXX="$CXX" \
	CXXFLAGS="$CXXFLAGS" \
	LDFLAGS="$LDFLAGS" \
	LIBS="$LIBS" \
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix= \
		--disable-systemd \
		--enable-library \
		$with_audio \
		$with_cups \
		$with_legacy \
		$with_udev
	make
	make install DESTDIR=$DESTDIR
	rm -f $DESTDIR/lib/libbluetooth.la
	install -Dm0644 src/main.conf $DESTDIR/etc/bluetooth/main.conf
	install -Dm0644 ../bluetooth $DESTDIR/etc/rc.d/bluetooth

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