#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

PYVERS=$(shell pyversions -r debian/control)
PY3VERS=$(shell py3versions -r debian/control)
-include /usr/share/python/python.mk
ifeq (,$(py_sitename))
  py_sitename = site-packages
  py_libdir = /usr/lib/python$(subst python,,$(1))/site-packages
  py_sitename_sh = $(py_sitename)
  py_libdir_sh = $(py_libdir)
  py_setup_install_args =
endif
DEFAULT_PYTHON3 = $(shell py3versions -d)
DEFAULT_BUILDIR = $(shell $(DEFAULT_PYTHON3) -c 'from distutils.command.build import build; from distutils.core import Distribution; b = build(Distribution()); b.finalize_options(); print(b.build_platlib)')

configure: configure-stamp
configure-stamp:
	dh_testdir
	rm -f configure
	touch configure-stamp

build: configure build-stamp
build-stamp:
	dh_testdir
	for python in $(PYVERS); do \
		$$python setup.py build ; \
	done
	for python3 in $(PY3VERS); do \
		$$python3 setup.py build ; \
	done
	for python in $(PYVERS); do \
		$$python-dbg setup.py build ; \
	done
	for python3 in $(PY3VERS); do \
		$$python3-dbg setup.py build ; \
	done
	touch build-stamp

clean: configure
	dh_testdir
	dh_testroot
	rm -fr *-stamp build
	-for python in $(PYVERS); do \
		$$python setup.py clean ; \
	done
	-for python3 in $(PY3VERS); do \
		$$python3 setup.py clean ; \
	done
	rm -rf $(CURDIR)/doc/src/_build
	dh_clean

build-arch: build
build-indep:
	export PYTHONPATH=$(CURDIR)/$(DEFAULT_BUILDIR)/; \
	cd $(CURDIR)/doc/src; \
	make -f Makefile html; \
	make -f Makefile text

install-arch: build-arch
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	# psycopg2
	for python in $(PYVERS); do \
            $$python setup.py install \
		--root=$(CURDIR)/debian/python-psycopg2 --no-compile $(py_setup_install_args); \
	done
	for python3 in $(PY3VERS); do \
		$$python3 setup.py install \
			--root=$(CURDIR)/debian/python3-psycopg2 --no-compile --install-layout=deb; \
	done
	for python in $(PYVERS); do \
	    $$python-dbg setup.py install \
		--root=$(CURDIR)/debian/python-psycopg2-dbg --no-compile $(py_setup_install_args); \
	done
	for python3 in $(PY3VERS); do \
		$$python3-dbg setup.py install \
			--root=$(CURDIR)/debian/python3-psycopg2-dbg --no-compile --install-layout=deb; \
	done
	find debian/python-*-dbg ! -type d ! -name '*.so' | xargs rm -f
	find debian/python-*-dbg -depth -empty -exec rmdir {} \;
	rm -rf $(CURDIR)/debian/python3-psycopg2-dbg/usr/lib/python3/dist-packages/psycopg2/tests
	# # Incompatible with Python3.7 (#902715)
	rm $(CURDIR)/debian/python3-psycopg2/usr/lib/python3/dist-packages/psycopg2/tests/test_async_keyword.py

install-indep: build-indep
	dh_testdir
	dh_testroot
	dh_prep
	dh_install

# Build architecture-independent files here.
binary-indep: build install-indep
	dh_testdir
	dh_testroot
	dh_installdocs -i README.rst AUTHORS
	dh_installchangelogs NEWS
	dh_sphinxdoc -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install-arch
	dh_testdir
	dh_testroot
	dh_installdocs -a README.rst AUTHORS
	dh_installchangelogs NEWS
	dh_link -a
	dh_strip -ppython-psycopg2 --dbg-package=python-psycopg2-dbg
	rm -rf debian/python-psycopg2-dbg/usr/share/doc/python-psycopg2-dbg
	ln -s python-psycopg2 debian/python-psycopg2-dbg/usr/share/doc/python-psycopg2-dbg
	dh_strip -ppython3-psycopg2 --dbg-package=python3-psycopg2-dbg
	rm -rf debian/python3-psycopg2-dbg/usr/share/doc/python3-psycopg2-dbg
	ln -s python3-psycopg2 debian/python3-psycopg2-dbg/usr/share/doc/python3-psycopg2-dbg
	dh_compress -a -X.js -X_sources -Xobjects.inv
	dh_fixperms -a
	dh_makeshlibs -a
	dh_python2 -a
	dh_python3 -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 configure
