#!/usr/bin/make -f

PYTHONS:=$(shell pyversions -vr)

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

export OSLO_PACKAGE_VERSION=$(shell dpkg-parsechangelog | grep Version: | cut -d' ' -f2 | sed -e 's/^[[:digit:]]*://' -e 's/[-].*//' -e 's/~/.0/' | head -n 1)

%:
	dh $@  --with python2,systemd

override_dh_auto_test:
ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	PYTHONPATH=$(CURDIR) bash run_tests.sh -N -P || true
endif

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

override_dh_auto_install:
	set -e ; for pyvers in $(PYTHONS); do \
		python$$pyvers setup.py install --install-layout=deb \
			--root $(CURDIR)/debian/tmp; \
	done

	# 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
	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python2.7/dist-packages oslo-config-generator \
		--namespace mistral.config \
		--namespace oslo.db \
		--namespace oslo.messaging \
		--namespace keystonemiddleware.auth_token \
		--namespace periodic.config \
		--namespace oslo.log \
		--output-file $(CURDIR)/debian/mistral-common/usr/share/mistral-common/mistral.conf
	pkgos-fix-config-default $(CURDIR)/debian/mistral-common/usr/share/mistral-common/mistral.conf keystone_authtoken auth_protocol http

override_dh_install:
	dh_install --fail-missing
	# As usual, pbr is missing the alembic.ini
	cp mistral/db/sqlalchemy/migration/alembic.ini $(CURDIR)/debian/python-mistral/usr/lib/python2.7/dist-packages/mistral/db/sqlalchemy/migration/

override_dh_auto_build:
	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
