#!/usr/bin/make -f
# Copyright  2002,2003 Colin Walters <walters@verbum.org>
# Copyright  2003 Daniel Stone <daniels@debian.org>
# Copyright  2006 Sjoerd Simons <sjoerd@debian.org>
# Copyright  2011 Michael Biebl <biebl@debian.org>

include /usr/share/dpkg/default.mk

%:
	dh $@ $(dh_options)

libdbusN := $(shell sed -rn 's/Package:[[:space:]]*(libdbus-[0-9-]+)[[:space:]]*$$/\1/p' debian/control | head -n 1)
libdbus_soname := $(patsubst libdbus-1-%,libdbus-1.so.%,$(libdbusN))

LDFLAGS += -Wl,--as-needed

dh_options = \
	--builddirectory=build \
	--with autoreconf \
	--parallel \
	$(NULL)

common_configure_flags := \
		--disable-silent-rules \
		--enable-installed-tests \
		--libexecdir=\$${prefix}/lib/dbus-1.0 \
		$(NULL)

ifeq ($(filter stage1,$(DEB_BUILD_PROFILES)),)
ifeq ($(DEB_HOST_ARCH_OS),linux)
# Non-bootstrap build on Linux: add Linux-specifics
with_systemd = yes
common_configure_flags += \
		--enable-libaudit \
		--enable-selinux \
		--enable-systemd \
		--with-systemdsystemunitdir=/lib/systemd/system \
		$(NULL)
endif
endif

normal_configure_flags := \
	--libdir=/usr/lib/$(DEB_HOST_MULTIARCH) \
	$(NULL)

debug_configure_flags := \
	--disable-xml-docs \
	--disable-doxygen-docs \
	--enable-asserts \
	--enable-checks \
	--enable-verbose-mode \
	--with-valgrind=auto \
	--prefix=/usr/lib/$(DEB_HOST_MULTIARCH)/dbus-1.0/debug-build \
	--libdir='$${prefix}/lib' \
	$(NULL)

udeb_configure_flags := \
	$(normal_configure_flags) \
	--disable-asserts \
	--disable-checks \
	--disable-doxygen-docs \
	--disable-libaudit \
	--disable-selinux \
	--disable-systemd \
	--disable-tests \
	--disable-verbose-mode \
	--disable-xml-docs \
	CFLAGS='$(CFLAGS) -Os' \
	$(NULL)

ifeq ($(filter nodocs,$(DEB_BUILD_OPTIONS))$(filter stage1,$(DEB_BUILD_PROFILES)),)
# documentation enabled
normal_configure_flags += \
	--enable-xml-docs \
	--enable-doxygen-docs \
	$(NULL)
else
# documentation disabled
dh_options += -Ndbus-1-doc
dh_install_options += -X/usr/share/man/
normal_configure_flags += \
	--disable-xml-docs \
	--disable-doxygen-docs \
	$(NULL)
endif

ifeq ($(filter stage1,$(DEB_BUILD_PROFILES)),)
# not a stage1 build - build all the tests
debug_configure_flags += \
		--enable-tests \
		$(NULL)
endif

override_dh_auto_configure:
	cp debian/dbus.install-generic debian/dbus.install
	if [ -n "$(with_systemd)" ]; then \
		cat debian/dbus.install-systemd >> \
			debian/dbus.install; \
	fi
	dh_auto_configure \
		-- \
		$(common_configure_flags) \
		$(normal_configure_flags) \
		$(NULL)
	dh_auto_configure \
		--builddirectory=build-debug \
		-- \
		$(common_configure_flags) \
		$(debug_configure_flags) \
		$(NULL)
	dh_auto_configure \
		--builddirectory=build-udeb \
		-- \
		$(common_configure_flags) \
		$(udeb_configure_flags) \
		$(NULL)

override_dh_auto_build:
	dh_auto_build
	dh_auto_build --builddirectory=build-debug
	dh_auto_build --builddirectory=build-udeb

# The tests need more environmental setup at the moment: they need our
# home directory, and we can't just set HOME because the nonce-tcp
# transport uses our "official" HOME according to getpwent(), since its
# purpose is to prevent one user from impersonating another. (#630152)
override_dh_auto_test:
	:

override_dh_auto_install:
	dh_auto_install
	make -C build-debug install-exec DESTDIR=$(CURDIR)/debian/tmp
	make -C build-udeb install-exec DESTDIR=$(CURDIR)/debian/tmp-udeb

override_dh_link:
	dh_link -plibdbus-1-dev lib/$(DEB_HOST_MULTIARCH)/$$(basename $$(readlink debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libdbus-1.so)) usr/lib/$(DEB_HOST_MULTIARCH)/libdbus-1.so
	dh_link --remaining-packages

# if bootstrapping (without dbus-glib), no tests will get installed - create
# the directory in case that's happened, so the .install file doesn't need
# modification
override_dh_install:
	install -d debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/dbus-1.0/test
	rm -f debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libdbus-1.la
	rm -f debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/dbus-1.0/debug-build/lib/libdbus-1.la
	dh_install -p$(libdbusN) \
		"usr/lib/$(DEB_HOST_MULTIARCH)/$(libdbus_soname)*" \
		lib/$(DEB_HOST_MULTIARCH)
	dh_install -pdbus-udeb
	dh_install -p$(libdbusN)-udeb \
		--sourcedir=debian/tmp-udeb \
		"usr/lib/$(DEB_HOST_MULTIARCH)/$(libdbus_soname)*" \
		lib/$(DEB_HOST_MULTIARCH)
	dh_install --remaining-packages --list-missing $(dh_install_options)
	install -m 644 -D debian/dbus-Xsession debian/dbus-x11/etc/X11/Xsession.d/75dbus_dbus-launch

override_dh_installinit:
	dh_installinit -pdbus -r

# we don't want docs for the debug symbols, just symlink to the library docs
override_dh_installdocs:
	dh_installdocs -pdbus-1-dbg --link-doc=$(libdbusN)
	dh_installdocs -pdbus-udeb -p$(libdbusN)-udeb --no-act
	dh_installdocs --remaining-packages --all AUTHORS NEWS README

override_dh_strip:
	dh_strip --dbg-package=dbus-1-dbg

override_dh_makeshlibs:
	dh_makeshlibs -V -Ndbus-1-dbg --add-udeb=$(libdbusN)-udeb -- -c4

override_dh_autoreconf:
	cp INSTALL INSTALL.orig
	dh_autoreconf
	mv INSTALL.orig INSTALL

override_dh_autoreconf_clean:
	cp INSTALL INSTALL.orig
	dh_autoreconf_clean
	mv INSTALL.orig INSTALL

override_dh_auto_clean:
	dh_auto_clean
	dh_auto_clean --builddirectory=build-debug
	dh_auto_clean --builddirectory=build-udeb
	rm -f build/test/data/valid-config-files/session.conf
	rm -f build/test/data/valid-config-files/system.conf
	rm -f build-debug/test/data/valid-config-files/session.conf
	rm -f build-debug/test/data/valid-config-files/system.conf
	rm -f dbus.devhelp

clean:
	rm -rf debian/tmp-udeb
	dh $@ $(dh_options)
