#!/usr/bin/make -f

export XDG_RUNTIME_DIR=/tmp

PY3VERS := $(shell py3versions -v -r debian/control)
DEFPY3 := $(shell py3versions -v -d)
PY_PLATFORM := $(shell python3 -c 'from distutils.util import get_platform; print(get_platform())')
p3 := python3-matplotlib
pdata := python-matplotlib-data
DEB_BUILD_OPTIONS += --no-parallel

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

# build
build: build-arch build-indep

build-indep: build-indep-stamp
build-indep-stamp: build-arch-stamp
	dh_testdir

	touch $@

build-arch: build-arch-stamp
build-arch-stamp:
	dh_testdir

	cp -f debian/mplsetup.cfg mplsetup.cfg

	set -e; \
	for v in $(PY3VERS); do \
		python$$v ./setup.py build $(PY_BUILD_FLAGS) ; \
	done

ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	echo "backend      : TkAgg" > matplotlibrc
	# all the "not" tests are due to https://github.com/matplotlib/matplotlib/issues/24770
	-for v in $(PY3VERS); do \
		LIB=$$(python$$v -c "from distutils.command.build import build ; from distutils.core import Distribution ; b = build(Distribution()) ; b.finalize_options() ; print(b.build_platlib)") ; \
		PYTHONPATH=$$LIB MATPLOTLIBDATA=$(CURDIR)/lib/matplotlib/mpl-data/ MPLCONFIGDIR=. xvfb-run -a python$$v -m pytest --verbose --full-trace $$LIB/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

	touch $@


# clean
clean:
	dh_testdir
	dh_testroot
	
	for i in $(PY3VERS); do \
		python$$i ./setup.py clean --all; \
	done

	# clean the doc

	# workaround for cleaning the doc ("doc/make.py clean" only runs useless svn-clean)
	rm -f doc/api/_as_gen/*
	#rm -rf doc/build doc/examples
	#rm -f doc/mpl_examples/api/*.png
	#rm -f doc/mpl_examples/pylab_examples/*.png
	#rm -f doc/mpl_examples/pylab_examples/*.pdf
	#rm -f doc/mpl_examples/units/*.png
	#rm -f doc/pyplots/tex_demo.png
	#rm -f doc/_static/matplotlibrc
	#rm -f doc/_templates/gallery.html

	find . -name '*.pyc' -exec rm {} \;

	#dh_clean lib/matplotlib/mpl-data/matplotlib.conf \
	#	lib/matplotlib/mpl-data/matplotlibrc setup.cfg
	rm -rf build
	rm -rf lib/matplotlib.egg-info

	# restore "pristine" doc/matplotlibrc
	sed -i '/^examples\./d' $(CURDIR)/doc/matplotlibrc

	## remove doc symlinks to examples directory
	#rm -rf doc/mpl_examples
	#rm -rf doc/mpl_toolkits/axes_grid/examples

	# build twice in a row
	rm -rf doc/tex.cache/ fontList.py3k.cache result_images/ test_cache/ tex.cache/ examples/pylab_examples/test.xls
	rm -rf doc/finance.cache/ doc/fontList.cache fontList.cache

	# remove embedded CXX to prevent it to be picked up
	rm -rf extern/CXX/

	# remove auto-generated version file
	rm -f lib/matplotlib/_version.py
	rm -rf .eggs/

# install
install-arch: build-arch install-arch-stamp
	dh_testdir
	dh_testroot
	dh_numpy3

install-arch-stamp: build-arch-stamp
	dh_testdir
	dh_testroot

	set -e; \
	for v in $(PY3VERS) ; do \
		python$$v ./setup.py install_lib --install-dir $(CURDIR)/debian/$(p3)/usr/lib/python3/dist-packages/ ; \
		python$$v ./setup.py install_egg_info --install-dir $(CURDIR)/debian/$(p3)/usr/lib/python3/dist-packages/ ; \
	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 dir tho, as matplotlib/tests/__init__.py checks for it
	for v in $(PY3VERS) ; do \
		rm -rf $(CURDIR)/debian/$(p3)/usr/lib/python3/dist-packages/matplotlib/tests/baseline_images/* ; \
		mkdir -p $(CURDIR)/debian/$(p3)/usr/lib/python3/dist-packages/matplotlib/tests/baseline_images/ ; \
		touch $(CURDIR)/debian/$(p3)/usr/lib/python3/dist-packages/matplotlib/tests/baseline_images/.keep ; \
	done

	rm -rf $(CURDIR)/debian/$(p3)/usr/lib/python3/dist-packages/matplotlib/mpl-data/
	rm -rf $(CURDIR)/debian/$(p3)/usr/lib/python3/dist-packages/matplotlib/backends/Matplotlib.nib/

	-rm $(CURDIR)/debian/$(p3)/usr/lib/python3/dist-packages/matplotlib/backends/web_backend/jquery/js/jquery-1.7.1.min.js
	-rm $(CURDIR)/debian/$(p3)/usr/lib/python3/dist-packages/matplotlib/backends/web_backend/jquery/js/jquery-ui.min.js
	-rm $(CURDIR)/debian/$(p3)/usr/lib/python3/dist-packages/matplotlib/backends/web_backend/jquery/css/themes/base/jquery-ui.min.css

	# 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

	touch $@

install-indep: build-indep
	dh_testdir -i
	dh_testroot -i
	dh_prep
	dh_install -p $(pdata)
	# set the default backend
	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

# binary
binary-indep: build-indep install-indep
	dh_testdir -i
	dh_testroot -i
	dh_installchangelogs
	dh_installdocs -p$(pdata)
	# remove pyc file from doc
	dh_compress -i -Xexamples -Xexamples/data -Xpdf -X.js -X.py -Xobjects.inv -X.html
	dh_link -i
	dh_lintian -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: build-arch install-arch
	dh_testdir -a
	dh_testroot -a
	dh_installchangelogs
	dh_installdocs -p$(p3) -a README.rst
	# 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
	dh_python3 -a
	dh_link -a
	dh_lintian -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install
