#!/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/python2 -c 'import sys; print sys.version[:3]')

PY_INTERPRETER = /usr/bin/python2

include /usr/share/python/python.mk

build: build-stamp
build-stamp: $(PYVERS:%=build-python%) $(PYVERS:%=dbg-build-python%)
	touch $@
build-python%:
	python$* setup.py build
	touch $@
dbg-build-python%:
	python$*-dbg setup.py build
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f *-stamp build-python* dbg-build-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%)

install-prereq: build-stamp
	dh_testdir
	dh_testroot
	dh_clean -k

install-python%: install-prereq
	python$* setup.py install --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,$*)
	mv debian/python-reportlab/$(call py_libdir,$*)/_renderPM.so \
		debian/python-renderpm/$(call py_libdir,$*)/

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

	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 --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,$*)
	mv debian/python-reportlab-accel-dbg/$(call py_libdir,$*)/_renderPM_d.so \
		debian/python-renderpm-dbg/$(call py_libdir,$*)/

# Build architecture-independent files here.
# Pass -i to all debhelper commands in this target to reduce clutter.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installdocs -i README.txt
	rm -rf debian/python-reportlab-doc/usr/share/doc/python-reportlab-doc/examples/tests
	: # 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 -i
	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

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