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

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

include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/buildopts.mk

ifeq ($(PYBUILD_AUTOPKGTEST),1)
    HG=/usr/bin/hg
else
    HG={build_dir}/../scripts/hg
endif

common_test_flags = \
    --with-hg=${HG} \
    --verbose \
    --timeout 1800 \
    $(DEB_BUILD_OPTION_PARALLEL:%=--jobs %)
test_flags = \
    $(common_test_flags) \
    --blacklist $(CURDIR)/debian/mercurial.test_blacklist
ifeq ($(DEB_HOST_ARCH), riscv64)
test_flags += \
    --blacklist $(CURDIR)/debian/mercurial.test_blacklist_riscv64
endif

export http_proxy=
export PYBUILD_TEST_CUSTOM=1
export PYBUILD_TEST_ARGS=$(MAKE) tests PYTHON={interpreter} TESTFLAGS="$(test_flags)" PYTHONPATH={build_dir} && cd {dir}/tests && (PYTHONPATH={build_dir} {interpreter} run-tests.py $(common_test_flags) `grep ^test ../debian/mercurial.test_blacklist` || /usr/bin/true)

build:
	dh $@ --with bash-completion --buildsystem=pybuild
%:
	dh $@ --with bash-completion --buildsystem=pybuild

.PHONY: build

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

execute_after_dh_auto_build:
	$(MAKE) build-chg

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

execute_after_dh_python3-arch:
	find debian/mercurial/usr/share -type d -empty -delete ; \

execute_after_dh_auto_clean: mercurial/__version__.py
	$(MAKE) clean
	$(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
