#!/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.
#

# TODO: add MONO option.

PKGNAME=avahi
PKGVERSION=0.7
PKGREVISION=1
PKGLICENSE="LGPLv21"
PKGSUMMARY="DNS-SD over mDNS implementation"
PKGDOWNLOAD="http://avahi.org/download/$PKGNAME-$PKGVERSION.tar.gz"
PKGUIDS="avahi:107 avahi-autoipd:108"
PKGGIDS="avahi:107 avahi-autoipd:108"
PKGDEPENDS="expat libdaemon"
PKGOPTIONS="DBUS GLIB GOBJECT GTK INTROSPECTION NLS"
PKGPROTECT="etc/avahi/avahi-autoipd.action
            etc/avahi/avahi-daemon.conf
            etc/avahi/avahi-dnfconfd.action
            etc/avahi/hosts
            etc/rc.d/avahi"

: ${CHOST:=$(uname -m)-linux-musl}
: ${CBUILD:=$(uname -m)-linux-musl}
: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${CXX:=clang++}
: ${CXXFLAGS:=-O2}
: ${LDFLAGS:=}
: ${LIBS:=}
: ${DBUS:=yes}
: ${GLIB:=yes}
: ${GOBJECT:=yes}       # Note: requires GLIB.
: ${GTK:=yes}
: ${INTROSPECTION:=yes}
: ${NLS:=yes}

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

if [ "$GLIB" = "yes" ]; then
	PKGDEPENDS="glib $PKGDEPENDS"
	with_glib="--enable-glib"
else
	with_glib="--disable-glib"
fi

if [ "$GOBJECT" = "yes" ]; then
	with_gobject="--enable-gobject"
else
	with_gobject="--disable-gobject"
fi

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

if [ "$INTROSPECTION" = "yes" ]; then
	PKGDEPENDS="gobject-introspection:build $PKGDEPENDS"
	with_introspection="--enable-introspection"
else
	with_introspection="--disable-introspection"
fi

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

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

	# --disable-python: does not support Python 3 yet.
	CC="$CC" \
	CFLAGS="$CFLAGS" \
	CXX="$CXX" \
	CXXFLAGS="$CXXFLAGS" \
	LDFLAGS="$LDFLAGS" \
	LIBS="$LIBS" \
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix= \
		--sbindir=/bin \
		--disable-gtk \
		--disable-qt3 \
		--disable-qt4  \
		--disable-mono \
		--disable-monodoc \
		--disable-python \
		--with-xml=expat \
		--with-distro="none" \
		--without-systemdsystemunitdir \
		$with_dbus \
		$with_glib \
		$with_gobject \
		$with_gtk \
		$with_introspection \
		$with_nls
	make
	make install DESTDIR=$DESTDIR
	rmdir $DESTDIR/run
	find $DESTDIR -type f -name "*.la" -delete
	install -Dm644 ../avahi $DESTDIR/etc/rc.d/avahi

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