#!/usr/bin/make -f

#export DH_VERBOSE=
include /usr/share/quilt/quilt.make

CFLAGS=-Wall -g
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
        CFLAGS += -O0
else
        CFLAGS += -O2
endif

VERSION=$(shell awk '/^PACKAGE_VERSION/ { print $$2 }' "FS='" configure)

build: build-stamp
build-stamp: patch
	dh_testdir

	#remove upstream debian/ stuff
	-rm -f debian/changelog.in debian/menu debian/conffiles debian/post*
	CFLAGS="$(CFLAGS)" ./configure --prefix=/usr --sysconfdir=/etc
	$(MAKE)

	pod2man --section=1 --release=$(VERSION) --center \
		"Openbox documentation" debian/openbox.pod > openbox.1
	#pod2man --section=1 --release=$(VERSION) --center \
	#	"Openbox documentation" debian/kdetrayproxy.pod > kdetrayproxy.1
	pod2man --section=1 --release=$(VERSION) --center \
		"Openbox documentation" debian/gnome-panel-control.pod > \
		gnome-panel-control.1
	pod2man --section=1 --release=$(VERSION) --center \
		"Openbox documentation" debian/gdm-control.pod > \
		gdm-control.1
	touch build-stamp

clean: unpatch
	dh_testdir
	dh_testroot
	rm -f build-stamp install-stamp

	[ ! -f Makefile ] || $(MAKE) distclean

	rm -rf tools/gnome-panel-control/.libs
	rm -rf tools/kdetrayproxy/.libs
	rm -rf openbox/.libs
	rm -rf debian/conffiles

	rm -f openbox.1
	#rm -f kdetrayproxy.1
	rm -f gnome-panel-control.1

	dh_clean

install: install-stamp
install-stamp: build-stamp
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp

	dh_install --sourcedir=debian/tmp -a

	install -m0644 debian/menu.xml $(CURDIR)/debian/openbox/etc/xdg/openbox
	install -m0664 debian/openbox.xpm $(CURDIR)/debian/openbox/usr/share/pixmaps/
	install -m0664 debian/openbox.xsession $(CURDIR)/debian/openbox/usr/share/xsessions/openbox.desktop
	install -m0664 debian/openbox.xsession $(CURDIR)/debian/openbox/usr/share/applications/openbox.desktop
	mkdir -p $(CURDIR)/debian/openbox/var/lib/openbox
	touch install-stamp

binary-indep: build install

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs CHANGELOG
	dh_installdocs
	dh_installmenu
	dh_installman -A
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

.PHONY: build clean binary-indep binary-arch binary install configure
