#!/usr/bin/make -f

# Get the supported 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,sphinxdoc,bash_completion  --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 -Derror selftest -v --parallel=fork
endif

override_dh_auto_clean:
	dh_auto_clean
	rm -rf debian/bzrhome
	rm -f bzrlib/*_pyx.c
	# 'make clean-sphinx' calls Makefiles in sub-directories that are
	# created only after calling 'make docs-sphinx'. Check for them first.
	if [ -f doc/es/Makefile ]; then $(MAKE) clean-sphinx; fi
	# It also doesn't really clean every thing...
	ls doc/en/user-reference/*txt | grep -v readme.txt | xargs rm -rf
	rm -rf doc/developers/make.bat \
		doc/es/make.bat \
		doc/ja/make.bat \
		doc/ru/make.bat \
		doc/developers/Makefile \
		doc/es/Makefile \
		doc/ja/Makefile \
		doc/ru/Makefile \
		doc/*/_build/ \
		doc/en/release-notes/index.txt

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.
	( cd doc && find -name "*.txt" -not -wholename "*/_build/*" -print0 ) | \
		xargs -r0 -i'{}' -n1 install -D -m 644 'doc/{}' \
		"debian/bzr-doc/usr/share/doc/bzr/txt/{}"

override_dh_auto_build:
	dh_auto_build
	PYTHONHASHSEED=0 $(MAKE) docs-sphinx

override_dh_compress:
	dh_compress -X.xvg -X.pdf

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

override_dh_link:
	# symlink identical resources.
	( cd debian/bzr-doc/usr/share/doc && fdupes -o name -r1q . | while read s; do \
	      set -- $$(echo $$s | tr ' ' '\n' | sort); \
	      f=$$1; shift; \
	      for d; do \
		  ln -sf /usr/share/doc/"$$d" "$$f"; \
	      done; \
	done )
	dh_link

override_dh_sphinxdoc:
ifneq "$(shell dh_listpackages | grep -- -doc)" ""
	dh_sphinxdoc -X searchtools.js
endif
	# dh_sphinxdoc chokes if we delete the extra licence files before it runs.
	find debian/bzr-doc/usr/share/doc/bzr -name "licence.txt" -print | xargs rm -f
