#! /usr/bin/make -f

include /usr/share/quilt/quilt.make

##

CXXFLAGS = -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CXXFLAGS += -O0
else
	CXXFLAGS += -O2
endif

##

PYTHONS     := $(shell pyversions -vr debian/control)
DBG_PYTHONS := $(shell pyversions -vr debian/control)

GCCVER := $(shell gcc --version | sed -n '/^gcc (GCC)/s/.*(GCC) \(...\).*/\1/p')
ifneq (,$(filter $(GCCVER),4.2 4.3))
	SPLIT=20
else
	SPLIT=4
endif

.PRECIOUS: build-%/configure-stamp build-%-dbg/configure-stamp

##

configure: $(PYTHONS:%=build-%/configure-stamp) $(DBG_PYTHONS:%=build-%-dbg/configure-stamp)

build-%/configure-stamp: $(QUILT_STAMPFN)
	dh_testdir
	mkdir -p build-$*
	cd build-$* && python$* ../configure.py \
		-c -u -k /usr/lib/kde4/ -o /usr/lib/kde4/include/ -n /usr/lib/kde4/lib/ -j $(SPLIT)
	touch $@

build: $(PYTHONS:%=build-%/build-stamp) $(DBG_PYTHONS:%=build-%-dbg/build-stamp)

build-%/build-stamp: build-%/configure-stamp
	dh_testdir
	$(MAKE) -C build-$*
	touch $@

##

clean: unpatch
	dh_testdir
	dh_testroot
	dh_clean
	rm -f install-stamp
	rm -rf $(PYTHONS:%=build-%) $(DBG_PYTHONS:%=build-%-dbg)
	cd sip; \
	    for moddir in *; do \
		rm -f $$moddir/$${moddir}mod.sip; \
	    done

##

install: install-stamp
install-stamp:
	dh_testdir
	dh_testroot
	dh_clean
	dh_installdirs
	# the -dbg versions have to be installed *and* renamed first, because
	# the get installed into the same directory
	set -e; for version in $(DBG_PYTHONS); do \
	    $(MAKE) -C build-$$version-dbg install DESTDIR=$(CURDIR)/debian/tmp; \
	done
	for mod in $$(find debian/tmp -name '*.so'); do \
	    mv $$mod $$(echo $$mod | sed -re 's/\.so$$/_d.so/'); \
	done
	set -e; for version in $(PYTHONS); do \
	    $(MAKE) -C build-$$version install DESTDIR=$(CURDIR)/debian/tmp; \
	done
	dh_install
	install -D -m 644 debian/python-kde4-doc.lintian-overrides \
	    debian/python-kde4-doc/usr/share/lintian/overrides/python-kde4-doc
	touch $@

##

binary: binary-arch binary-indep

binary-indep: install
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

binary-arch: install
	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-common

binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs AUTHORS THANKS
	dh_link
	dh_strip -X_d.so --dbg-package=python-kde4-dbg
	dh_compress -X.py -X.txt
	dh_fixperms
	dh_pycentral
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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