#!/usr/bin/make -f
DEB_VERSION=$(shell dpkg-parsechangelog | grep ^Version | cut -f2 -d\ |cut -f1 -d-)
DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
py3sdo=set -e; $(foreach py, $(shell py3versions -r), $(py) $(1);)

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

override_dh_auto_build:
	$(call py3sdo, setup.py build)

override_dh_auto_install:
	$(call py3sdo, setup.py install --root=$(CURDIR)/debian/tmp --install-layout=deb)

override_dh_auto_test:
ifeq (, $(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	$(call py3sdo, setup.py egg_info)
	set -e; $(foreach py, $(shell py3versions -r), PATH=$$PATH:/sbin:/usr/sbin PYTHONPATH=. $(py) -B tests/run || [ "$(DEB_HOST_ARCH)" = powerpc ];)
endif

override_dh_auto_clean:
	$(call py3sdo, setup.py clean)

override_dh_install:
	dh_install --fail-missing -Xlib/systemd -Xsbin -Xlib/udev

	# on architectures where we build gpu-manager, install the systemd unit,
	# the udev rule, and the script for gpu detection
	if [ -d debian/tmp/lib/systemd ]; then \
		dh_install -p ubuntu-drivers-common lib/systemd; \
		dh_systemd_enable -p ubuntu-drivers-common; \
		dh_install -p ubuntu-drivers-common lib/udev/rules.d; \
		dh_install -p ubuntu-drivers-common sbin; \
	fi

	# build dh_modaliases manpage
	mkdir -p debian/dh-modaliases/usr/share/man/man1
	pod2man -c Debhelper -r "$(DEB_VERSION)" debhelper/dh_modaliases debian/dh-modaliases/usr/share/man/man1/dh_modaliases.1

override_dh_python3:
	dh_python3 --shebang=/usr/bin/python3

override_dh_clean:
	rm -f share/hybrid/hybrid-detect
	rm -f share/hybrid/gpu-manager
	rm -f quirksreader_test*.txt
	rm -rf build
	rm -rf *.egg-info
	rm -f debian/ubuntu-drivers-common.maintscript
	dh_clean
