#!/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
# 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},"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),"
DISTRO_INFO_DEPS=""  # Don't set on trusty as we aren't releasing anymore
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),"
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
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:
	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 ua-license-check.timer
	dh_systemd_enable -pubuntu-advantage-tools ua-license-check.service
	dh_systemd_enable -pubuntu-advantage-tools ua-license-check.path

override_dh_systemd_start:
	dh_systemd_start -pubuntu-advantage-tools ua-timer.timer
	dh_systemd_start -pubuntu-advantage-tools ua-license-check.path

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

ifeq (${VERSION_ID},"14.04")
	# Move ua-auto-attach.conf out to ubuntu-advantage-pro
	mkdir -p debian/ubuntu-advantage-pro/etc/init
	mv debian/ubuntu-advantage-tools/etc/init/ua-auto-attach.conf debian/ubuntu-advantage-pro/etc/init/
	rmdir debian/ubuntu-advantage-tools/etc/init
else
	# 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
endif

override_dh_auto_clean:
	dh_auto_clean
	make clean
