#!/usr/bin/make -f

# Verbose mode
#export DH_VERBOSE=1

DEBVERS ?= $(shell dpkg-parsechangelog | sed -n -e 's/^Version: //p')
VERSION ?= $(shell echo '$(DEBVERS)' | sed -e 's/^[[:digit:]]*://' -e 's/[-].*//' -e 's/~/./g')
export OSLO_PACKAGE_VERSION=$(VERSION)
export PBR_VERSION=$(VERSION)


include /usr/share/openstack-pkg-tools/pkgos.make

%:
	dh $@ --with python2,python3,systemd

override_dh_auto_install:
	echo "Do nothing..."

override_dh_install:
	pkgos-dh_auto_install
	rm -rf $(CURDIR)/debian/python-glance/usr/etc
	rm -rf $(CURDIR)/debian/python3-glance/usr/etc
	dh_install --fail-missing -Xrootwrap.conf

	crudini --merge $(CURDIR)/debian/glance-common/etc/glance/glance-api.conf \
		< $(CURDIR)/debian/glance-api.conf.defaults
	crudini --merge $(CURDIR)/debian/glance-common/etc/glance/glance-registry.conf \
		< $(CURDIR)/debian/glance-registry.conf.defaults

ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
override_dh_auto_test:
	# NOTE(coreycb) Skip Py3.7 tests until they run successfully upstream:
	#   https://bugs.launchpad.net/bugs/1781617
	# pkgos-dh_auto_test "^glance.tests.unit.*"
	echo "===> Running tests"
	set -ex ; for i in 2.7 3.6 ; do \
		PYMAJOR=`echo $$i | cut -d'.' -f1` ; \
		echo "===> Testing with python$$i (python$$PYMAJOR)" ; \
		rm -rf .stestr ; \
		TEMP_REZ=`mktemp -t` ; \
		PYTHONPATH=$(CURDIR) PYTHON=python$$i python$$PYMAJOR-stestr run --subunit "^glance.tests.unit.*" | tee $$TEMP_REZ | subunit2pyunit ; \
		cat $$TEMP_REZ | subunit-filter -s --no-passthrough | subunit-stats ; \
		rm -f $$TEMP_REZ ; \
		rm -rf .stestr ; \
	done
endif

override_dh_auto_build:
	dh_auto_build
	mkdir -p doc/build/man doc/build/html
	sphinx-build -b man doc/source doc/build/man
	sphinx-build -b html doc/source doc/build/html

override_dh_auto_clean:
	dh_auto_clean
	rm -rf doc/build glance.sqlite .testrepository
	rm -f debian/*.init debian/*.service debian/*.upstart
