#!/usr/bin/make -f

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

LAST_CHANGE = $(shell dpkg-parsechangelog | grep Date: | sed 's/Date: //')
BUILD_DATE  = $(shell LC_ALL=C date -u "+%B %d, %Y" -d "$(LAST_CHANGE)")
SPHINXOPTS := -D today="$(BUILD_DATE)"
SPHINXOPTS += -D html_last_updated_fmt="$(BUILD_DATE)"

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 $@ --buildsystem=python_distutils --with python2,sphinxdoc,systemd

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 'glance\.tests\.unit.*' | 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_build:
	dh_auto_build -O--buildsystem=python_distutils

	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func glance-common.config
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func glance-common.postinst
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func glance-api.config
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func glance-api.postinst

override_dh_sphinxdoc:
ifeq (,$(findstring nodocs, $(DEB_BUILD_OPTIONS)))
	sphinx-build $(SPHINXOPTS) -b html doc/source $(CURDIR)/debian/python-glance-doc/usr/share/doc/python-glance-doc/html
	dh_sphinxdoc -O--buildsystem=python_distutils
endif

override_dh_installman:
ifeq (,$(findstring nodocs, $(DEB_BUILD_OPTIONS)))
	sphinx-build $(SPHINXOPTS) -b man doc/source doc/build/man
	dh_installman -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
	dh_install -O--buildsystem=python_distutils --fail-missing
	# Generate all config files
	mkdir -p $(CURDIR)/debian/glance-common/usr/share/glance-common
	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python2.7/dist-packages oslo-config-generator \
		--output-file $(CURDIR)/debian/glance-common/usr/share/glance-common/glance-api.conf \
		--namespace glance.api \
		--namespace glance.store \
		--namespace oslo.concurrency \
		--namespace oslo.messaging \
		--namespace oslo.db \
		--namespace oslo.db.concurrency \
		--namespace oslo.policy \
		--namespace keystonemiddleware.auth_token \
		--namespace oslo.log \
		--namespace oslo.middleware.cors
	pkgos-fix-config-default $(CURDIR)/debian/glance-common/usr/share/glance-common/glance-api.conf keystone_authtoken auth_protocol http
	pkgos-fix-config-default $(CURDIR)/debian/glance-common/usr/share/glance-common/glance-api.conf glance_store filesystem_store_datadir /var/lib/glance/images
	pkgos-fix-config-default $(CURDIR)/debian/glance-common/usr/share/glance-common/glance-api.conf oslo_concurrency lock_path /var/lock/glance
	# Do not persist build time CPU numbers. See https://bugs.debian.org/807475
	sed -i 's|^[ \t#]*workers[ \t]*=[ \t].*|#workers = 2|' $(CURDIR)/debian/glance-common/usr/share/glance-common/glance-api.conf

	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python2.7/dist-packages oslo-config-generator \
		--output-file $(CURDIR)/debian/glance-common/usr/share/glance-common/glance-registry.conf \
		--namespace glance.registry \
		--namespace glance.store \
		--namespace oslo.messaging \
		--namespace oslo.db \
		--namespace oslo.db.concurrency \
		--namespace oslo.policy \
		--namespace keystonemiddleware.auth_token \
		--namespace oslo.log
	pkgos-fix-config-default $(CURDIR)/debian/glance-common/usr/share/glance-common/glance-registry.conf keystone_authtoken auth_protocol http
	pkgos-fix-config-default $(CURDIR)/debian/glance-common/usr/share/glance-common/glance-registry.conf glance_store filesystem_store_datadir /var/lib/glance/images
	# Do not percist build time CPU numbres. See https://bugs.debian.org/807475
	sed -i 's|^[ \t#]*workers[ \t]*=[ \t].*|#workers = 2|' $(CURDIR)/debian/glance-common/usr/share/glance-common/glance-registry.conf

	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python2.7/dist-packages oslo-config-generator \
		--output-file $(CURDIR)/debian/glance-common/usr/share/glance-common/glance-cache.conf \
		--namespace glance.cache \
		--namespace oslo.log \
		--namespace oslo.policy
	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python2.7/dist-packages oslo-config-generator \
		--output-file $(CURDIR)/debian/glance-common/usr/share/glance-common/glance-glare.conf \
		--namespace glance.glare \
		--namespace glance.store \
		--namespace oslo.db \
		--namespace oslo.db.concurrency \
		--namespace keystonemiddleware.auth_token \
		--namespace oslo.log \
		--namespace oslo.middleware.cors
	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python2.7/dist-packages oslo-config-generator \
		--output-file $(CURDIR)/debian/glance-common/usr/share/glance-common/glance-manage.conf \
		--namespace glance.manage \
		--namespace oslo.db \
		--namespace oslo.db.concurrency \
		--namespace oslo.log
	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python2.7/dist-packages oslo-config-generator \
		--output-file $(CURDIR)/debian/glance-common/usr/share/glance-common/glance-scrubber.conf \
		--namespace glance.scrubber \
		--namespace oslo.concurrency \
		--namespace oslo.db \
		--namespace oslo.db.concurrency \
		--namespace oslo.log \
		--namespace oslo.policy
	install -D -m 0664 etc/glance-swift.conf.sample $(CURDIR)/debian/glance-common/usr/share/glance-common/glance-swift.conf
	install -D -m 0664 etc/ovf-metadata.json.sample $(CURDIR)/debian/glance-common/usr/share/glance-common/ovf-metadata.json
	install -D -m 0664 etc/property-protections-policies.conf.sample $(CURDIR)/debian/glance-common/usr/share/glance-common/property-protections-policies.conf
	install -D -m 0664 etc/property-protections-roles.conf.sample $(CURDIR)/debian/glance-common/usr/share/glance-common/property-protections-roles.conf

override_dh_auto_clean:
	dh_auto_clean -O--buildsystem=python_distutils
	find . -iname '*.pyc' -delete
	rm -rf doc/build
	rm -rf glance.sqlite
	rm -f debian/glance-common.config debian/glance-common.postinst
	rm -f debian/*.init debian/*.upstart debian/*.service
	rm -rf build
