#!/usr/bin/make -f

export PYBUILD_NAME=zaqar
SHELL := /bin/sh -e

UPSTREAM_GIT := https://opendev.org/openstack/zaqar.git

DEBVERS ?= $(shell dpkg-parsechangelog | sed -n -e 's/^Version: //p')
VERSION ?= $(shell echo '$(DEBVERS)' | sed -e 's/^[[:digit:]]*://' -e 's/[-].*//' -e 's/[+].*//' -e 's/~git.*//g' -e 's/~/./g')
export PBR_VERSION=$(VERSION)

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

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

override_dh_auto_clean:
	dh_auto_clean
	rm -rf build .pybuild .testrepository .eggs

override_dh_auto_build:
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func zaqar-common.config
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func zaqar-common.postinst
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func zaqar-server.config
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func zaqar-server.postinst
	pkgos-merge-templates zaqar-server zaqar endpoint
	pkgos-merge-templates zaqar-common zaqar rabbit ksat

override_dh_auto_test:

override_dh_auto_install:
	pkgos-dh_auto_install --no-py2

override_dh_install:
ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	export ZAQAR_TESTS_CONFIGS_DIR=$(CURDIR)/zaqar/tests/etc ; pkgos-dh_auto_test --no-py2 'zaqar\.tests\.unit.(?!(.*transport\.websocket.*|.*transport\.wsgi.*|.*test_bootstrap\.TestBootstrap.*))'
endif

	mkdir -p $(CURDIR)/debian/zaqar-common/usr/share/zaqar-common
	PYTHONPATH=$(CURDIR)/debian/python3-zaqar/usr/lib/python3/dist-packages oslo-config-generator \
		--output-file $(CURDIR)/debian/zaqar-common/usr/share/zaqar-common/zaqar.conf \
		--wrap-width 140 \
		--namespace zaqar.common.auth \
		--namespace zaqar.common.configs \
		--namespace zaqar.storage.pipeline \
		--namespace zaqar.storage.pooling \
		--namespace zaqar.storage.mongodb \
		--namespace zaqar.storage.redis \
		--namespace zaqar.storage.sqlalchemy \
		--namespace zaqar.storage.swift \
		--namespace zaqar.transport.wsgi \
		--namespace zaqar.transport.websocket \
		--namespace zaqar.transport.base \
		--namespace zaqar.transport.validation \
		--namespace keystonemiddleware.auth_token \
		--namespace oslo.cache \
		--namespace oslo.log \
		--namespace oslo.messaging \
		--namespace oslo.middleware.cors \
		--namespace osprofiler \
		--namespace oslo.policy \
		--namespace oslo.reports
	pkgos-readd-keystone-authtoken-missing-options $(CURDIR)/debian/zaqar-common/usr/share/zaqar-common/zaqar.conf keystone_authtoken zaqar

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

	dh_install
	dh_missing --fail-missing

	for i in zaqar-benchmark.conf logging.conf ; do \
		cp etc/$$i.sample $(CURDIR)/debian/zaqar-common/usr/share/zaqar-common/$$i ; \
	done
	cp etc/zaqar-benchmark-messages.json $(CURDIR)/debian/zaqar-common/usr/share/zaqar-common

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

override_dh_clean:
	dh_clean
	rm -f debian/zaqar-common.postinst debian/zaqar-common.config \
		debian/zaqar-server.postinst debian/zaqar-server.config \
		debian/*.init debian/*.service debian/*.upstart \
		debian/*.templates
	rm -rf debian/po
