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

export DEB_BUILD_MAINT_OPTIONS = hardening=+pie,+bindnow

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))
debug_build_prefix := /usr/lib/$(DEB_HOST_MULTIARCH)/dbus-1.0/debug-build

export DEB_LDFLAGS_MAINT_APPEND = \
	-Wl,--as-needed \
	-Wl,--gc-sections \
	$(NULL)

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

ifeq ($(filter noudeb,$(DEB_BUILD_OPTIONS)),)
# keep the udeb
else
dh_options += -Ndbus-udeb -N$(libdbusN)-udeb
endif

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

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

ifeq ($(filter stage1,$(DEB_BUILD_PROFILES)),)
ifeq ($(DEB_HOST_ARCH_OS),linux)
# Non-bootstrap build on Linux: add Linux-specifics
common_configure_flags += \
		--enable-apparmor \
		--enable-libaudit \
		--enable-selinux \
		--enable-systemd \
		--with-systemdsystemunitdir=/lib/systemd/system \
		SYSTEMCTL=/bin/systemctl \
		$(NULL)
normal_configure_flags += \
	--enable-user-session \
	--with-systemduserunitdir='$${prefix}/lib/systemd/user' \
	$(NULL)
endif
endif

debug_configure_flags := \
	--disable-xml-docs \
	--disable-doxygen-docs \
	--enable-asserts \
	--enable-checks \
	--enable-verbose-mode \
	--with-valgrind=auto \
	--prefix=${debug_build_prefix} \
	--libdir='$${prefix}/lib' \
	--sysconfdir='$${prefix}/etc' \
	--with-systemdsystemunitdir='$${libdir}/systemd/system' \
	$(NULL)

udeb_configure_flags := \
	$(normal_configure_flags) \
	--disable-apparmor \
	--disable-asserts \
	--disable-checks \
	--disable-doxygen-docs \
	--disable-libaudit \
	--disable-selinux \
	--disable-stats \
	--disable-systemd \
	--disable-tests \
	--disable-user-session \
	--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:
	dh_auto_configure \
		-- \
		$(common_configure_flags) \
		$(normal_configure_flags) \
		$(NULL)
	dh_auto_configure \
		--builddirectory=debian/build-debug \
		-- \
		$(common_configure_flags) \
		$(debug_configure_flags) \
		$(NULL)
ifeq ($(filter noudeb,$(DEB_BUILD_OPTIONS)),)
	dh_auto_configure \
		--builddirectory=debian/build-udeb \
		-- \
		$(common_configure_flags) \
		$(udeb_configure_flags) \
		$(NULL)
endif

override_dh_auto_build:
	dh_auto_build
	dh_auto_build --builddirectory=debian/build-debug
ifeq ($(filter noudeb,$(DEB_BUILD_OPTIONS)),)
	dh_auto_build --builddirectory=debian/build-udeb
endif

# 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:
ifeq ($(filter noudeb,$(DEB_BUILD_OPTIONS)),)
	# udeb build
	make -C debian/build-udeb install-exec DESTDIR=$(CURDIR)/debian/tmp-udeb
	cd $(CURDIR)/debian/tmp-udeb && find | LC_ALL=C sort
endif
	# debug build
	make -C debian/build-debug install DESTDIR=$(CURDIR)/debian/tmp
	rm -rfv debian/tmp/${debug_build_prefix}/etc/dbus-1/session.d
	rm -rfv debian/tmp/${debug_build_prefix}/etc/dbus-1/system.d
	rm -rfv debian/tmp/${debug_build_prefix}/share/dbus-1/services
	rm -rfv debian/tmp/${debug_build_prefix}/share/dbus-1/session.d
	rm -rfv debian/tmp/${debug_build_prefix}/share/dbus-1/system.d
	rm -rfv debian/tmp/${debug_build_prefix}/share/dbus-1/system-services
	rm -rfv debian/tmp/${debug_build_prefix}/share/doc
	rm -rfv debian/tmp/${debug_build_prefix}/share/man
	mv debian/tmp/${debug_build_prefix}/share/installed-tests/dbus \
		debian/tmp/${debug_build_prefix}/share/installed-tests/dbus-debug-build
	rm -f debian/tmp/${debug_build_prefix}/lib/libdbus-1.la
	cd $(CURDIR)/debian/tmp && find | LC_ALL=C sort
	# normal build (do this last so that it overwrites the others)
	dh_auto_install
	rm -f debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libdbus-1.la
ifeq ($(filter nodocs,$(DEB_BUILD_OPTIONS))$(filter stage1,$(DEB_BUILD_PROFILES)),)
	# Reproducible build: sed out the generated dates from the man pages.
	sed -i \
		-e '/^\.\\\" *Date: .*/d' \
		-e 's/^\(.TH "[^"]*" "[^"]*" \)"[^"]*"/\1""/' \
		debian/tmp/usr/share/man/man?/*
endif
	# Remove the legacy bus configuration files. We move their old
	# versions out of the way, include the moved names in the new
	# bus setup instead of the old names, and replace the old names
	# with a symlink to the new bus setup so that old dbus-daemons
	# can still reload their configuration.
	rm -f debian/tmp/etc/dbus-1/session.conf
	rm -f debian/tmp/etc/dbus-1/system.conf
	# dbus.target no longer exists. This will be removed upstream in 1.11
	rm -f -r -v debian/tmp/lib/systemd/system/dbus.target.wants
	# if bootstrapping it's possible that no tests will get installed - create
	# the directory in case that's happened, so the .install file doesn't need
	# modification
	install -d debian/tmp/usr/lib/dbus-1.0/installed-tests/dbus
	install -d debian/tmp/${debug_build_prefix}/lib/dbus-1.0/installed-tests/dbus
	# Fix up the development symlink for us installing the shared library
	# in /lib
	target=$$(readlink "debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libdbus-1.so"); \
	target=$$(basename "$$target"); \
	ln -fns /lib/$(DEB_HOST_MULTIARCH)/$$target debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libdbus-1.so

ifeq ($(DEB_DISTRIBUTION),UNRELEASED)
# We can't expect the install files to be fully up to date for a snapshot.
dh_install_options += --list-missing
else
dh_install_options += --fail-missing
endif

override_dh_install:
	dh_install $(dh_install_options)

override_dh_installinit:
	dh_installinit -pdbus --no-start

# 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

debian/$(libdbusN).symbols: debian/$(libdbusN).symbols.in
	sed -e 's/$${DEB_VERSION}/${DEB_VERSION}/g' \
		< $< > $@

ifeq ($(DEB_DISTRIBUTION),UNRELEASED)
# We can't expect the symbols file to be fully up to date for a snapshot,
# so only fail if we broke ABI. This overrides the -c4 below.
export DPKG_GENSYMBOLS_CHECK_LEVEL = 1
endif

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

override_dh_shlibdeps:
	dh_shlibdeps \
		-pdbus-1-dbg \
		-Ldbus-1-dbg \
		-ldbus-1-dbg/${debug_build_prefix}/lib \
		-- \
		-Ldebian/dbus-1-dbg.shlibs.local \
		$(NULL)
	dh_shlibdeps --remaining-packages --

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=debian/build-debug
	dh_auto_clean --builddirectory=debian/build-udeb
	rm -f debian/build-*/test/data/valid-config-files/session.conf
	rm -f debian/build-*/test/data/valid-config-files/system.conf
	rm -f dbus.devhelp

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