#!/usr/bin/make -f

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

# all versions
PYVERS	:= $(shell pyversions -vs)
VER	:= $(shell /usr/bin/python -c 'import sys; print sys.version[:3]')

PY3VERS	:= $(shell py3versions -vs)
VER3	:= $(shell /usr/bin/python3 -c 'import sys; print(sys.version[:3])')

PY_INTERPRETER = /usr/bin/python

include /usr/share/python/python.mk

setup_options = --no-download-t1-files

build-arch: build
build-indep: build
build: build-stamp
build-stamp: $(PYVERS:%=build-python%) $(PYVERS:%=dbg-build-python%) \
  $(PY3VERS:%=build3-python%) $(PY3VERS:%=dbg-build3-python%)
	touch $@
build-python%:
	python$* setup.py build $(setup_options)
	touch $@
dbg-build-python%:
	python$*-dbg setup.py build $(setup_options)
	touch $@
build3-python%:
	python$* setup.py build $(setup_options)
	touch $@
dbg-build3-python%:
	python$*-dbg setup.py build $(setup_options)
	touch $@

# needs installed python-imaging, python-renderpm
build-doc-stamp:
	set -x; \
	cd docs \
	  && PYTHONPATH=$(wildcard $(CURDIR)/build/lib.*-*-$(VER3)) python3 genAll.py
	$(MAKE) -C docs html PAPER=a4
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f *-stamp build-python* dbg-build-python* build3-python* dbg-build3-python*
	rm -rf compile build debian/tmp-dbg
	find -name '*.py[co]' -exec rm -f {} \;
	rm -rf *.log *.pdf # test runs ...
	rm -rf docs/build docs/*.pdf
	dh_clean

install: build-stamp install-prereq \
  $(PYVERS:%=install-python%) $(PYVERS:%=install-dbg-python%) \
  $(PY3VERS:%=install3-python%) $(PY3VERS:%=install3-dbg-python%)

install-prereq: build-stamp
	dh_testdir
	dh_testroot
	dh_prep

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

	mkdir -p debian/python-renderpm/$(call py_libdir,$*)/reportlab/graphics
	mv debian/python-reportlab/$(call py_libdir,$*)/reportlab/graphics/_renderPM.so \
		debian/python-renderpm/$(call py_libdir,$*)/reportlab/graphics/

	mkdir -p debian/python-reportlab-accel/$(call py_libdir,$*)/reportlab/lib
	mv debian/python-reportlab/$(call py_libdir,$*)/reportlab/lib/*.so \
		debian/python-reportlab-accel/$(call py_libdir,$*)/reportlab/lib/

	install -d debian/python-reportlab-doc/usr/share/doc/python-reportlab

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

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

#	-find debian -name '*.so' | xargs rm -f

	: # Replace all '#!' calls to python with $(PY_INTERPRETER)
	: # and make them executable
	for i in `find debian/python-reportlab -type f`; do \
	  sed '1s,#!.*python[^ ]*\(.*\),#! $(PY_INTERPRETER)\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

install-dbg-python%: install-prereq
	python$*-dbg setup.py install \
		$(setup_options) \
		--root $(CURDIR)/debian/python-reportlab-accel-dbg \
		--install-layout=deb
	find debian/python-reportlab-accel-dbg ! -type d ! -name '*_d.so' | xargs rm -f
	find debian/python-reportlab-accel-dbg -depth -empty -exec rmdir {} \;
	mkdir -p debian/python-renderpm-dbg/$(call py_libdir,$*)/reportlab/graphics
	mv debian/python-reportlab-accel-dbg/$(call py_libdir,$*)/reportlab/graphics/_renderPM_d.so \
		debian/python-renderpm-dbg/$(call py_libdir,$*)/reportlab/graphics

install3-python%: install-prereq
	python$* 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

	mkdir -p debian/python3-renderpm/$(call py_libdir,$*)/reportlab/graphics
	abitag=.$$(python$* -c "import sysconfig; print(sysconfig.get_config_var('SOABI'))"); \
	mv debian/python3-reportlab/$(call py_libdir,$*)/reportlab/graphics/_renderPM$$abitag*.so \
		debian/python3-renderpm/$(call py_libdir,$*)/reportlab/graphics/

	mkdir -p debian/python3-reportlab-accel/$(call py_libdir,$*)/reportlab/lib
	abitag=.$$(python$* -c "import sysconfig; print(sysconfig.get_config_var('SOABI'))"); \
	mv debian/python3-reportlab/$(call py_libdir,$*)/reportlab/lib/*$$abitag*.so \
		debian/python3-reportlab-accel/$(call py_libdir,$*)/reportlab/lib/

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

#	-find debian -name '*.so' | xargs rm -f

	: # Replace all '#!' calls to python with $(PY_INTERPRETER)3
	: # and make them executable
	for i in `find debian/python3-reportlab -type f`; do \
	  sed '1s,#!.*python[^ ]*\(.*\),#! $(PY_INTERPRETER)3\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

install3-dbg-python%: install-prereq
	python$*-dbg setup.py install \
		$(setup_options) \
		--root $(CURDIR)/debian/python3-reportlab-accel-dbg \
		--install-layout=deb
	find debian/python3-reportlab-accel-dbg ! -type d ! -name '*3?dm-*.so' | xargs rm -f
	find debian/python3-reportlab -name '__pycache__' | xargs -r rm -rf
	find debian/python3-reportlab-accel-dbg -depth -empty -exec rmdir {} \;
	mkdir -p debian/python3-renderpm-dbg/$(call py_libdir,$*)/reportlab/graphics
	abitag=.$$(python$*-dbg -c "import sysconfig; print(sysconfig.get_config_var('SOABI'))"); \
	mv debian/python3-reportlab-accel-dbg/$(call py_libdir,$*)/reportlab/graphics/_renderPM$$abitag*.so \
		debian/python3-renderpm-dbg/$(call py_libdir,$*)/reportlab/graphics

# Build architecture-independent files here.
# Pass -i to all debhelper commands in this target to reduce clutter.
binary-indep: build build-doc-stamp install
	dh_testdir
	dh_testroot
	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
	cp -p debian/testdemos.py debian/python-reportlab-doc/usr/share/doc/python-reportlab-doc/examples/.
	cp -rp tools/pythonpoint/demos debian/python-reportlab-doc/usr/share/doc/python-reportlab-doc/pythonpoint-demos
	: # Replace all '#!' calls to python with $(PY_INTERPRETER)
	: # and make them executable
	for i in `find debian/python-reportlab-doc -mindepth 3 -type f`; do \
	  sed '1s,#!.*python[^ ]*\(.*\),#! $(PY_INTERPRETER)\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_installchangelogs -i
	dh_link -i
	dh_compress -i -X.py -X.pdf -X.js -Xodyssey.txt -X.xml
	dh_fixperms -i
	dh_python2 -ppython-reportlab
	dh_python3 -ppython3-reportlab
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs -a
	dh_installchangelogs -a

	rm -rf debian/python-renderpm-dbg/usr/share/doc/python-renderpm-dbg
	ln -s python-renderpm debian/python-renderpm-dbg/usr/share/doc/python-renderpm-dbg
	rm -rf debian/python-reportlab-accel-dbg/usr/share/doc/python-reportlab-accel-dbg
	ln -s python-reportlab-accel debian/python-reportlab-accel-dbg/usr/share/doc/python-reportlab-accel-dbg

	rm -rf debian/python3-renderpm-dbg/usr/share/doc/python3-renderpm-dbg
	ln -s python3-renderpm debian/python3-renderpm-dbg/usr/share/doc/python3-renderpm-dbg
	rm -rf debian/python3-reportlab-accel-dbg/usr/share/doc/python3-reportlab-accel-dbg
	ln -s python3-reportlab-accel debian/python3-reportlab-accel-dbg/usr/share/doc/python3-reportlab-accel-dbg

	dh_compress -a -X.py -X.pdf -Xodyssey.txt -X.xml
	dh_fixperms -a
	dh_python2 \
		-ppython-renderpm -ppython-reportlab-accel \
		-ppython-renderpm-dbg -ppython-reportlab-accel-dbg
	dh_python3 \
		-ppython3-renderpm -ppython3-reportlab-accel \
		-ppython3-renderpm-dbg -ppython3-reportlab-accel-dbg
	dh_strip -ppython-renderpm --dbg-package=python-renderpm-dbg
	dh_strip -ppython-reportlab-accel --dbg-package=python-reportlab-accel-dbg
	dh_strip -ppython3-renderpm --dbg-package=python3-renderpm-dbg
	dh_strip -ppython3-reportlab-accel --dbg-package=python3-reportlab-accel-dbg
	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 configure