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

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

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

PYVERS=$(shell py3versions -vs)

include /usr/share/dpkg/architecture.mk

override_dh_python3:
	dh_python3 --shebang=/usr/bin/python3

override_dh_auto_build: $(PYVERS:%=build-python%)
	$(MAKE) build-chg
	# 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

build-python%:
	$(MAKE) all PYTHON=python$*

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 -- PYTHON=python3 TESTFLAGS="--verbose --timeout 1800 $(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 && python3 run-tests.py --verbose `grep ^test ../debian/mercurial.test_blacklist`
endif

override_dh_auto_install: $(PYVERS:%=install-python%)
	$(MAKE) install-chg DESTDIR=$(CURDIR)/debian/tmp PREFIX=/usr PYTHON=python3
	# #961245: mercurial-git grabbed the hgext/git directory years
	# ago, so don't install our experimental extension there for now
	rm -rf debian/tmp/usr/lib/python*/*-packages/hgext/git

install-python%:
	dh_auto_install -- PREFIX=/usr PYTHON=python$*

execute_after_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
	install -DTm 644 contrib/zsh_completion \
	    debian/mercurial-common/usr/share/zsh/vendor-completions/_mercurial
	install -DTm 644 tests/run-tests.py \
	    debian/mercurial-common/usr/share/mercurial/tests/run-tests.py

override_dh_python3-arch:
	dh_python3
	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 -- PYTHON=python3
	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
