#!/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)
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
  endif
endif

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