#!/usr/bin/make -f

PYTHONS:=$(shell pyversions -vr)

include /usr/share/openstack-pkg-tools/pkgos.make
export OSLO_PACKAGE_VERSION=$(VERSION)

%:
	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 -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 -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 file.
	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

	# Copy the alembic migration scripts which aren't copied
	# by python setup.py
	set -e ; for pyvers in $(PYTHONS); do \
		cp -rfv $(CURDIR)/sahara/db/migration/alembic_migrations $(CURDIR)/debian/python-sahara/usr/lib/python$$pyvers/dist-packages/sahara/db/migration/ ; \
	done

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