#!/usr/bin/make -f
export DH_VERBOSE=1
export PYBUILD_DESTDIR_python3=debian/python3-mypy
export PYBUILD_NAME=mypy
PY3VERS=$(shell py3versions -vr)

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

export DEB_BUILD_MAINT_OPTIONS=hardening=+all
include /usr/share/dpkg/default.mk

ifneq (,$(filter $(DEB_BUILD_ARCH),armel armhf hppa riscv64 mipsel alpha ia64 m68k powerpc sh4 sparc64))
  export MYPYC_MULTI_FILE=1
endif
ifneq (,$(filter $(DEB_HOST_ARCH),mips64el))
  export DEB_CFLAGS_MAINT_APPEND += -mxgot
endif

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

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

debian/%.1: debian/sphinx/%.rst debian/sphinx/%_options.rst
	sphinx-build -N -b man debian/sphinx debian $<

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

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

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

override_dh_auto_build-arch:
	MYPY_USE_MYPYC=1 dh_auto_build

override_dh_auto_build-indep: manpages
	MYPY_USE_MYPYC=0 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
	rm -rf debian/.doctrees
	rm -f  debian/sphinx/dmypy_options.rst
	rm -f  debian/sphinx/mypy_options.rst
	rm -f  debian/sphinx/stubgen_options.rst
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-arch:
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
	set -e; for v in $(PY3VERS); do \
		PYTHONPATH=$$(pybuild --print build_dir --pyver $$v | awk '{ print $$3 }') PATH=$$PATH:$(CURDIR)/debian/python3-mypy/usr/bin/ python$$v -m pytest -n auto \
		-o testpaths="mypy/test mypyc/test" -o python_files=test*.py -k 'not (testCustomTypeshedDirWithRelativePathDoesNotCrash or testNoPython3StubAvailable or testIgnoreImportIfNoPython3StubAvailable or PEP561Suite)' \
		-o python_classes= -o python_functions= ; \
	done
	# testIgnoreImportIfNoPython3StubAvailable fails due to scribe not being installed (it isn't packaged for Debian)
	# testNoPython3StubAvailable no longer work because python3-typeshed includes docutils types, which it isn't expecting
	# PEP561Suite due to needing internet access to pypi.org
endif

override_dh_auto_test-indep:
	echo No tests to run for the "mypy" package, only for "python3-mypy"

.PHONY: manpages
