#!/usr/bin/make -f

PYTHONS:=$(shell pyversions -vr)

include /usr/share/openstack-pkg-tools/pkgos.make
export OSLO_PACKAGE_VERSION=$(shell dpkg-parsechangelog -S Version | sed -e 's/^[[:digit:]]*://' -e 's/[-].*//' -e 's/~/.0/')

LAST_CHANGE = $(shell dpkg-parsechangelog -S Date)
BUILD_DATE  = $(shell LC_ALL=C date -u "+%B %d, %Y" -d "$(LAST_CHANGE)")
SPHINXOPTS := -D html_last_updated_fmt="$(BUILD_DATE)"

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

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

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

override_dh_installman:
ifeq (,$(findstring nodocs, $(DEB_BUILD_OPTIONS)))
	PYTHONPATH=. sphinx-build $(SPHINXOPTS) -b man doc/source doc/build/man
	dh_installman -O--buildsystem=python_distutils
endif

override_dh_auto_build:
	dh_auto_build
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func sahara-api.config
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func sahara-api.postinst
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func sahara-common.config
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func sahara-common.postinst

override_dh_install:
	dh_install -O--buildsystem=python_distutils --fail-missing

	# Install the config files.
	mkdir -p $(CURDIR)/debian/sahara-common/usr/share/sahara-common
	oslo-config-generator \
		--namespace sahara.config \
		--namespace oslo.db \
		--namespace oslo.messaging \
		--namespace oslo.concurrency \
		--namespace keystonemiddleware.auth_token \
		--namespace periodic.config \
		--namespace ssl.config \
		--namespace oslo.log \
		--namespace oslo.policy \
		--output-file $(CURDIR)/debian/sahara-common/usr/share/sahara-common/sahara.conf
	sed -i 's|#connection=<None>|connection=sqlite:///var/lib/sahara/saharadb|' $(CURDIR)/debian/sahara-common/usr/share/sahara-common/sahara.conf

	mkdir -p $(CURDIR)/debian/sahara-commom/etc/sahara
	cp etc/sahara/compute.topology.sample $(CURDIR)/debian/sahara-commom/etc/sahara/compute.topology
	cp etc/sahara/swift.topology.sample $(CURDIR)/debian/sahara-commom/etc/sahara/swift.topology
	set -e ; for i in policy.json rootwrap.conf rootwrap.d ; do \
		cp -rfv etc/sahara/$$i $(CURDIR)/debian/sahara-commom/etc/sahara ; \
	done

override_dh_clean:
	rm -rf build 	debian/sahara-common.postinst \
			debian/sahara-common.config \
			debian/sahara-api.postinst \
			debian/sahara-api.config
	rm -rf doc/build
	rm -f debian/*.init debian/*.service debian/*.upstart
	dh_clean -O--buildsystem=python_distutils
