#!/usr/bin/make -f

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

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

override_dh_auto_clean:
	find . -iname '*.pyc' -delete
	rm -f debian/*.init debian/*.upstart debian/*.service debian/*.templates
	rm -f debian/glare-common.postinst debian/glare-common.config debian/glare-api.config debian/glare-api.postinst debian/glare-common.postrm
	rm -rf build

override_dh_auto_build:
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func glare-common.config
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func glare-common.postinst
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func glare-api.config
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func glare-api.postinst
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_postrm glare-common.postrm
	pkgos-merge-templates glare-api glare endpoint
	pkgos-merge-templates glare-common glare db 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)))
	# Skip Python 3.7 tests until Glare supports it - https://bugs.launchpad.net/glare/+bug/1837778
	if [ -x /usr/bin/python3.6 ] ; then \
		rm -rf .testrepository ; \
		/usr/bin/testr-python3 init ; \
		TEMP_REZ=$(mktemp -t) ; \
		PYTHON=python3.6 /usr/bin/testr-python3 run --parallel --subunit 'glare\.tests\.unit(?!(.*test_store_api\.TestStoreAPI\.test_read_data.*|.*test_wsgi\.RequestTest\.test_best_match_language_unknown.*|.*test_wsgi\.ResourceTest\.test_translate_exception.*))' | tee ${TEMP_REZ} | subunit2pyunit ; \
		cat ${TEMP_REZ} | subunit-filter -s --no-passthrough | subunit-stats ; \
		rm -f ${TEMP_REZ} ; \
		${TESTR} slowest ; \
		rm -rf .testrepository ; \
	fi
endif
	mkdir -p $(CURDIR)/debian/glare-common/usr/share/glare-common
	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python3/dist-packages oslo-config-generator \
		--output-file $(CURDIR)/debian/glare-common/usr/share/glare-common/glare.conf \
                --wrap-width 140 \
		--namespace glare \
		--namespace glance.store \
		--namespace keystonemiddleware.auth_token \
		--namespace oslo.concurrency \
		--namespace oslo.db \
		--namespace oslo.db.concurrency \
		--namespace oslo.log \
		--namespace oslo.messaging \
		--namespace oslo.middleware.cors \
		--namespace oslo.middleware.http_proxy_to_wsgi \
		--namespace oslo.policy
	pkgos-readd-keystone-authtoken-missing-options $(CURDIR)/debian/glare-common/usr/share/glare-common/glare.conf keystone_authtoken glare

	# Set a valid default. /tmp is a security problem.
	pkgos-fix-config-default $(CURDIR)/debian/glare-common/usr/share/glare-common/glare.conf oslo_concurrency lock_path /var/lock/glare

	cp etc/glare-paste.ini $(CURDIR)/debian/glare-common/usr/share/glare-common
	cp etc/glare-swift.conf.sample $(CURDIR)/debian/glare-common/usr/share/glare-common/glare-swift.conf

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

	rm -rf $(CURDIR)/debian/tmp/usr/etc
	dh_install
	dh_missing --fail-missing

override_dh_sphinxdoc:
ifeq (,$(findstring nodocs, $(DEB_BUILD_OPTIONS)))
	echo "No doc for now."
#	sphinx-build $(SPHINXOPTS) -b html doc/source $(CURDIR)/debian/glare-doc/usr/share/doc/glare-doc/html
#	dh_sphinxdoc -O--buildsystem=python_distutils
endif

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