#!/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

ARCH_IS_MIPS := $(if $(subst mips,,$(DEB_HOST_ARCH)),,T)
ARCH_IS_KFREEBSD-i386 := $(if $(subst kfreebsd-i386,,$(DEB_HOST_ARCH)),,T)
ARCH_IS_ARMEL := $(if $(subst armel,,$(DEB_HOST_ARCH)),,T)
ARCH_IS_SPARC := $(if $(subst sparc,,$(DEB_HOST_ARCH)),,T)

override_dh_auto_test:
# test-pull-pull-corruption tries to execute two hg commands at the
# same time to test for corruption.  It does so by running an hg
# command in the background, sleeping one second and running the
# second command.  In architectures with slow buildds, 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 architectures.
ifneq ($(or $(ARCH_IS_MIPS), $(ARCH_IS_KFREEBSD-i386), $(ARCH_IS_ARMEL), $(ARCH_IS_SPARC)),)
		sed -i.deb-backup -e 's/sleep 1/sleep 2/' $(CURDIR)/tests/test-pull-pull-corruption.t
endif

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

	rename.ul .deb-backup '' $(CURDIR)/tests/*

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) -f tests/gpg/random_seed
	$(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

