#!/usr/bin/make -f

export PYBUILD_SYSTEM := pyproject

# export PYBUILD_VERBOSE := 1
# export DH_VERBOSE := 1

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

include /usr/share/dpkg/architecture.mk

CRIU_MAKE_ARGS :=
ifeq ($(DEB_HOST_ARCH),armhf)
# OBS builds armhf on ARMv8 hosts, where CRIU otherwise selects ARMv7
# without an FPU.
CRIU_MAKE_ARGS += ARCHCFLAGS=-march=armv7-a+fp
endif

version := $(shell dpkg-parsechangelog -S Version | sed -rne 's,([^-\+]+)+(\+dfsg)*.*,\1,p'i)
source := $(shell dpkg-parsechangelog -S Source)
upstreampck := $(source)_$(version).orig.tar.gz

PACKAGE = $(firstword $(shell dh_listpackages))
TMP     = $(CURDIR)/debian/tmp

%:
	dh ${@} --with python3 --buildsystem=pybuild

override_dh_auto_clean:
	$(MAKE) V=1 -j1 clean

override_dh_auto_build:
	$(MAKE) V=1 $(CRIU_MAKE_ARGS)
	PYBUILD_NAME=pycriu PYBUILD_DIR=lib \
	pybuild --build

	PYBUILD_NAME=crit PYBUILD_DIR=crit \
	pybuild --build -d crit

override_dh_auto_install:
	$(MAKE) install V=1 SKIP_PIP_INSTALL=1 $(CRIU_MAKE_ARGS) DESTDIR="$(TMP)" PREFIX="/usr" LIBEXECDIR="/usr/lib/$(DEB_TARGET_MULTIARCH)"

	PYBUILD_NAME=pycriu PYBUILD_DIR=lib \
	pybuild --install --dest-dir $(CURDIR)/debian/python3-pycriu -d lib

	PYBUILD_NAME=crit PYBUILD_DIR=crit \
	pybuild --install --dest-dir $(CURDIR)/debian/python3-pycriu -d crit

override_dh_auto_test:
	# disabled: not appropriate to run as part of the package build

clean:
	@ echo CLEAN
	[ -f ../$(upstreampck) ] || tar czf ../$(upstreampck) --exclude=.git --exclude=debian --exclude=.pc \
		--exclude='criu-build-deps*' --exclude='*.buildinfo' --exclude='*.changes' .
	dh $@ --with python3 --buildsystem=pybuild
