#!/usr/bin/make -f

# Export all hardening flags
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

FLAGS_profanity_base = \
	--enable-dependency-tracking \
	--enable-c-plugins \
	--enable-plugins \
	--enable-omemo \
	--enable-otr \
	--enable-pgp \
	--libdir=/usr/lib/profanity
FLAGS_profanity_full = $(FLAGS_profanity_base) \
	--enable-python-plugins \
	--enable-notifications \
	--enable-icons-and-clipboard \
	--enable-icons \
	--with-xscreensaver
FLAGS_profanity_light = $(FLAGS_profanity_base) \
	--with-xscreensaver=no \
	--disable-icons-and-clipboard \
	--disable-icons \
	--disable-notifications \
	--disable-python-plugins

BUILDDIR_LIGHT := $(CURDIR)/debian/build-light
BUILDDIR_FULL := $(CURDIR)/debian/build-full

%:
	dh $@ --with autoreconf

override_dh_autoreconf:
# "aclocal" fails without "m4" directory
	[ -d 'm4' ] || mkdir 'm4'
	dh_autoreconf
	rmdir --ignore-fail-on-non-empty 'm4'

override_dh_auto_configure: src/gitversion.h.in
	dh_auto_configure --builddirectory=$(BUILDDIR_LIGHT) -- $(FLAGS_profanity_light)
	PYTHON_VERSION=3; export PYTHON_VERSION; \
	dh_auto_configure --builddirectory=$(BUILDDIR_FULL) -- $(FLAGS_profanity_full)

override_dh_auto_build:
	dh_auto_build -a -- -C $(BUILDDIR_LIGHT)/src
	dh_auto_build -a -- -C $(BUILDDIR_FULL)/src

fix_whatis:
	-mkdir man
	for manpage in docs/*.1; do \
	    sed '0,/^\/.*/s|^\(/.*\)|\1 \\- the Profanity \1 command.|' < $$manpage > man/$$(basename $$manpage); \
	done

override_dh_auto_install: fix_whatis
	$(MAKE) -C $(BUILDDIR_LIGHT) install DESTDIR=$(BUILDDIR_LIGHT).inst
	$(MAKE) -C $(BUILDDIR_FULL) install DESTDIR=$(BUILDDIR_FULL).inst
	sed -i "/dependency_libs/ s/'.*'/''/" $$(find . -name '*.la')

override_dh_auto_clean:
	dh_auto_clean
	rm -fr $(BUILDDIR_FULL) $(BUILDDIR_LIGHT) $(BUILDDIR_FULL).inst $(BUILDDIR_LIGHT).inst

src/gitversion.h.in:
	echo "#ifndef PROF_GIT_BRANCH" > $@
	echo "#define PROF_GIT_BRANCH \"debian/experimental\"" >> $@
	echo "#endif" >> $@
	echo "#ifndef PROF_GIT_REVISION" >> $@
	echo "#define PROF_GIT_REVISION \"$(dpkg-parsechangelog --show-field Version)\"" >> $@
	echo "#endif" >> $@

.PHONY: fix_whatis
