#!/usr/bin/make -f

#export DH_VERBOSE=1

# Prevent setuptools/distribute from accessing the internet.
export http_proxy=http://127.0.9.1:9

REL_NO	:= $(shell lsb_release -rs)
RELEASE_GTE_RARING	:= $(shell expr $(REL_NO) \>= 13.04)
%:
	dh "$@" --with python3,translations

override_dh_auto_clean:
	rm -rf $(CURDIR)/plainbox/build
	rm -rf $(CURDIR)/plainbox/*.egg-info
	rm -rf $(CURDIR)/checkbox/build
	rm -rf $(CURDIR)/checkbox/*.egg-info
	rm -rf $(CURDIR)/checkbox-ng/build
	rm -rf $(CURDIR)/checkbox-ng/*.egg-info
	rm -rf $(CURDIR)/checkbox-ng/*.egg-link
	rm -rf $(CURDIR)/plainbox-provider*/*.egg-info
	-find . -name \*.mo -exec rm {} \;
	-rm -f debian/checkbox-cli.postrm debian/checkbox-qt.postrm debian/checkbox-hw-collection.postrm
	debconf-updatepo
	dh_clean

override_dh_auto_build:
ifeq (1,$(RELEASE_GTE_RARING))
	# funcsigs.Signature (the backport) is part of the Python standard library,
	# available as inspect.Signature in Python 3.3 onwards.
	rm -rf $(CURDIR)/plainbox/plainbox/vendor/funcsigs
	# mock is now part of the Python standard library, available as
	# unittest.mock in Python 3.3 onwards.
	echo "from unittest.mock import *" > $(CURDIR)/plainbox/plainbox/vendor/mock.py
	dh_quilt_patch
endif
	cd $(CURDIR)/checkbox-gui && qmake PREFIX=/usr && make
	python3 setup.py build

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
override_dh_auto_test:
	# drop LD_PRELOAD to avoid running under fakeroot; drop TMPDIR to work
	# around LP#972324 (set by autopkgtest)
	cd $(CURDIR)/checkbox-old && env -u LD_PRELOAD -u TMPDIR CHECKBOX_PACKAGING=1 ./setup.py test -vv
	rm -rf  $(CURDIR)/checkbox-old/scripts/__pycache__
	cd $(CURDIR)/plainbox && ./setup.py test -vv
	# checkbox-ng requires plainbox to be installed, let's just use the develop
	# mode and create an egg-link to the plainbox source
	cd $(CURDIR)/plainbox && PYTHONPATH=$(CURDIR)/checkbox-ng ./setup.py develop -d $(CURDIR)/checkbox-ng
	cd $(CURDIR)/checkbox-ng && ./setup.py test -vv
endif

override_dh_auto_install:
	python3 setup.py install --root=$(CURDIR)/debian/tmp --install-layout=deb
	mkdir -p $(CURDIR)/debian/tmp/usr/lib/checkbox/bin/
	cp -R $(CURDIR)/debian/tmp/usr/share/checkbox/scripts/* $(CURDIR)/debian/tmp/usr/lib/checkbox/bin/
	cd $(CURDIR)/checkbox-gui && make INSTALL_ROOT=$(CURDIR)/debian/tmp install
	dh_install

override_dh_installdocs:
	# This is tricky, dh_installdirs installs stuff to the first package in
	# debian/control, ensure that's the -doc package.
	dh_installdocs -pcheckbox ./checkbox-old/README
	dh_installdocs -pcheckbox-cli ./checkbox-old/README
	dh_installdocs -pcheckbox-qt ./checkbox-old/README
	dh_installdocs -pcheckbox-hw-collection ./checkbox-old/README
	dh_installdocs

override_dh_python3:
	# dh_python3 runs automatically on packages with the python3- prefix
	# let's not forget plainbox
	dh_python3 --package=plainbox
	dh_python3 --no-package=checkbox-gui

override_dh_installdeb:
	dh_installdeb
