#!/usr/bin/make -f
include /usr/share/dpkg/pkg-info.mk
DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
TEST_ARCHES=amd64 arm64
PYBUILD_NAME=ubuntu-drivers-common

%:
	dh "$@" --with python3,apport --buildsystem=pybuild

override_dh_auto_test:
ifeq (, $(findstring nocheck, $(DEB_BUILD_OPTIONS)))
  ifeq ($(DEB_HOST_ARCH),$(filter $(DEB_HOST_ARCH), $(TEST_ARCHES)))
	./setup.py egg_info
	PYTHONPATH=. tests/run
	# Run mypy type checking
	mypy UbuntuDrivers/ Quirks/ ubuntu-drivers
	# Run black tests. Same scope as git-hooks/pre-commit, so that a tree
	# which passes the hook also passes the build.
	black --check .
  endif
endif

override_dh_install:
	# on architectures where we build gpu-manager, this also picks up the
	# systemd unit, the udev rule and the script for gpu detection; they all
	# live under /usr now, so debian/ubuntu-drivers-common.install covers them
	dh_install

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

override_dh_installsystemd:
	# com.ubuntu.Drivers is D-Bus activated: enable it so that the
	# dbus-com.ubuntu.Drivers.service alias gets created, but never start it
	# at install time. dbus-daemon starts it on the first method call and it
	# exits again once idle.
	dh_installsystemd --name=ubuntu-drivers --no-start
	dh_installsystemd --name=gpu-manager

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
	find $(CURDIR) -name "__pycache__" -exec rm -rf "{}" \;
	rm -f share/hybrid/gpu-manager.o share/hybrid/json-parser/json.o
	dh_clean
