#!/usr/bin/make -f

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

%:
	dh $@  --with python2,python3,systemd

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

override_dh_auto_clean:
	python2 setup.py clean
	python3 setup.py clean

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

override_dh_auto_install:
	echo "Do nothing..."

override_dh_auto_test:
	echo "Do nothing..."

override_dh_install:
	pkgos-dh_auto_install

ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	# NOTE(coreycb) Skip Py3.7 tests until they run successfully upstream:
	#https://bugs.launchpad.net/mistral/+bug/1790001
	# pkgos-dh_auto_test
	echo "===> Running tests"
	set -ex ; for i in 2.7 3.6 ; do \
		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 | tee $$TEMP_REZ | subunit2pyunit ; \
		cat $$TEMP_REZ | subunit-filter -s --no-passthrough | subunit-stats ; \
		rm -f $$TEMP_REZ ; \
		rm -rf .stestr ; \
	done
endif


	# Copy the configuration files
	set -e ; for i in logging.conf wf_trace_logging.conf ; do \
		install -D -m 0664 etc/$$i.sample $(CURDIR)/debian/mistral-common/usr/share/mistral-common/$$i ; \
	done
	install -D -m 0664 etc/policy.json $(CURDIR)/debian/mistral-common/usr/share/mistral-common/policy.json ; \
	PYTHONPATH=$(CURDIR)/debian/python3-mistral/usr/lib/python3/dist-packages python3-oslo-config-generator \
		--namespace mistral.config \
		--namespace oslo.db \
		--namespace oslo.messaging \
		--namespace oslo.middleware.cors \
		--namespace keystonemiddleware.auth_token \
		--namespace periodic.config \
		--namespace oslo.log \
		--namespace oslo.policy \
		--namespace oslo.service.sslutils \
		--output-file $(CURDIR)/debian/mistral-common/usr/share/mistral-common/mistral.conf
	pkgos-readd-keystone-authtoken-missing-options $(CURDIR)/debian/mistral-common/usr/share/mistral-common/mistral.conf keystone_authtoken mistral

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

	pkgos-fix-config-default $(CURDIR)/debian/mistral-common/usr/share/mistral-common/mistral.conf keystone_authtoken auth_protocol http

	dh_install
	dh_missing --fail-missing

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