#!/usr/bin/make -f

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

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

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

override_dh_auto_clean:
	python3 setup.py clean
	rm -rf build .stestr *.egg-info
	find . -iname '*.pyc' -delete
	for i in $$(find . -type d -iname __pycache__) ; do rm -rf $$i ; done

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:
	echo "Do nothing..."

override_dh_auto_install:
	echo "Do nothing..."

override_dh_install:
	for i in $(PYTHON3S) ; do \
		python$$i setup.py install --install-layout=deb --root=$(CURDIR)/debian/tmp ; \
	done

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.*|.*storage\.test_impl_sqlalchemy\.SqlalchemyPoolsTest\.test_create_succeeds.*|.*storage\.test_impl_sqlalchemy\.SqlalchemyPoolsTest\.test_create_replaces_on_duplicate_insert.*))'
endif

	mkdir -p $(CURDIR)/debian/zaqar-common/usr/share/zaqar-common
	PYTHONPATH=$(CURDIR)/debian/tmp/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 \
		--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

	mkdir -p $(CURDIR)/debian/zaqar-common/etc/zaqar/policy.d
	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python3/dist-packages oslopolicy-sample-generator \
		--output-file $(CURDIR)/debian/zaqar-common/etc/zaqar/policy.d/00_default_policy.yaml \
		--format yaml \
		--namespace zaqar

	# Use the policy.d folder
	pkgos-fix-config-default $(CURDIR)/debian/zaqar-common/usr/share/zaqar-common/zaqar.conf oslo_policy policy_dirs /etc/zaqar/policy.d


	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
