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

# Bionic and Xenial each have older versions of distro-info that don't support
# the flag --supported-esm. Those versions are 0.18 and 0.14build1,
# respectively. So we set specific distro-info requirements for bionic and later
# versus Xenial to make those contraints applicable on each series.
DISTRO_INFO_DEPS="distro-info (>= 0.18ubuntu0.18.04.1),"

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),"
DISTRO_INFO_DEPS="distro-info (>= 0.14ubuntu0.2),"
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,systemd --buildsystem=pybuild

override_dh_auto_build:
	dh_auto_build
	make -C apt-hook build

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	make -C apt-hook test
	python3 -m pytest
	python3 -m flake8 uaclient
endif

override_dh_gencontrol:
	echo extra:Depends=$(APT_PKG_DEPS) $(DISTRO_INFO_DEPS) >> debian/ubuntu-advantage-tools.substvars
	dh_gencontrol

override_dh_systemd_enable:
	dh_systemd_enable -pubuntu-advantage-pro ua-auto-attach.service
	dh_systemd_enable -pubuntu-advantage-tools ua-reboot-cmds.service
	dh_systemd_enable -pubuntu-advantage-tools ua-timer.timer
	dh_systemd_enable -pubuntu-advantage-tools ua-timer.service
	dh_systemd_enable -pubuntu-advantage-tools ubuntu-advantage.service
ifeq (${VERSION_ID},"16.04")
	# Only enable cloud-id-shim on Xenial
	dh_systemd_enable -pubuntu-advantage-tools ubuntu-advantage-cloud-id-shim.service
endif

override_dh_systemd_start:
	dh_systemd_start -pubuntu-advantage-tools ua-timer.timer
	dh_systemd_start -pubuntu-advantage-tools ubuntu-advantage.service

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
	# We want to guarantee that we are not shipping any conftest files
	find $(CURDIR)/debian/ubuntu-advantage-tools -type f -name conftest.py -delete

ifneq (${VERSION_ID},"16.04")
	# Only install cloud-id-shim on Xenial
	rm $(CURDIR)/debian/ubuntu-advantage-tools/lib/systemd/system/ubuntu-advantage-cloud-id-shim.service
endif

	# Move ua-auto-attach.service out to ubuntu-advantage-pro
	mkdir -p debian/ubuntu-advantage-pro/lib/systemd/system
	mv debian/ubuntu-advantage-tools/lib/systemd/system/ua-auto-attach.* debian/ubuntu-advantage-pro/lib/systemd/system
	cd debian/ubuntu-advantage-tools


override_dh_auto_clean:
	dh_auto_clean
	make clean
