#!/usr/bin/make -f

DEB_BUILD_MAINT_OPTIONS=hardening=+bindnow
DPKG_EXPORT_BUILDFLAGS=1
include /usr/share/dpkg/buildflags.mk

export PREFIX = /usr
export LIBDIR = /usr/lib/spectrwm
export SYSCONFDIR = /etc
export DESTDIR = $(CURDIR)/debian/tmp

%:
	dh $@

# Build, install and clean.
#
# The Makefile we need to use is in the linux/ directory, so we need
# to override the usual dh_auto_* commands to prevent them from using
# the top-level Makefile, which only works on OpenBSD. We also pass a
# few extra variables to customize the installation paths.
override_dh_auto_build:
	$(MAKE) -C linux/

override_dh_auto_install:
	$(MAKE) -C linux/ install
	# Rename the release notes so that dh_installdocs can pick them
	# up with the expected name later
	mv $(DESTDIR)/usr/share/doc/spectrwm/CHANGELOG.md \
	   $(DESTDIR)/usr/share/doc/spectrwm/NEWS

override_dh_auto_clean:
	$(MAKE) -C linux/ clean

# Prevent CHANGELOG.md from being installed.
#
# Despite the name, we don't want it to end up being installed as
# changelog.gz, as doing so would go against Policy.
override_dh_installchangelogs:
	dh_installchangelogs --exclude=CHANGELOG.md

# Don't try to run tests.
#
# spectrwm doesn't have a test suite, but the OpenBSD Makefile in the
# top-level directory confuses dh_auto_test's detection and tricks it
# into thinking one exists.
override_dh_auto_test:

# Don't create shlibs files.
#
# libswmhack.so is not a proper shared library, and it should not be
# treated as such.
override_dh_makeshlibs:

# Rebuild the patches series by looking at the contents of the
# debian/patches directory.
#
# Patches with names starting with D (Debian-specific) should be applied
# after those with names starting with U (to be forwarded upstream).
rebuild-patches-series:
	@( \
		SERIES=debian/patches/series; \
		{ \
			echo "# To rebuild this file, use \`debian/rules $@'"; \
			for patch in debian/patches/U*.diff debian/patches/D*.diff; \
			do \
				test -f "$${patch}" || continue; \
				patch=$$(basename "$${patch}"); \
				echo "$${patch}"; \
			done; \
		} >"$${SERIES}"; \
	)

.PHONY: rebuild-patches-series
