#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-

%:
	dh $@ --with python2

PYVERS=$(shell pyversions -vs)
DEB_HOST_ARCH := $(shell dpkg-architecture -qDEB_HOST_ARCH)

override_dh_auto_build: $(PYVERS:%=build-python%)
	$(MAKE) -C doc man
	# Do not start a line with a word with a dot in a manpage
	sed -i -e 's,^[.]\(hgignore\|hg/hgrc\),\\fP\1,' doc/hg.1

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
         NJOBS := $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
         PARALLEL_TEST_JOBS := --jobs $(NJOBS)
endif

override_dh_auto_test:
# Some tests try to execute two hg commands at the same time
# to test for corruption.  They do so by running an hg command
# in the background, sleeping one second and running the
# second command.  In slow architectures, one second is not
# enough and the second test starts before the first one has
# started printing.  This is hacky and we just enhance the
# hack by incrementing the sleep time in those slow
# architectures.
ifeq ($(DEB_HOST_ARCH),mips)
		sed -i -e 's/sleep 1/sleep 2/' $(CURDIR)/tests/test-pull-pull-corruption.t $(CURDIR)/tests/test-pull-pull-corruption2.t
endif

	dh_auto_test -- TESTFLAGS="--timeout 480 $(PARALLEL_TEST_JOBS) --blacklist $(CURDIR)/debian/mercurial.test_blacklist"

build-python%: mercurial/__version__.py
	python$* setup.py build
	touch $@

override_dh_auto_install: $(PYVERS:%=install-python%)

install-python%: build-python%
	python$* setup.py install --root $(CURDIR)/debian/tmp --install-layout=deb
	# Do not hardcode the python interpreter
	sed -i '1c#!/usr/bin/python' debian/tmp/usr/bin/hg


override_dh_install:
	dh_install
	if test -d $(CURDIR)/debian/mercurial ; then \
		$(MAKE) -f debian/rules install-archdep ; \
	fi
	if test -d $(CURDIR)/debian/mercurial-common ; then \
		$(MAKE) -f debian/rules install-archindep ;\
	fi

install-archdep:
	# Install bash autocompletion.
	install -m 644 \
		contrib/bash_completion \
		$(CURDIR)/debian/mercurial/etc/bash_completion.d/mercurial
	# Install system-wide conffiles
	echo "# system-wide mercurial configuration file" \
		>  $(CURDIR)/debian/mercurial/etc/mercurial/hgrc
	echo "# See hgrc(5) for more information" \
		>>  $(CURDIR)/debian/mercurial/etc/mercurial/hgrc
	install -m 644 \
		contrib/mergetools.hgrc \
		$(CURDIR)/debian/mercurial/etc/mercurial/hgrc.d/mergetools.rc
	install -m 644 \
		debian/cacerts.hgrc \
		$(CURDIR)/debian/mercurial/etc/mercurial/hgrc.d/cacerts.rc
	# remove arch-independent python stuff
	find debian/mercurial/usr/lib \
		! -name '*.so' ! -type d -delete , \
		-type d -empty -delete

install-archindep:
	# remove arch-dependent python stuff
	find debian/mercurial-common/usr/lib \
		-name '*.so' ! -type d -delete , \
		-type d -empty -delete

override_dh_python2:
	dh_python2
	find debian/mercurial/usr/share -type d -empty -delete

override_dh_auto_clean: mercurial/__version__.py
	cp -a mercurial/__version__.py mercurial/__version__.py.save
	dh_auto_clean
	mv mercurial/__version__.py.save mercurial/__version__.py
	$(RM) -rv tmp/
	$(RM) build-python*

mercurial/__version__.py:
	@echo "$@ is missing (you probably call 'make clean' directly)."
	@echo "Restore it from sources before building the package"
	@echo "Aborting."
	exit 1

