#!/usr/bin/make -f

#export DH_VERBOSE=1

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

export OSLO_PACKAGE_VERSION=$(VERSION)

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

ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
override_dh_auto_test:
	./run_tests.sh -P -N || true
	#nosetests || true
endif


override_dh_install:
	dh_install -O--buildsystem=python_distutils

	# Generate the configuration files
	mkdir -p $(CURDIR)/debian/glance-common/usr/share/glance-common
	cp etc/glance-api.conf $(CURDIR)/debian/glance-common/usr/share/glance-common
#	oslo-config-generator \
#		--output-file $(CURDIR)/debian/glance-common/usr/share/glance-common/glance-api.conf \
#		--namespace glance.api \
#		--namespace glance.store \
#		--namespace oslo.messaging \
#		--namespace oslo.db \
#		--namespace oslo.db.concurrency \
#		--namespace keystonemiddleware.auth_token

	cp etc/glance-cache.conf $(CURDIR)/debian/glance-common/usr/share/glance-common
#	oslo-config-generator \
#		--output-file $(CURDIR)/debian/glance-common/usr/share/glance-common/glance-cache.conf \
#		--namespace glance.cache

	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

	cp etc/glance-registry.conf $(CURDIR)/debian/glance-common/usr/share/glance-common
#	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 keystonemiddleware.auth_token

	cp etc/glance-scrubber.conf $(CURDIR)/debian/glance-common/usr/share/glance-common
#	oslo-config-generator \
#		--output-file $(CURDIR)/debian/glance-common/usr/share/glance-common/glance-scrubber.conf \
#		--namespace glance.scrubber \
#		--namespace oslo.db \
#		--namespace oslo.db.concurrency

	mkdir -p $(CURDIR)/debian/glance-common/etc/glance
	cp etc/glance-swift.conf.sample $(CURDIR)/debian/glance-common/etc/glance/glance-swift.conf
	cp etc/logging.cnf.sample $(CURDIR)/debian/glance-common/etc/glance/logging.cnf
	cp etc/property-protections-policies.conf.sample $(CURDIR)/debian/glance-common/etc/glance/property-protections-policies.conf
	cp etc/property-protections-roles.conf.sample $(CURDIR)/debian/glance-common/etc/glance/property-protections-roles.conf
	cp -r etc/metadefs $(CURDIR)/debian/glance-common/etc/glance

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

override_dh_sphinxdoc:
ifeq (,$(findstring nodocs, $(DEB_BUILD_OPTIONS)))
	sphinx-build -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 -b man doc/source doc/build/man
	dh_installman -O--buildsystem=python_distutils
endif

override_dh_auto_configure:
	echo $(VERSION) > glance/versioninfo
	dh_auto_configure


override_dh_auto_clean:
	echo $(VERSION) > glance/versioninfo
	# dh_auto_clean
	# debconf-updatepo
	find . -iname '*.pyc' -delete
	rm -rf glance.egg-info
	rm -rf doc/build
	rm -rf glance.sqlite
	rm -f glance/versioninfo
	rm -f debian/glance-common.config debian/glance-common.postinst
	rm -f debian/*.init debian/*.upstart debian/*.service
	rm -rf build

.PHONY: override_dh_auto_clean override_dh_auto_configure override_dh_builddeb override_dh_installman override_dh_auto_build
