#!/usr/bin/make -f

# For now, permanently disable test cases
DEB_BUILD_OPTIONS=nocheck

%:
	dh "$@" --with python2,python3

override_dh_auto_clean:
	dh_auto_clean
	rm -rf build
	rm -f debian/apport-notifyd

override_dh_auto_build:
	dh_auto_build
	python3 setup.py build
	gcc -Wall $(shell dpkg-buildflags --get CFLAGS) $(shell dpkg-buildflags --get CPPFLAGS) $(shell dpkg-buildflags --get LDFLAGS) debian/apport-notifyd.c -o debian/apport-notifyd

override_dh_installinit:
	dh_installinit --error-handler=true

override_dh_auto_install:
	dh_auto_install
	set -ex; for python in $(shell py3versions -r); do \
		$$python setup.py install --root=$(CURDIR)/debian/tmp --install-layout=deb; \
        done

override_dh_auto_test:
ifeq (, $(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	-env -u LD_PRELOAD -u TMPDIR APPORT_TEST_NOXVFB=1 sh test/run
endif

override_dh_install:
	dh_install -X.pyc -X.egg-info --list-missing
	pod2man -c Debhelper -r "$(DEB_VERSION)" debian/debhelper/dh_apport debian/debhelper/dh_apport.1

ifneq (, $(filter apport-retrace, $(shell dh_listpackages)))
        # apport-retrace needs python-launchpadlib, which is not yet available
        #         # for Python 3; so switch back to Python 2
	sed -i '1 s/python3.*$$/python/' debian/apport-retrace/usr/bin/apport-retrace
endif

ifneq (, $(filter apport-kde, $(shell dh_listpackages)))
	# Same applies for KDE4...
	sed -i '1 s/python3.*$$/python/' debian/apport-kde/usr/share/apport/apport-kde
endif

override_dh_python3:
	dh_python3 --skip-private
