#!/usr/bin/make -f

PYTHONS:=$(shell pyversions -vr)
#PYTHON3S:=$(shell py3versions -vr)

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:
	rm -rf .testrepository
	testr init
	set -e && set -x && \
		TEMP_REZ=`mktemp -t` && \
		PYTHONPATH=$(CURDIR) testr run --subunit | tee $$TEMP_REZ | subunit2pyunit || true ; \
		cat $$TEMP_REZ | subunit-filter -s --no-passthrough | subunit-stats || true ; \
		rm -f $$TEMP_REZ ;
	testr slowest
endif

override_dh_clean:
	dh_clean
	rm -rf heat.egg-info .testrepository
	rm -rf doc/build
	find . -iname '*.pyc' -delete
	rm -f run_tests.err.log
	rm -f \
		debian/heat-common.postinst \
		debian/heat-common.config
	rm -f debian/*.init debian/*.service debian/*.upstart

override_dh_install:
	dh_install
	install -D -m 0640 etc/heat/heat.conf.sample $(CURDIR)/debian/heat-common/usr/share/heat-common/heat.conf
	cat $(CURDIR)/debian/keystone_authtoken >>$(CURDIR)/debian/heat-common/usr/share/heat-common/heat.conf
	set -e && for pyvers in $(PYTHONS); do \
		mkdir -p $(CURDIR)/debian/python-heat/usr/lib/python$$pyvers/dist-packages/heat/plugins ; \
		cp -rf contrib/heat_keystoneclient_v2/heat_keystoneclient_v2 $(CURDIR)/debian/python-heat/usr/lib/python$$pyvers/dist-packages/heat/plugins ; \
        done

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

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

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