#!/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)"

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

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

override_dh_auto_test:
	echo "Do nothing..."

override_dh_auto_install:
	echo "Do nothing..."

override_dh_auto_build:
	/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
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_postrm glance-common.postrm
	pkgos-merge-templates glance-api glance endpoint
	pkgos-merge-templates glance-common glance db rabbit ksat

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)))
	# Blacklist:
	# glance.tests.unit.v2.test_tasks_resource.TestTasksController.test_create_with_live_time https://bugs.launchpad.net/glance/+bug/1868455
	pkgos-dh_auto_test --no-py2 'glance\.tests\.unit\.(?!.*test_wsgi_ipv6\.IPv6ServerTest\.test_evnetlet_no_dnspython.*|.*async_\.flows\.test_ovf_process\.TestOvfProcessTask\.test_extract_ova_no_disk.*|.*test_tasks_resource\.TestTasksController\.test_create_with_live_time.*|.*common\.test_property_utils\.TestPropertyRulesWithRoles\.test_property_protection_with_missing_operation.*|.*common\.test_property_utils\.TestPropertyRulesWithRoles\.test_property_protection_with_misspelt_operation.*)'
endif

	# Generate all config files
	mkdir -p $(CURDIR)/debian/glance-common/usr/share/glance-common
	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python3/dist-packages oslo-config-generator \
		--output-file $(CURDIR)/debian/glance-common/usr/share/glance-common/glance-api.conf \
		--namespace castellan.config \
		--namespace glance.api \
		--namespace glance.store \
		--namespace os_brick \
		--namespace glance.multi_store \
		--namespace oslo.concurrency \
		--namespace oslo.messaging \
		--namespace oslo.db \
		--namespace oslo.policy \
		--namespace keystonemiddleware.auth_token \
		--namespace oslo.limit \
		--namespace oslo.log \
		--namespace oslo.middleware.cors \
		--namespace oslo.middleware.healthcheck \
		--namespace oslo.middleware.http_proxy_to_wsgi \
		--namespace oslo.reports

	pkgos-readd-keystone-authtoken-missing-options $(CURDIR)/debian/glance-common/usr/share/glance-common/glance-api.conf keystone_authtoken glance

	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/python3/dist-packages oslo-config-generator \
		--output-file $(CURDIR)/debian/glance-common/usr/share/glance-common/glance-cache.conf \
		--namespace glance.cache \
		--namespace glance.store \
		--namespace os_brick \
		--namespace oslo.log \
		--namespace oslo.policy
	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python3/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.log
	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python3/dist-packages oslo-config-generator \
		--output-file $(CURDIR)/debian/glance-common/usr/share/glance-common/glance-scrubber.conf \
		--namespace glance.scrubber \
		--namespace glance.store \
		--namespace os_brick \
		--namespace oslo.concurrency \
		--namespace oslo.db \
		--namespace oslo.log \
		--namespace oslo.policy
	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python3/dist-packages oslo-config-generator \
		--output-file $(CURDIR)/debian/glance-common/usr/share/glance-common/glance-image-import.conf \
		--namespace glance
	mkdir -p $(CURDIR)/debian/glance-common/etc/glance/policy.d
	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python3/dist-packages oslopolicy-sample-generator \
		--output-file=$(CURDIR)/debian/glance-common/etc/glance/policy.d/00_default_policy.yaml \
		--format yaml \
		--namespace glance

	# Comment out deprecated policies
	sed -i 's/^"/#"/' $(CURDIR)/debian/glance-common/etc/glance/policy.d/00_default_policy.yaml

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

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

	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_sphinxdoc:
ifeq (,$(findstring nodocs, $(DEB_BUILD_OPTIONS)))
	PYTHON=python3 python3 -m sphinx $(SPHINXOPTS) -b html doc/source $(CURDIR)/debian/glance-doc/usr/share/doc/glance-doc/html
	dh_sphinxdoc
endif

override_dh_installman:
ifeq (,$(findstring nodocs, $(DEB_BUILD_OPTIONS)))
	python3 -m sphinx $(SPHINXOPTS) -b man doc/source doc/build/man
	dh_installman
endif

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