#!/usr/bin/make -f

PYTHONS:=$(shell pyversions -vr)

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

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_clean:
	rm -rf build
	rm -f debian/*.init debian/*.service debian/*.upstart
	rm -f debian/senlin-common.config debian/senlin-common.postinst debian/senlin-api.config debian/senlin-api.postinst
	dh_clean

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

override_dh_auto_test:
ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	@echo "===> Running tests"
	set -e ; set -x ; for i in 2.7 ; do \
		PYMAJOR=`echo $$i | cut -d'.' -f1` ; \
		echo "===> Testing with python$$i (python$$PYMAJOR)" ; \
		rm -rf .testrepository ; \
		testr-python$$PYMAJOR init ; \
		TEMP_REZ=`mktemp -t` ; \
		OS_TEST_PATH=./senlin/tests/unit PYTHONPATH=$(CURDIR) PYTHON=python$$i testr-python$$PYMAJOR run --subunit 'senlin\.tests\.unit\.(?!(.*test_common_context.TestRequestContext.test_request_context_from_dict.*|.*test_common_context.TestRequestContext.test_request_context_init.*|.*test_common_context.TestRequestContext.test_request_context_update.*))' | tee $$TEMP_REZ | subunit2pyunit ; \
		cat $$TEMP_REZ | subunit-filter -s --no-passthrough | subunit-stats ; \
		rm -f $$TEMP_REZ ; \
		testr-python$$PYMAJOR slowest ; \
	done
endif

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


override_dh_auto_install:
	rm -rf $(CURDIR)/debian/tmp
	python2.7 setup.py install --install-layout=deb --root $(CURDIR)/debian/tmp
	cp -auxf senlin/db/sqlalchemy/migrate_repo $(CURDIR)/debian/tmp/usr/lib/python2.7/dist-packages/senlin/db/sqlalchemy
	mkdir -p $(CURDIR)/debian/senlin-common/usr/share/senlin-common
	PYTHONPATH=$(CURDIR)/debian/tmp oslo-config-generator --output-file $(CURDIR)/debian/senlin-common/usr/share/senlin-common/senlin.conf \
		--wrap-width 140 \
		--namespace senlin.config \
		--namespace keystonemiddleware.auth_token \
		--namespace oslo.db \
		--namespace oslo.log \
		--namespace oslo.messaging \
		--namespace oslo.policy \
		--namespace oslo.service.periodic_task \
		--namespace oslo.service.service \
		--namespace oslo.service.sslutils
	sed -i 's/^[ \t#]*auth_protocol[ \t]*=.*/auth_protocol = http/' $(CURDIR)/debian/senlin-common/usr/share/senlin-common/senlin.conf
