#!/usr/bin/make -f
export DH_VERBOSE=1

include /usr/share/dpkg/pkg-info.mk
FLAKE8 := $(shell flake8 --version 2> /dev/null)

include /etc/os-release
ifeq (${VERSION_ID},"14.04")
LIBAPT_PKG_DEP="libapt-pkg4.12 (>= 1.0.1ubuntu2.23)"
else ifeq (${VERSION_ID},"16.04")
LIBAPT_PKG_DEP="libapt-pkg5.0 (>= 1.2.31)"
else ifeq (${VERSION_ID},"18.04")
LIBAPT_PKG_DEP="libapt-pkg5.0 (>= 1.6.9)"
else ifeq (${VERSION_ID},"18.10")
LIBAPT_PKG_DEP="libapt-pkg5.0 (>= 1.7.4)"
else ifeq (${VERSION_ID},"19.04")
LIBAPT_PKG_DEP="libapt-pkg5.0 (>= 1.8.1)"
endif

%:
	dh $@ --with python3,bash-completion --buildsystem=pybuild

override_dh_auto_build:
	dh_auto_build
	make -C apt-hook build

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	python3 -m pytest
ifdef FLAKE8
	# required for Trusty: flake8 does not install a __main__  for -m
	# invocation
	python3 $(shell which flake8) uaclient
else
	python3 -m flake8 uaclient
endif
endif

override_dh_gencontrol:
	[ -z '$(LIBAPT_PKG_DEP)' ] || echo extra:Depends=$(LIBAPT_PKG_DEP) >> debian/ubuntu-advantage-tools.substvars
	dh_gencontrol

override_dh_auto_install:
	dh_auto_install --destdir=debian/ubuntu-advantage-tools
	flist=$$(find $(CURDIR)/debian/ -type f -name version.py) && sed -i 's,@@PACKAGED_VERSION@@,$(DEB_VERSION),' $${flist:-did-not-find-version-py-for-replacement}
	make -C apt-hook DESTDIR=$(CURDIR)/debian/ubuntu-advantage-tools install

override_dh_auto_clean:
	dh_auto_clean
	make clean
