#!/usr/bin/make -f
# -*- makefile -*-

#export DH_VERBOSE=1

include /usr/share/python/python.mk

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 (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

PYVERS=$(shell pyversions -vr 2>/dev/null)

.PRECIOUS: build-%/configure-stamp

configure-stamp:
	dh_testdir
	dh_autoreconf

	touch $@

build-%/configure-stamp: configure-stamp
	dh_testdir

	mkdir -p build-$* 
	cd build-$* && CFLAGS="$(CFLAGS)" PYTHON=/usr/bin/python$* ../configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) \
		--prefix=/usr \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info

	touch $@

build: $(PYVERS:%=build-%/build-stamp)

build-%/build-stamp: build-%/configure-stamp
	dh_testdir
	$(MAKE) -C build-$* pyexecdir=$(call py_libdir_sh, $*)
	touch $@

clean: $(PYVERS:%=clean-%)
	dh_testdir
	dh_testroot
	dh_autoreconf_clean
	dh_clean

clean-%:
	dh_testdir
	dh_testroot
	[ ! -f Makefile ] || $(MAKE) distclean -C build-$*
	[ ! -f Makefile ] || $(MAKE) clean -C build-$*

install-%: build-%/build-stamp
	dh_testdir
	dh_testroot
	$(MAKE) -C build-$* install DESTDIR=$(CURDIR)/debian/python-gtkglext1 pyexecdir=$(call py_libdir_sh, $*)

install-clean:
	dh_testdir
	dh_testroot
	dh_prep

install: build install-clean $(PYVERS:%=install-%)
	dh_installdirs
	dh_install

	mkdir -p debian/python-gtkglext1/usr/share/doc/python-gtkglext1/examples
	cp examples/*.py debian/python-gtkglext1/usr/share/doc/python-gtkglext1/examples/
	cp examples/*.png debian/python-gtkglext1/usr/share/doc/python-gtkglext1/examples/
	cp ChangeLog changelog.tmp
	cat ChangeLog.pre-1-0 >> changelog.tmp
	find debian/python-gtkglext1/usr -type d -empty | xargs -r rmdir -p --ignore-fail-on-non-empty
	find debian/python-gtkglext1/usr/lib -name 'apputils.py' | xargs chmod +x
	find debian/python-gtkglext1/usr/lib -name '*.la' -delete

binary-indep: build install

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs changelog.tmp
	dh_installdocs
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_python2
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

build-arch: build
build-indep: build

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