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

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

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

# the following files are modified in the build process, but shouldn't be.
#UPSTREAM_PRESERVE:=po/Makefile.in.in

# the following are generated by autoconf and have no way of being deleted
# gracefully. 
AUTOFOO_DELETE:=aclocal.m4 config.guess config.h.in config.sub configure \
                depcomp install-sh intltool-extract.in intltool-merge.in \
                intltool-update.in ltmain.sh missing mkinstalldirs INSTALL

PYVERS=$(shell pyversions -r)

configure: configure-stamp
configure-stamp:
	dh_testdir
	for f in $(UPSTREAM_PRESERVE); do \
		cp -a $$f $$f.preserve; \
	done
	autoreconf -v --install
	for p in $(PYVERS); do \
		mkdir build-$$p; \
		( cd build-$$p && \
		  ../configure --prefix=/usr --disable-static PYTHON=$$p ); \
		ln -s ../../src/compizconfig.pyx build-$$p/src; \
	done
	touch configure-stamp

build: build-stamp
build-stamp: configure-stamp 
	dh_testdir
	for p in $(PYVERS); do \
		$(MAKE) -C build-$$p; \
	done
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	for p in $(PYVERS); do \
		rm -f build-$$p/src/compizconfig.pyx; \
		[ ! -f build-$$p/Makefile ] || $(MAKE) -C build-$$p distclean; \
	done
	# clean up extra automake cruft
	find . -type f -name Makefile.in -print0 | xargs -0 rm -f
	rm -f $(AUTOFOO_DELETE)
	for f in $(UPSTREAM_PRESERVE); do \
		if [ -f $$f.preserve ]; then \
			cp -a $$f.preserve $$f; rm -f $$f.preserve; \
		fi; \
	done
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs
	for p in $(PYVERS); do \
		$(MAKE) -C build-$$p DESTDIR=$(CURDIR)/debian/tmp install; \
	done
	# clean up some upstream cruft
	find $(CURDIR)/debian/tmp -type f -name '*.la' -print0 | xargs -0 rm -f
	rm -rf $(CURDIR)/debian/tmp/usr/lib/pkgconfig

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
#	uncomment when upstream stops shipping an entry changelog...
#	dh_installchangelogs ChangeLog
	dh_installchangelogs
	dh_installdocs
	dh_installexamples
	find debian/tmp -type f -name '*.la' | xargs rm -f
	rm -rf debian/tmp/usr/lib/pkgconfig
	dh_install --sourcedir=debian/tmp --fail-missing
	dh_pysupport
	dh_installman
	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
