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

include /usr/share/dpkg/pkg-info.mk

include /etc/os-release
# see https://bugs.launchpad.net/ubuntu/+source/ubuntu-advantage-tools/+bug/1840091/comments/3
ifeq (${VERSION_ID},"14.04")
APT_PKG_DEPS="apt (>= 1.0.1ubuntu2.23), apt-transport-https (>= 1.0.1ubuntu2.23), apt-utils (>= 1.0.1ubuntu2.23), libapt-inst1.5 (>= 1.0.1ubuntu2.23), libapt-pkg4.12 (>= 1.0.1ubuntu2.23) "
else ifeq (${VERSION_ID},"16.04")
APT_PKG_DEPS="apt (>= 1.2.32), apt-transport-https (>= 1.2.32), apt-utils (>= 1.2.32), libapt-inst2.0 (>= 1.2.32), libapt-pkg5.0 (>= 1.2.32)"
else ifeq (${VERSION_ID},"18.04")
APT_PKG_DEPS="apt (>= 1.6.11), apt-utils (>= 1.6.11), libapt-inst2.0 (>= 1.6.11), libapt-pkg5.0 (>= 1.6.11)"
else ifeq (${VERSION_ID},"19.04")
APT_PKG_DEPS="apt (>= 1.8.1), apt-utils (>= 1.8.1), libapt-inst2.0 (>= 1.8.1), libapt-pkg5.0 (>= 1.8.1)"
else ifeq (${VERSION_ID},"19.10")
APT_PKG_DEPS="apt (>= 1.8.1), apt-utils (>= 1.8.1), libapt-pkg5.90 (>= 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
endif

override_dh_gencontrol:
	[ -z '$(APT_PKG_DEPS)' ] || echo extra:Depends=$(APT_PKG_DEPS) >> 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
