#!/usr/bin/make -f
SHELL=bash  # needed for the <(echo …) process subsitution temporary file
# tricks with help2man
export DH_VERBOSE=1
export PYBUILD_DESTDIR_python3=debian/python3-mypy
export PYBUILD_NAME=mypy
PPATH=$(CURDIR)
PY3V=$(shell py3versions -dv)

include /usr/share/dpkg/pkg-info.mk
ifeq (,$(filter nodoc,$(DEB_BUILD_PROFILES)))
	WITH += ,sphinxdoc
endif

export DEB_BUILD_MAINT_OPTIONS=hardening=+all
include /usr/share/dpkg/default.mk
ifneq (,$(filter $(DEB_HOST_ARCH),armhf mips64el mipsel ia64 risc64))
  export MYPYC_OPT_LEVEL=2
else ifneq (,$(filter $(DEB_HOST_ARCH),armel alpha m68k powerpc sh4))
  export MYPYC_OPT_LEVEL=1
endif

%:
	dh $@ --with python3$(WITH) --buildsystem=pybuild

ifeq (,$(filter nodoc,$(DEB_BUILD_PROFILES)))
manpages: debian/dmypy.1 debian/mypy.1 debian/stubgen.1
else
manpages:
endif

debian/%.1: debian/%.rst debian/%_options.rst
	rst2man $< > $@

# create an empty file to simplify the makefile logic
debian/dmypy_options.rst:
	touch $@

debian/mypy_options.rst: docs/source/command_line.rst
	sed 's,The .* command line,OPTIONS,g' $< > $@

debian/stubgen_options.rst: docs/source/stubgen.rst
	sed -n -e '/stubgen --help/,$$ {/stubgen --help/d; p}' $< > $@

override_dh_auto_build: manpages
	export MYPY_USE_MYPYC=1; dh_auto_build
ifeq (,$(filter nodoc,$(DEB_BUILD_PROFILES)))
	PYTHONPATH=$(CURDIR) $(MAKE) -C docs html
endif

override_dh_auto_clean:
	dh_auto_clean
ifeq (,$(filter nodoc,$(DEB_BUILD_PROFILES)))
	$(MAKE) -C docs clean
endif

override_dh_auto_install:
	dh_auto_install
	dh_movefiles --package=mypy --sourcedir=debian/python3-mypy usr/bin
	rm -Rf debian/python3-mypy/usr/bin

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_PROFILES)))
	export TEST_MYPYC=1
	PYBUILD_SYSTEM=custom \
		PYBUILD_TEST_ARGS="{interpreter} -m mypy \
		--config-file {dir}/mypy_self_check.ini -p mypy" dh_auto_test
	dh_auto_install
	PATH=$$PATH:$(CURDIR)/debian/mypy/usr/bin/ pytest-3 -n auto \
	     -o testpaths=mypy/test -o python_files=test*.py \
	     -o python_classes= -o python_functions=
endif

.PHONY: manpages
