#!/usr/bin/make -f

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

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

CFLAGS = -Wall -g
ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
	CONFIG_OPTIONS =
else
	CFLAGS += -O2
ifneq (,$(filter ratpoison:gendeps,$(DEB_BUILD_OPTIONS)))
	CONFIG_OPTIONS =
else
	CONFIG_OPTIONS = --disable-dependency-tracking
endif
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

config.status: configure
	dh_testdir
	./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) \
	    --prefix=/usr \
	    --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info \
	    --with-xterm=x-terminal-emulator \
	    --with-menu="/etc/X11/ratpoison/ratpoisonmenu debian.menu" \
	    $(CONFIG_OPTIONS) \
	    --x-includes="" --x-libraries="" \
	    CFLAGS="$(CFLAGS)" CPPFLAGS="" LDFLAGS="-Wl,-z,syms"
	# libXext is not needed for dynamic linking:
	sed -i -e '/^X_LIBS =/s/-lXext //' src/Makefile
# do not delete config.status if above rule failed,
# as that might hide other left over files that need cleaning
.PRECIOUS: config.status

build-arch: build-arch-stamp
build-arch-stamp: config.status
	dh_testdir

	$(MAKE)
	# bindings for the different languages
	cd contrib && ./genrpbindings
	# manpage for rpws
	pod2man contrib/rpws rpws.1
	touch build-arch-stamp

build-indep: build-indep-stamp
build-indep-stamp:
	touch build-indep-stamp
build: build-arch build-indep

clean:
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp build-indep-stamp

	if [ -e config.status ] ; then $(MAKE) distclean ; fi
	! test -f config.log
	-rm -f contrib/Ratpoison.pm contrib/ratpoison-cmd.el contrib/ratpoison.lisp contrib/ratpoison.py contrib/ratpoison.rb
	-rm -f rpws.1
	-rm -f config.sub config.guess
	dh_clean

# Build architecture-independent files
binary-indep: build-indep-stamp
# We have nothing to do

# Build architecture-dependent files
binary-arch: build-arch-stamp
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	$(MAKE) install pkgdatadir=/usr/share/doc/ratpoison/examples DESTDIR=$(CURDIR)/debian/ratpoison
	# 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
	rm debian/ratpoison/usr/share/doc/ratpoison/ChangeLog
	# 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
	dh_install
	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_installexamples
	dh_installemacsen
	dh_installmenu
	dh_installinfo doc/ratpoison.info
	dh_installman rpws.1
	dh_link
	dh_strip
	dh_compress
	find debian/ratpoison -name "dir.gz" -print -delete
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

RATPOISON_VERSION=1.4.5

# some helpers for me
# just ignore those if you want to do a NMU or a security upload, change the files outside
# of debian/patches and do an upload. The patches are only stored there for reference and
# so I do not loose them.
maintainer-clean:
	quilt refresh
	quilt push -a || true
	rm -f .pc patches
	mv debian/patches/series debian/patches/_series

maintainer-unclean:
	mv debian/patches/_series debian/patches/series
	ln -s "../pc-ratpoison-$(RATPOISON_VERSION)" .pc
	ln -s debian/patches patches
	quilt push -a

binary: binary-indep binary-arch
.PHONY: build-arch build-indep build clean binary-indep binary-arch binary maintainer-clean maintainer-unclean
