#!/usr/bin/make -f
# -*- makefile -*-

#export DH_VERBOSE = 1

include /usr/share/dpkg/buildflags.mk

DPKG_EXPORT_BUILDFLAGS = 1
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export PYBUILD_NAME=pyinstrument

SPHINXOPTS := -E -N

%:
	dh $@ --buildsystem=pybuild

override_dh_auto_clean:
	dh_auto_clean
	rm -rf .mypy_cache .pytest_cache

override_dh_sphinxdoc:
	cd .pybuild && \
	PYTHONPATH=$$PWD/`dirname $$(find . -type d -name "$(PYBUILD_NAME)*dist-info" | head -n1)` && \
	PATH=$(shell realpath $$(find . -type d -name scripts | head -n1)):$(PATH) && \
	export PYTHONPATH && \
	export PATH && \
	    python3 -m sphinx -b man $(SPHINXOPTS) ../docs $(CURDIR)/debian
ifeq (,$(findstring nodoc, $(DEB_BUILD_OPTIONS)))
	cd .pybuild && \
	PYTHONPATH=$$PWD/`dirname $$(find . -type d -name "$(PYBUILD_NAME)*dist-info" | head -n1)` && \
	PATH=$(shell realpath $$(find . -type d -name scripts | head -n1)):$(PATH) && \
	export PYTHONPATH && \
	export PATH && \
	    python3 -m sphinx -b html $(SPHINXOPTS) ../docs $(CURDIR)/debian/python-$(PYBUILD_NAME)-doc/usr/share/doc/python-$(PYBUILD_NAME)-doc/html
	rm -rf $(CURDIR)/debian/python-$(PYBUILD_NAME)-doc/usr/share/doc/python-$(PYBUILD_NAME)-doc/html/.doctree
	cp docs/img/screenshot.jpg $(CURDIR)/debian/python-$(PYBUILD_NAME)-doc/usr/share/doc/python-$(PYBUILD_NAME)-doc/html/_images
	sed -i -e 's,https:\/\/github.com\/joerick\/pyinstrument\/raw\/main\/docs\/img\/,file:\/\/\/usr\/share\/doc\/python-$(PYBUILD_NAME)-doc\/html\/_images\/,g' \
	    $(CURDIR)/debian/python-$(PYBUILD_NAME)-doc/usr/share/doc/python-$(PYBUILD_NAME)-doc/html/home.html
endif
	dh_sphinxdoc

override_dh_fixperms:
	for FILE in `find $(CURDIR)/debian/python3-$(PYBUILD_NAME)/ -type f -name ".gitignore"`; do \
	    rm -rf $${FILE} ;\
	done
	dh_fixperms
