#!/usr/bin/make -f

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

export TZ=UTC

%:
	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)))
	pkgos-dh_auto_test --no-py2 'watcher\.tests\.api\.(?!(.*test_hooks\.TestContextHook\.test_context_hook_before_method.*))'
	#pkgos-dh_auto_test --no-py2 'watcher\.tests\.applier.*'
	pkgos-dh_auto_test --no-py2 'watcher\.tests\.cmd.*'
	pkgos-dh_auto_test --no-py2 'watcher\.tests\.common.*'
	pkgos-dh_auto_test --no-py2 'watcher\.tests\.conf.*'
	pkgos-dh_auto_test --no-py2 'watcher\.tests\.datasource.*'
	pkgos-dh_auto_test --no-py2 'watcher\.tests\.db.*'
	pkgos-dh_auto_test --no-py2 'watcher\.tests\.decision_engine.*'
	pkgos-dh_auto_test --no-py2 'watcher\.tests\.notifications.*'
	pkgos-dh_auto_test --no-py2 'watcher\.tests\.objects.*'
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

	mkdir -p $(CURDIR)/debian/watcher-api/usr/bin
	cp watcher/api/app.wsgi $(CURDIR)/debian/watcher-api/usr/bin/watcher-wsgi-api

	# 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
