#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

include /usr/share/dpkg/default.mk

binaries := $(shell dh_listpackages)

%:
	dh $@ --with=gir

override_dh_autoreconf:
	NOCONFIGURE=1 \
	dh_autoreconf \
		--as-needed \
		./autogen.sh \
		-- \
		$(NULL)

configure_options =

ifneq ($(filter nodoc,$(DEB_BUILD_OPTIONS)),)
configure_options += --disable-docbook-docs
configure_options += --disable-documentation
else
configure_options += --enable-docbook-docs
configure_options += --enable-documentation
endif

ifneq ($(filter libflatpak-doc,$(binaries)),)
configure_options += --enable-gtk-doc
else
configure_options += --disable-gtk-doc
endif

override_dh_auto_configure:
	rm -f app/parse-datetime.c
	dh_auto_configure -- \
		--disable-auto-sideloading \
		--disable-selinux-module \
		--enable-gdm-env-file \
		--enable-installed-tests \
		--with-curl=yes \
		--with-dbus-config-dir=/usr/share/dbus-1/system.d \
		--with-priv-mode=none \
		--with-privileged-group=sudo \
		--with-run-media-dir=/media \
		--with-system-bubblewrap=bwrap \
		--with-system-dbus-proxy=xdg-dbus-proxy \
		--with-systemdsystemunitdir=/lib/systemd/system \
		--with-system-helper-user=_flatpak \
		$(configure_options)

override_dh_install:
	install -d debian/tmp/etc/X11/Xsession.d
	install -m644 debian/tmp/etc/profile.d/flatpak.sh \
		debian/tmp/etc/X11/Xsession.d/20flatpak
	find debian/tmp -name '*.la' -print -delete
	dh_install

# debhelper >= 13.4 makes all of /usr/libexec executable, which is not
# quite right for installed-tests
override_dh_fixperms:
	dh_fixperms -Xusr/libexec/installed-tests
ifneq ($(filter %-tests,$(binaries)),)
	chmod --changes u=rw,og=r debian/*-tests/usr/libexec/installed-tests/*/*.so
	chmod --recursive --changes a+rX,u+w,og-w debian/*-tests/usr/libexec/installed-tests
endif

override_dh_installsystemd:
	dh_installsystemd --no-start

override_dh_auto_test-arch:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	debian/test.sh
endif

override_dh_auto_test-indep:
	@:
