#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

DEB_BUILD_MAINT_OPTIONS=hardening=+all
include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/buildflags.mk

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
ifeq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
BUILD_SYSTEM = --build $(DEB_BUILD_GNU_TYPE)
else
BUILD_SYSTEM = --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

CFLAGS += -Wall
LDFLAGS += -Wl,-z,defs

CONFIG_OPTIONS = --with-xkb --with-xrandr --enable-mdoc
ifneq (,$(filter ratpoison:gendeps,$(DEB_BUILD_OPTIONS)))
	CONFIG_OPTIONS +=
else
	CONFIG_OPTIONS += --disable-dependency-tracking
endif
ifneq (,$(filter ratpoison:noxft,$(DEB_BUILD_OPTIONS)))
	CONFIG_OPTIONS += --without-xft
else
	CONFIG_OPTIONS += --with-xft
endif
ifneq (,$(filter ratpoison:readline,$(DEB_BUILD_OPTIONS)))
	CONFIG_OPTIONS += --enable-history
else
	CONFIG_OPTIONS += --disable-history
endif

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	MAKEFLAGS += -j$(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

%:
	dh $@

override_dh_auto_configure:
	dh_auto_configure -- $(BUILD_SYSTEM) \
	    --with-menu="/etc/X11/ratpoison/ratpoisonmenu debian.menu" \
	    $(CONFIG_OPTIONS) \
	    CFLAGS='$(CFLAGS)' CPPFLAGS='$(CPPFLAGS)' LDFLAGS='$(LDFLAGS)'

override_dh_auto_build:
	dh_auto_build
	# bindings for the different languages
	unset RATPOISON ; cd contrib && ./genrpbindings
	# manpage for rpws
	pod2man contrib/rpws rpws.1

override_dh_auto_clean:
	dh_auto_clean
	if [ -e config.status ] ; then $(MAKE) maintainer-clean ; fi
	rm -f m4/pkg.m4
	! test -f config.log
	-rm -f contrib/Ratpoison.pm contrib/ratpoison-cmd.el contrib/ratpoison.lisp contrib/ratpoison.py contrib/ratpoison.rb m4/package.m4
	-rm -rf build-aux
	-rm -f rpws.1
	-rm -f config.sub config.guess

override_dh_auto_install:
	$(MAKE) install pkgdatadir=/usr/share/doc/ratpoison/examples DESTDIR=$(CURDIR)/debian/ratpoison
	sed -e 's%^#!/usr/bin/env perl$$%#!/usr/bin/perl%' -i debian/ratpoison/usr/bin/rpws
	# since Debian uses gnu install-info, this seems to need manual removing...
	find debian/ratpoison -name "dir" -print -delete
	# Those are installed to different places already by dh_*
	rm debian/ratpoison/usr/share/doc/ratpoison/COPYING
	# not needed two times:
	rm debian/ratpoison/usr/share/doc/ratpoison/examples/rpws
	# The menu command:
	install -D -m 0755 debian/callmenu.sh debian/ratpoison/etc/X11/ratpoison/ratpoisonmenu
	for perlscript in `grep -l '#!/usr/bin/env \+perl' debian/*/usr/share/doc/ratpoison/examples/*` ; do \
	    sed -i '1s+#!/usr/bin/env perl+#!/usr/bin/perl+' $${perlscript} ; \
	done

override_dh_installinfo:
	dh_installinfo doc/ratpoison.info

override_dh_compress:
	dh_compress
	find debian/ratpoison -name "dir.gz" -print -delete
