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

# avoid scratchbox python
PATH :=/usr/bin:$(PATH)
export PATH
SBOX_REDIRECT_IGNORE = /usr/bin/python
export SBOX_REDIRECT_IGNORE

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

PYVERS	:= $(shell pyversions -vs)
PYVER   := $(shell pyversions -vd)

UPSTREAMVER := $(subst lxml-,,$(notdir $(CURDIR)))

DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

include /usr/share/python/python.mk

prebuild: prebuild-stamp
prebuild-stamp: src/lxml/lxml.etree.pyx src/lxml/lxml.objectify.pyx
#	cython src/lxml/lxml.etree.pyx src/lxml/lxml.objectify.pyx
	touch $@

build: build-stamp
build-stamp: $(PYVERS:%=build-python%) $(PYVERS:%=dbg-build-python%)
	touch $@
build-python%: prebuild
	python$* setup.py build
	touch $@
dbg-build-python%: prebuild
	python$*-dbg setup.py build
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp build-python* dbg-build-python* prebuild-stamp
	rm -rf build dist
#	rm -f src/lxml/lxml.etree_api.h src/lxml/lxml.etree.c \
#	      src/lxml/lxml.etree.h src/lxml/lxml.objectify.c

	-find -name '*.py[co]' | xargs rm -f
	rm -rf [23].[0-9]
	rm -f src/lxml/lxml-version.h
	dh_clean 

install: build install-prereq $(PYVERS:%=install-python%) $(PYVERS:%=dbg-install-python%)
	-find debian -name '*.py[co]' | xargs rm -f

install-prereq:
	dh_testdir
	dh_testroot
	dh_clean -k

install-python%:
	mkdir -p debian/python-lxml/usr/bin
	mkdir -p debian/python-lxml/$(call py_libdir, $*)
	python$* setup.py install \
		--root=$(CURDIR)/debian/python-lxml

dbg-install-python%:
	mkdir -p debian/python-lxml/usr/bin
	mkdir -p debian/python-lxml-dbg/$(call py_libdir, $*)
	python$*-dbg setup.py install \
		--root=$(CURDIR)/debian/python-lxml-dbg
	find debian/python-*-dbg ! -type d ! -name '*.so' | xargs rm -f
	find debian/python-*-dbg -depth -empty -exec rmdir {} \;

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs -i
	dh_installexamples -i samples/*
	dh_compress -i -X.xml -X.py \
	    -X.html -X.css -X.asc -X.png -Xapi-objects.txt
	dh_fixperms -i
	dh_installdeb -i
	dh_shlibdeps -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs -a CHANGES.txt
	dh_installdocs -a CREDITS.txt README.txt TODO.txt
	dh_installexamples -a samples/*
	dh_strip -ppython-lxml --dbg-package=python-lxml-dbg
	rm -rf debian/python-lxml-dbg/usr/share/doc/python-lxml-dbg
	ln -s python-lxml debian/python-lxml-dbg/usr/share/doc/python-lxml-dbg
	dh_compress -a -X.xml -X.py \
	    -X.html -X.css -X.asc -X.png -Xapi-objects.txt
	dh_fixperms -a
	dh_pycentral -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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