#!/usr/bin/make -f
#export DH_VERBOSE = 1

# Canonical OpenStack debian/rules template for Neutron plugins

export PYBUILD_NAME = networking_bagpipe

# -e: Fail build if any command fails, not just the last call
SHELL := /bin/sh -e

UPSTREAM_GIT := https://opendev.org/openstack/networking-bagpipe.git

DEBVERS ?= $(shell dpkg-parsechangelog -SVersion)
VERSION ?= $(shell echo '$(DEBVERS)' | sed -e 's/^[[:digit:]]*://' -e 's/[-].*//' -e 's/[+].*//' -e 's/~git.*//g' -e 's/+git.*//g' -e 's/~/./g')
# pbr's only version source: clean drops PKG-INFO and a .dsc has no .git.
export PBR_VERSION = $(VERSION)

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

# Whole-word match; also honors the nodoc/nocheck build profiles.
NODOC   := $(filter nodoc nodocs,$(DEB_BUILD_OPTIONS) $(DEB_BUILD_PROFILES))
NOCHECK := $(filter nocheck,$(DEB_BUILD_OPTIONS) $(DEB_BUILD_PROFILES))

DOCPKG := python-networking-bagpipe-doc

# Package-specific: these come out [DEFAULT]-only because upstream
# asks for namespaces its own setup.cfg does not register.
CFG_EMPTY_OK := dataplane-evpn-linux-vxlan.conf neutron-sfc.conf

%:
	dh $@ --buildsystem=pybuild --with python3,sphinxdoc

override_dh_auto_clean:
	dh_auto_clean
	rm -rf build .pybuild .stestr .testrepository .eggs
	find . -type d -name __pycache__ -prune -exec rm -rf {} +

	# Remove the generated configs only: etc/neutron may also hold
	# files shipped by upstream.
	for file in etc/oslo-config-generator/* ; do \
		[ -e "$$file" ] || continue ; \
		rm -f etc/neutron/`basename $$file` ; \
	done

	# doc/source/conf.py runs the config and policy sample generators,
	# which write into the source tree.
	rm -rf doc/build
	rm -f doc/source/_static/*.policy.yaml.sample
	rm -f doc/source/_static/config-samples/*.sample
	rm -f etc/policy.yaml.sample etc/*.conf.sample

override_dh_auto_build:
	# Regenerates networking_bagpipe.egg-info, which dh_auto_clean
	# removed. Otherwise entry points vanish and generators create empty
	# files with exit 0
	dh_auto_build

override_dh_auto_test:
ifeq (,$(NOCHECK))
	pkgos-dh_auto_test --no-py2
endif

override_dh_auto_install:
	pkgos-dh_auto_install --no-py2

	# pyproject.toml declares data-files with a relative key, which
	# installs under /usr/etc. The real copy reaches /etc via the
	# -common package, so drop the stray duplicate.
	rm -rf $(CURDIR)/debian/python3-networking-bagpipe/usr/etc

override_dh_install:
	# Generate the configs into the source tree; the -common package
	# ships /etc/neutron from there.
	mkdir -p etc/neutron
	for file in etc/oslo-config-generator/* ; do \
		[ -e "$$file" ] || continue ; \
		out=etc/neutron/`basename $$file` ; \
		PYTHONPATH=$(CURDIR) oslo-config-generator \
			--config-file=$$file \
			--output-file=$$out ; \
		if echo " $(CFG_EMPTY_OK) " | grep -q " `basename $$file` " ; then continue ; fi ; \
		grep -qE '^\#?[a-z_0-9]+ = ' $$out \
			|| { echo "ERROR: $$out has no options" >&2 ; exit 1 ; } ; \
	done

	dh_install

override_dh_sphinxdoc:
ifeq (,$(NODOC))
	PYTHONPATH=$(CURDIR) python3 -m sphinx -b html doc/source \
		$(CURDIR)/debian/$(DOCPKG)/usr/share/doc/$(DOCPKG)/html
	dh_sphinxdoc -p$(DOCPKG)
endif

override_dh_python3:
	dh_python3 --shebang=/usr/bin/python3
