#!/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),
endif

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

override_dh_auto_build:
	dh_auto_build
	make -C apt-hook build
	make -C debian/po build

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
# Hooks will only be delivered on LTS instances
ifeq (LTS,$(findstring LTS,$(VERSION)))
	make -C apt-hook test
endif
	python3 -m pytest
	python3 -m flake8 uaclient
endif

override_dh_gencontrol:
	echo "extra:Depends=$(APT_PKG_DEPS) $(DISTRO_INFO_DEPS)" >> debian/ubuntu-pro-client.substvars
	dh_gencontrol

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

override_dh_systemd_start:
	dh_systemd_start -pubuntu-pro-client ua-timer.timer
	dh_systemd_start -pubuntu-pro-client ubuntu-advantage.service

override_dh_auto_install:
	dh_auto_install --destdir=debian/ubuntu-pro-client
	debian/jinja2_render debian/apparmor/ubuntu_pro_apt_news.jinja2 debian/apparmor/ubuntu_pro_apt_news ubuntu_codename=${UBUNTU_CODENAME}
	# quick syntax check on the generated profile
	apparmor_parser -K -T -Q debian/apparmor/ubuntu_pro_apt_news
	install -D -m 644 $(CURDIR)/debian/apparmor/ubuntu_pro_apt_news $(CURDIR)/debian/ubuntu-pro-client/etc/apparmor.d/ubuntu_pro_apt_news
	dh_apparmor -pubuntu-pro-client --profile-name=ubuntu_pro_apt_news

	flist=$$(find $(CURDIR)/debian/ -type f -name version.py) && sed -i 's,@@PACKAGED_VERSION@@,$(DEB_VERSION),' $${flist:-did-not-find-version-py-for-replacement}

	# We install the conf file even on non-LTS version to avoid issues on upgrade scenarios
	make -C apt-hook DESTDIR=$(CURDIR)/debian/ubuntu-pro-client install-conf
	make -C apt-hook DESTDIR=$(CURDIR)/debian/ubuntu-pro-client install

	# We want to guarantee that we are not shipping any conftest files
	find $(CURDIR)/debian/ubuntu-pro-client -type f -name conftest.py -delete

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

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

	# move migration scripts from before the package rename into ubuntu-advantage-tools
	mkdir -p debian/ubuntu-advantage-tools/usr/lib/ubuntu-advantage
	mv debian/ubuntu-pro-client/usr/lib/ubuntu-advantage/migrate_user_config.py debian/ubuntu-advantage-tools/usr/lib/ubuntu-advantage
	mv debian/ubuntu-pro-client/usr/lib/ubuntu-advantage/patch_status_json.py debian/ubuntu-advantage-tools/usr/lib/ubuntu-advantage


override_dh_auto_clean:
	dh_auto_clean
	make clean
