#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk
export SETUPTOOLS_SCM_PRETEND_VERSION=${DEB_VERSION_UPSTREAM}

export XDG_RUNTIME_DIR=/tmp

PY3VERS := $(shell py3versions -v -s)
PY3DEF := $(shell py3versions -v -d)
pdata := python-matplotlib-data
p3 := python3-matplotlib

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# https://bugs.debian.org/1000435
# it is due to a bug of gcc-11
ifneq (,$(filter $(DEB_HOST_ARCH), mips64 mips64el mips64r6 mips64r6el))
  DEB_CFLAGS_MAINT_APPEND += -O3
  DEB_CXXFLAGS_MAINT_APPEND += -O3
endif

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

%:
	dh $@ --buildsystem=pybuild --with python3,numpy3,sphinxdoc

execute_before_dh_auto_configure:
	cp -f debian/mplsetup.cfg mplsetup.cfg

ifeq (,$(findstring nodoc,$(DEB_BUILD_PROFILES)))
execute_before_dh_installdocs-indep:
	# build the doc
	MPLCONFIGDIR=. MATPLOTLIBDATA=$(CURDIR)/lib/matplotlib/mpl-data/ PYTHONPATH=$$(pybuild --print build_dir --interpreter python$(PY3DEF)) \
	    HOME=/tmp xvfb-run -a $(MAKE) -C doc html
endif

ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
override_dh_auto_test:
	echo "backend      : TkAgg" > matplotlibrc
	# all the "not" tests are due to https://github.com/matplotlib/matplotlib/issues/24770
	-for v in $(PY3VERS); do \
		BUILDDIR=$$(pybuild --print build_dir --interpreter python$$v) ; \
		PYTHONPATH=$$BUILDDIR MATPLOTLIBDATA=$(CURDIR)/lib/matplotlib/mpl-data/ MPLCONFIGDIR=. xvfb-run -a python$$v -m pytest --verbose --full-trace $$BUILDDIR/matplotlib/tests/ --ignore-glob=*/test_backend_nbagg.py -k 'not test_fig_close and not test_fig_sigint_override and not test_correct_key and not test_device_pixel_ratio_change and not test_subplottool and not test_figureoptions and not test_double_resize and not test_canvas_reinit and not test_form_widget_get_with_datetime_and_date_fields and not test_span_selector_animated_artists_callback';  \
	done
	rm -f matplotlibrc
endif

execute_after_dh_install-indep:
	sed -i '/backend:/s/.*/backend: TkAgg/' debian/python-matplotlib-data/etc/matplotlibrc
	rm -fr $(CURDIR)/debian/$(pdata)/usr/share/matplotlib/mpl-data/fonts/ttf/Vera*.ttf
	rm -fr $(CURDIR)/debian/$(pdata)/usr/share/matplotlib/mpl-data/fonts/ttf/*.TXT
	rm -fr $(CURDIR)/debian/$(pdata)/usr/share/matplotlib/mpl-data/fonts/ttf/local.conf
	rm -fr $(CURDIR)/debian/$(pdata)/usr/share/matplotlib/mpl-data/fonts/pdfcorefonts/readme.txt
	chmod 644 $(CURDIR)/debian/$(pdata)/usr/share/matplotlib/mpl-data/images/*.svg

execute_after_dh_install-arch:
	# make python scripts starting with '#!' executable
	for i in `find debian -mindepth 2 -type f ! -perm 755`; do \
		if head -1 $$i | grep -q '^#!'; then \
			chmod 755 $$i; \
			echo "made executable: $$i"; \
		fi; \
	done
	find debian/ -name '*.pyc' | xargs rm -rf
	find debian/ -name '__pycache__' | xargs rm -rf

	# don't install baseline_images, needed for tests only, but huge
	# leave the baseline_images dir, as matplotlib/tests/__init__.py checks for it
	for v in $(PY3VERS) ; do \
		rm -rf $(CURDIR)/debian/$(p3)/usr/lib/python$$v/dist-packages/matplotlib/tests/baseline_images/* ; \
		mkdir -p $(CURDIR)/debian/$(p3)/usr/lib/python$$v/dist-packages/matplotlib/tests/baseline_images/ ; \
		touch $(CURDIR)/debian/$(p3)/usr/lib/python$$v/dist-packages/matplotlib/tests/baseline_images/.keep ; \
		rm -rf $(CURDIR)/debian/$(p3)/usr/lib/python$$v/dist-packages/matplotlib/mpl-data/ ; \
		rm -rf $(CURDIR)/debian/$(p3)/usr/lib/python$$v/dist-packages/matplotlib/backends/Matplotlib.nib/ ; \
		rm -f $(CURDIR)/debian/$(p3)/usr/lib/python$$v/dist-packages/matplotlib/backends/web_backend/jquery/js/jquery-1.7.1.min.js ; \
		rm -f $(CURDIR)/debian/$(p3)/usr/lib/python$$v/dist-packages/matplotlib/backends/web_backend/jquery/js/jquery-ui.min.js ; \
		rm -f $(CURDIR)/debian/$(p3)/usr/lib/python$$v/dist-packages/matplotlib/backends/web_backend/jquery/css/themes/base/jquery-ui.min.css ; \
	done


override_dh_link-arch:
	# replace minified jquery with packaged version and hope it works
	-for i in $(PY3VERS); do \
	    dh_link -ppython3-matplotlib usr/share/javascript/jquery/jquery.min.js \
	      usr/lib/python3/dist-packages/matplotlib/backends/web_backend/jquery/js/jquery-1.7.1.min.js; \
	    dh_link -ppython3-matplotlib usr/share/javascript/jquery-ui/jquery-ui.min.js \
	      usr/lib/python3/dist-packages/matplotlib/backends/web_backend/jquery/js/jquery-ui.min.js; \
	    dh_link  -ppython3-matplotlib usr/share/javascript/jquery-ui/css/smoothness/jquery-ui.min.css \
	      usr/lib/python3/dist-packages/matplotlib/backends/web_backend/jquery/css/themes/base/jquery-ui.min.css; \
	done
