#!/usr/bin/make -f

PYX_FILES = $(wildcard djvu/*.pyx)
C_FILES = $(PYX_FILES:.pyx=.c)

nosetests = /usr/bin/nosetests --with-doctest --verbose
tests = decode.py sexpr.py const.py

pythonpath = $$(_py_=$(strip $(1)); _py_=$${_py_\#python}; ls -d $(CURDIR)/build/lib.*-$${_py_})
pythonpath_dbg = $$(_py_=$(strip $(1)); _py_=$${_py_\#python}; ls -d $(CURDIR)/build/lib_d.*-$${_py_} 2>/dev/null || ls -d $(CURDIR)/build/lib.*-$${_py_}-pydebug)

.PHONY: override_dh_auto_build
override_dh_auto_build: build/stamp
	
build/stamp: $(C_FILES)
	dh_auto_build
	touch $(@)

%.c: %.pyx
	cython $(<)

.PHONY: override_dh_installdocs
ifneq ($(shell dh_listpackages | grep -x -F python-djvu-doc),)
override_dh_installdocs: doc/stamp
endif
override_dh_installdocs:
	dh_installdocs -X.buildinfo -Xobjects.inv

doc/stamp: build/stamp
	$(MAKE) -C doc/ html PYTHONPATH=$(call pythonpath,$(shell pyversions -d)):$(CURDIR)
	touch $(@)

.PHONY: override_dh_auto_clean
override_dh_auto_clean: 
	rm -Rf doc/build/ doc/stamp
	rm -Rf build/
	find . -type f \( -name '*.py[co]' -or -name '*.so' -or -name '*.c' \) -delete
	rm -Rf *.egg-info/

.PHONY: override_dh_strip
override_dh_strip:
ifeq (,$(filter nostrip,$(DEB_BUILD_OPTIONS)))
	dh_strip --dbg-package=python-djvu-dbg
	cd debian/*-dbg/usr/lib/debug/usr/lib && mv pyshared pymodules
endif

.PHONY: override_dh_auto_test
override_dh_auto_test:
ifeq ($(filter nocheck,$(DEB_BUILD_OPTIONS)),)
	cd tests && \
	set -x -e; \
	for python in $(shell pyversions -r); do \
	    PYTHONPATH=$(call pythonpath,$$python) $$python $(nosetests) $(tests); \
	    PYTHONPATH=$(call pythonpath_dbg,$$python) $$python-dbg $(nosetests) $(tests); \
	done
endif

.PHONY: build build-arch build-indep binary binary-arch binary-indep clean
build build-arch build-indep binary binary-arch binary-indep clean:
	dh $(@)

# vim:ts=4 sw=4 noet
