#!/usr/bin/make -f

include /usr/share/dpkg/buildopts.mk
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
export PYBUILD_BUILD_ARGS := -Csetup-args=-Dsystem-freetype=true -Csetup-args=-Dsystem-qhull=true
export PYBUILD_BEFORE_TEST=cp -a {dir}/lib/matplotlib/tests/baseline_images {build_dir}/matplotlib/tests/ && \
			cp -a {dir}/lib/matplotlib/tests/tinypages {build_dir}/matplotlib/tests/ && \
			cp -a {dir}/lib/matplotlib/tests/mpltest.ttf {build_dir}/matplotlib/tests/ && \
			cp -a {dir}/lib/mpl_toolkits/mplot3d/tests/baseline_images {build_dir}/mpl_toolkits/mplot3d/tests/ && \
			cp -a {dir}/lib/mpl_toolkits/axes_grid1/tests/baseline_images {build_dir}/mpl_toolkits/axes_grid1/tests/ && \
			cp -a {dir}/lib/mpl_toolkits/axisartist/tests/baseline_images {build_dir}/mpl_toolkits/axisartist/tests/

# 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

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-indep:

execute_before_dh_auto_test-arch:
	echo "backend      : TkAgg" > matplotlibrc

execute_after_dh_auto_test-arch:
	rm matplotlibrc

# the "not" tests are due to https://github.com/matplotlib/matplotlib/issues/24770
# and 2025-01-29 attempt fix FTBFS as part of numpy2/python3.13 transitions
override_dh_auto_test-arch: export PYBUILD_TEST_ARGS=-n $(DEB_BUILD_OPTION_PARALLEL) -v --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 and not test_normal_axes and not test_rcupdate and not test_unicode_won and not test_lazy_auto_backend_selection and not test_webagg and not test_bbox_inches_tight_suptile_legend[png] and not test_bbox_inches_tight_suptitle_non_default[png] and not test_hidden_axes and not test_compressed1 and not test_auto_date_locator_intmult_tz and not test_DateLocator and not test_figure_legend_outside and not test_warn_big_data_best_loc and not test_tmpconfigdir_warning and not test_importable_with_no_home and not test_get_tightbbox_polar and not test_backend_fallback_headful and not test_outward_ticks and not test_image_grid_single_bottom[png] and not test_sphinx_gallery_example_header and not test_interactive_backend and not test_interactive_thread_safety and not test_blitting_events and not test_sigint and not test_other_signal_before_sigint and not test_interactive_timers and not test_savefig_to_stringio and not test_psd_csd and not test_overprecision and not test_Type1Font_2 and not test_Type1Font and not test_ft2font_get_kerning and not test_ipython and not test_lazy_linux_headless and not test_determinism_check'
override_dh_auto_test-arch: export MATPLOTLIBDATA=$(CURDIR)/lib/matplotlib/mpl-data/
override_dh_auto_test-arch: export MPLCONFIGDIR=$(CURDIR)
override_dh_auto_test-arch:
	-dh_auto_test
endif

execute_after_dh_install-indep:
	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:
	# scrub some test output that should not be in the module
	rm -rf $(CURDIR)/debian/python3-matplotlib/usr/lib/python*/dist-packages/result_images/
	# 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' -delete
	find debian/ -name '__pycache__' -exec rm -rf {} +
	find debian/ -name '.eslintrc.js' -delete
	find debian/ -name '.gitignore' -delete

	# FIXME: is it possible to omit baseline_images and bring it in only
	# for CI tests
	rm -rf $(CURDIR)/debian/$(p3)/usr/lib/python*/dist-packages/matplotlib/mpl-data/
	rm -rf $(CURDIR)/debian/$(p3)/usr/lib/python*/dist-packages/matplotlib/backends/Matplotlib.nib/
	rm -f $(CURDIR)/debian/$(p3)/usr/lib/python*/dist-packages/matplotlib/backends/web_backend/jquery/js/jquery-1.7.1.min.js
	rm -f $(CURDIR)/debian/$(p3)/usr/lib/python*/dist-packages/matplotlib/backends/web_backend/jquery/js/jquery-ui.min.js
	rm -f $(CURDIR)/debian/$(p3)/usr/lib/python*/dist-packages/matplotlib/backends/web_backend/jquery/css/themes/base/jquery-ui.min.css

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