#!/usr/bin/make -f

# Get the suppoirted Python versions
PYVERS = $(shell pyversions -r -v)
# Get the default Python version
PYVERSION = $(shell pyversions -d -v)

# Callable functions to determine the correct PYTHONPATH
pythonpath = $$(ls -d $(CURDIR)/build/lib.*-$(1))
pythonpath_dbg = $$(ls -d $(CURDIR)/build/lib_d.*-$(1) 2>/dev/null || ls -d $(CURDIR)/build/lib.*$(1)-pydebug)

%:
	dh --with python2 --buildsystem=python_distutils $*

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
CONCURRENCY = BZR_CONCURRENCY=$(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	[ -d debian/bzrhome ] || mkdir debian/bzrhome
	$(CONCURRENCY) \
	BZR_HOME=debian/bzrhome \
	BZR_PLUGIN_PATH=-site:-user \
	BZR_DISABLE_PLUGINS=launchpad \
	PYTHONPATH=$(wildcard $(CURDIR)/build/lib.*-$(PYVERSION)) \
	$(CURDIR)/build/scripts-$(PYVERSION)/bzr selftest -v --parallel=fork
endif

override_dh_auto_clean:
	dh_auto_clean
	$(MAKE) clean-docs
	rm -rf debian/bzrhome
	rm -f bzrlib/*_pyx.c

override_dh_install:
	find debian/tmp/ -name "*.pyc" | xargs rm -f
	find debian/tmp/ -name "*.pyo" | xargs rm -f
	mkdir -p debian/python-bzrlib.tests/usr/lib \
		debian/python-bzrlib.tests/usr/share/pyshared/bzrlib
	for py in $(shell pyversions -r); do \
		mkdir -p debian/python-bzrlib.tests/usr/lib/$$py/dist-packages/bzrlib; \
		mv debian/tmp/usr/lib/$$py/dist-packages/bzrlib/tests \
			debian/python-bzrlib.tests/usr/lib/$$py/dist-packages/bzrlib/tests; \
	done
	if [ -d debian/tmp/usr/share/locale ]; then \
		mkdir -p debian/python-bzrlib/usr/share; \
		mv debian/tmp/usr/share/locale debian/python-bzrlib/usr/share/locale; \
	fi;
	dh_install --list-missing --fail-missing
	# Install the documentation; since html and txt and intermixed
	# under doc/, this is handier than trying to do it from bzr-doc.install. 
	for ext in txt html; do \
	    ( cd doc && find -name "*.$$ext" -print0 ) | \
	    	xargs -r0 -i'{}' -n1 install -D -m 644 'doc/{}' \
	    	"debian/bzr-doc/usr/share/doc/bzr/$$ext/{}"; \
	done
	rm debian/bzr-doc/usr/share/doc/bzr/txt/*/*/licence.txt
	# Install images as well
	( cd doc && find -name "*.png" -print0 ) | \
	    xargs -r0 -i'{}' -n1 install -D -m 644 'doc/{}' \
	    "debian/bzr-doc/usr/share/doc/bzr/html/{}"

override_dh_auto_build:
	dh_auto_build
	$(MAKE) docs

override_dh_compress:
	dh_compress -X.xvg -X.pdf

override_dh_strip:
	dh_strip -ppython-bzrlib --dbg-package=python-bzrlib-dbg
