#!/usr/bin/make -f

PYTHON3S:=$(shell py3versions -vr)

include /usr/share/openstack-pkg-tools/pkgos.make

%:
	dh $@ --buildsystem=python_distutils --with python3,systemd,sphinxdoc

override_dh_clean:
	dh_clean
	rm -f debian/watcher-common.config debian/watcher-common.postinst debian/watcher-api.config debian/watcher-api.postinst
	rm -rf debian/*.upstart debian/*.service
	rm -rf debian/watcher-common.postrm debian/*.templates debian/po

override_dh_auto_build:
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func watcher-common.config
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func watcher-common.postinst
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func watcher-api.config
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func watcher-api.postinst
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_postrm watcher-common.postrm
	pkgos-merge-templates watcher-api watcher endpoint
	pkgos-merge-templates watcher-common watcher db rabbit ksat

override_dh_auto_clean:
	python3 setup.py clean

override_dh_auto_install:
	echo "Do nothing..."

override_dh_install:
	pkgos-dh_auto_install --no-py2

ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	# NOTE(coreycb) Skip Py3.7 tests until they run successfully upstream:
	# https://bugs.launchpad.net/bugs/1786326
	# pkgos-dh_auto_test 'watcher\.tests\.(?!.*api\.test_hooks\.TestContextHook\.test_context_hook_before_method.*)'
	echo "===> Running tests"
	set -ex ; for i in $(PYTHON3S) ; do \
		[ $$i = "3.7" ] && break; \
		PYMAJOR=`echo $$i | cut -d'.' -f1` ; \
		echo "===> Testing with python$$i (python$$PYMAJOR)" ; \
		rm -rf .stestr ; \
		TEMP_REZ=`mktemp -t` ; \
		PYTHONPATH=$(CURDIR) PYTHON=python$$i python$$PYMAJOR-stestr run --subunit 'watcher\.tests\.(?!.*api\.test_hooks\.TestContextHook\.test_context_hook_before_method.*)' | tee $$TEMP_REZ | subunit2pyunit ; \
		cat $$TEMP_REZ | subunit-filter -s --no-passthrough | subunit-stats ; \
		rm -f $$TEMP_REZ ; \
		rm -rf .stestr ; \
	done


endif

	rm -rf $(CURDIR)/debian/python3-watcher/usr/etc
	mkdir -p $(CURDIR)/debian/watcher-common/usr/share/watcher-common
	PYTHONPATH=$(CURDIR)/debian/python3-watcher/usr/lib/python3/dist-packages python3-oslo-config-generator \
		--output-file $(CURDIR)/debian/watcher-common/usr/share/watcher-common/watcher.conf \
		--wrap-width 140 \
		--namespace watcher \
		--namespace keystonemiddleware.auth_token \
		--namespace oslo.cache \
		--namespace oslo.concurrency \
		--namespace oslo.db \
		--namespace oslo.log \
		--namespace oslo.messaging \
		--namespace oslo.policy \
		--namespace oslo.reports \
		--namespace oslo.service.periodic_task \
		--namespace oslo.service.service \
		--namespace oslo.service.wsgi
	pkgos-readd-keystone-authtoken-missing-options $(CURDIR)/debian/watcher-common/usr/share/watcher-common/watcher.conf keystone_authtoken watcher

	PYTHONPATH=$(CURDIR)/debian/python3-watcher/usr/lib/python3/dist-packages python3-oslopolicy-sample-generator \
		--output-file $(CURDIR)/debian/watcher-common/usr/share/watcher-common/policy.json \
		--format json \
		--namespace watcher

	pkgos-fix-config-default $(CURDIR)/debian/watcher-common/usr/share/watcher-common/watcher.conf oslo_concurrency lock_path /var/lock/watcher

	dh_install
	dh_missing --fail-missing

	# Disable https in default setup, as this makes our CI fail
	pkgos-fix-config-default $(CURDIR)/debian/watcher-common/usr/share/watcher-common/watcher.conf keystone_authtoken auth_protocol http

	# Fix the missing sql_connection directive
	pkgos-fix-config-default $(CURDIR)/debian/watcher-common/usr/share/watcher-common/watcher.conf database sqlite:////var/lib/watcher/watcherdb

override_dh_auto_test:
	echo "Do nothing..."

override_dh_sphinxdoc:
ifeq (,$(findstring nodocs, $(DEB_BUILD_OPTIONS)))
	PYTHON=python3 PYTHONPATH=. python3 -m sphinx $(SPHINXOPTS) -b html doc/source $(CURDIR)/debian/watcher-doc/usr/share/doc/watcher-doc/html
	touch $(CURDIR)/debian/watcher-doc/usr/share/doc/watcher-doc/html/_static/toggle.js
	dh_sphinxdoc
endif

override_dh_python3:
	dh_python3 --shebang=/usr/bin/python3
