#!/usr/bin/make -f

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


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

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

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


override_dh_auto_test:
ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	@echo "===> Running tests"
	set -e ; set -x ; for i in 2.7 ; do \
		PYMAJOR=`echo $$i | cut -d'.' -f1` ; \
		echo "===> Testing with python$$i (python$$PYMAJOR)" ; \
		rm -rf .testrepository ; \
		testr-python$$PYMAJOR init ; \
		TEMP_REZ=`mktemp -t` ; \
		PYTHONPATH=$(CURDIR) PYTHON=python$$i testr-python$$PYMAJOR run --subunit --parallel 'glare\.tests\.unit(?!.*test_store_api\.TestStoreAPI\.test_read_data.*)' | tee $$TEMP_REZ | subunit2pyunit ; \
		cat $$TEMP_REZ | subunit-filter -s --no-passthrough | subunit-stats ; \
		rm -f $$TEMP_REZ ; \
		testr-python$$PYMAJOR slowest ; \
        done
endif

override_dh_auto_install:
	echo "Please do nothing"

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_install:
	rm -rf $(CURDIR)/debian/tmp
	python2.7 setup.py install --install-layout=deb --root $(CURDIR)/debian/tmp
	mkdir -p $(CURDIR)/debian/glare-common/usr/share/glare-common
	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python2.7/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

	# 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

	rm -rf $(CURDIR)/debian/tmp/usr/etc
	dh_install -O--buildsystem=python_distutils --fail-missing
