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

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

%:
	dh $@ --with python2,bash-completion

export PYTHON=python2
PYVERS=$(shell pyversions -vs)
PYVER_DEFAULT=$(shell pyversions -vd)
DEB_HOST_ARCH := $(shell dpkg-architecture -qDEB_HOST_ARCH)
ifeq ($(DEB_HOST_ARCH),mips64el)
        # Work around #871514
        export DEB_CFLAGS_MAINT_APPEND = -mno-lra
endif

override_dh_auto_build:
	$(MAKE) all
	$(MAKE) -C contrib/chg all
	# 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:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
# 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

	http_proxy='' dh_auto_test -- TESTFLAGS="--verbose --timeout 1440 $(PARALLEL_TEST_JOBS) --blacklist $(CURDIR)/debian/mercurial.test_blacklist"
	file-rename 's/\.deb-backup$$//' $(CURDIR)/tests/*
	# run blacklisted tests but ignore their results
	-cd tests && python2 run-tests.py --verbose `grep ^test ../debian/mercurial.test_blacklist`
endif

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

install-python%:
	python$* setup.py install --root $(CURDIR)/debian/tmp --install-layout=deb

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:
	# 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_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/

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

