#!/usr/bin/make -f
# output every command that modifies files on the build system.
#DH_VERBOSE = 1

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk

# Necessary for the configuration file (xdg).
DISTRO = $(shell lsb_release -is)
CONFIG_FILE = debian/config/$(DISTRO).conf
ifneq (0, $(shell test -f $(CONFIG_FILE); echo "$$?"))
	DISTRO = Debian
endif

%:
	dh $@ --buildsystem=cmake --builddirectory=_build/

# Midori uses a "poor man's configure" which only accepts a minimal,
# pre-defined set of flags.  Therefore, we invoke cmake directly here.
override_dh_auto_configure:
	dh_auto_configure -- \
	-DCMAKE_INSTALL_PREFIX=/usr \
	-DCMAKE_INSTALL_SYSCONFDIR=/etc \
	-DCMAKE_INSTALL_LIBDIR=/usr/lib \
	..

# cmake does not emit useful information when compiling the source
# files.  We use VERBOSE here for that.
override_dh_auto_build:
	dh_auto_build -- VERBOSE=1

# We have to override 'make check' because it needs to run under
# xvfb-run.  And some tests still fail.
override_dh_auto_test:
	(cd _build && xvfb-run make check) || true

override_dh_auto_install:
	dh_auto_install
	rm -f debian/tmp/usr/share/doc/midori/COPYING debian/tmp/usr/share/doc/midori/TRANSLATE
	install -m 0644 debian/$(DISTRO)-config debian/tmp/etc/xdg/midori/config

# Generate the debuginfo package.
override_dh_strip:
	dh_strip --dbg-package=midori-dbg

# Updating the alternatives priority
PRIORITY = $(shell sed -ne 's/^DEBIAN_WWW_ALTERNATIVES_PRIORITY \([0-9]\+\)/\1/p' $(CONFIG_FILE))

debian/midori.postinst: debian/midori.postinst.base
	sed 's/DEBIAN_WWW_ALTERNATIVES_PRIORITY/$(PRIORITY)/g' debian/midori.postinst.base > debian/midori.postinst

override_dh_installdeb: debian/midori.postinst
	dh_installdeb

override_dh_installchangelogs:
	rm -f debian/midori/usr/share/doc/midori/ChangeLog
	dh_installchangelogs
