#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1

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)

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

override_dh_auto_build:
	dh_auto_build -O--buildsystem=python_distutils
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func manila-common.postinst

override_dh_auto_install:
	dh_auto_install -O--buildsystem=python_distutils

	# Generate the config file
	mkdir -p $(CURDIR)/debian/manila-common/usr/share/manila-common
	oslo-config-generator --config-file etc/oslo-config-generator/manila.conf \
		--output-file $(CURDIR)/debian/manila-common/usr/share/manila-common/manila.conf

	# and copy the remaining
	set -e ; for i in api-paste.ini policy.json rootwrap.conf ; do \
		cp $(CURDIR)/etc/manila/$$i $(CURDIR)/debian/manila-common/usr/share/manila-common ; \
	done
	cp $(CURDIR)/etc/manila/logging_sample.conf $(CURDIR)/debian/manila-common/usr/share/manila-common/logging.conf
	cp -rfv $(CURDIR)/etc/manila/rootwrap.d $(CURDIR)/debian/manila-common/usr/share/manila-common

override_dh_auto_clean:
	dh_auto_clean
	rm -f etc/manila/manila.conf
	rm -f debian/*.service debian/*.upstart debian/*.init
	rm -f debian/manila-common.postinst

override_dh_install:
	dh_install --fail-missing

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` ; \
		PYTHONPATH=$(CURDIR) PYTHON=python$$i testr-python$$PYMAJOR run --subunit | 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)))
	sphinx-build -b html doc/source $(CURDIR)/debian/manila-doc/usr/share/doc/manila-doc/html
	dh_sphinxdoc -O--buildsystem=python_distutils
endif
