#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

export PYBUILD_DESTDIR_python3=debian/python3-reportlab/
setup_options = --no-download-t1-files

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

override_dh_auto_build:
	python3 setup.py build $(setup_options)
	set -x; cd docs && PYTHONPATH=$(CURDIR)/build/lib python3 genAll.py
	PYTHONPATH=$(CURDIR)/build/lib $(MAKE) -C docs html PAPER=a4

override_dh_auto_clean:
	rm -rf .pybuild build
	find -name '*.py[co]' -exec rm -f {} \;
	rm -rf *.log *.pdf # test runs ...
	rm -rf docs/build docs/*.pdf
	rm -rf src/reportlab.egg-info
	dh_clean

DOCDIR = debian/python-reportlab-doc/usr/share/doc/python-reportlab-doc
override_dh_auto_install:
	python3 setup.py install \
		$(setup_options) \
		--root $(CURDIR)/debian/python3-reportlab \
		--install-layout=deb
	find debian/python3-reportlab -name '*.ttf' | xargs -r rm -f
	find debian/python3-reportlab -name '*.py[co]' | xargs -r rm -f

	find debian/python3-reportlab -name '__pycache__' | xargs -r rm -rf

	: # remove test, docs and demo files
	rm -rf debian/python3-reportlab/usr/lib/python3/*-packages/reportlab/demos
	rm -rf debian/python3-reportlab/usr/lib/python3/*-packages/reportlab/tools/pythonpoint/demos
	rm -rf debian/python3-reportlab/usr/lib/python3/*-packages/reportlab/docs
	rm -rf debian/python3-reportlab/usr/lib/python3/*-packages/reportlab/test
	rm -rf debian/python3-reportlab/usr/lib/python3/*-packages/reportlab/fonts

	rm -f debian/python3-reportlab/usr/lib/python3/*-packages/reportlab/README.txt
	rm -f debian/python3-reportlab/usr/lib/python3/*-packages/reportlab/CHANGES.txt

	: # Replace all '#!' calls to python with python3
	: # and make them executable
	for i in `find debian/python3-reportlab -type f`; do \
	  sed '1s,#!.*python[^ ]*\(.*\),#! /usr/bin/python3\1,' \
		$$i > $$i.temp; \
	  if cmp --quiet $$i $$i.temp; then \
	    rm -f $$i.temp; \
	  else \
	    mv -f $$i.temp $$i; \
	    chmod 755 $$i; \
	    echo "fixed interpreter: $$i"; \
	  fi; \
	done
	dh_installdocs -i README.txt
	dh_installdocs -ppython-reportlab-doc -X.buildinfo docs/*.pdf docs/build/html
	dh_sphinxdoc -i
	dh_installexamples -ppython-reportlab-doc demos/*
	rm -rf debian/python-reportlab-doc/usr/share/doc/python-reportlab-doc/examples/tests
	if [ -d $(DOCDIR)/examples ]; then \
	 cp -p debian/testdemos.py $(DOCDIR)/examples/. ;\
	 cp -rp tools/pythonpoint/demos $(DOCDIR)/pythonpoint-demos; \
	fi
	: # Replace all '#!' calls to python with python3
	: # and make them executable
	for i in `find debian/python-reportlab-doc -mindepth 3 -type f`; do \
	  sed '1s,#!.*python[^ ]*\(.*\),#! /usr/bin/python3\1,' \
		$$i > $$i.temp; \
	  if cmp --quiet $$i $$i.temp; then \
	    rm -f $$i.temp; \
	  else \
	    mv -f $$i.temp $$i; \
	    chmod 755 $$i; \
	    echo "fixed interpreter: $$i"; \
	  fi; \
	done

override_dh_auto_test:
	@echo "not yet running the tests"
