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

# Canonical OpenStack debian/rules template for Neutron plugins
# This package is a standalone OVN BGP daemon, not a Neutron plugin.

export PYBUILD_NAME = ovn_bgp_agent

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

UPSTREAM_GIT := https://opendev.org/openstack/ovn-bgp-agent.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-ovn-bgp-agent-doc

%:
	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 {} +

	# 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_clean:
	dh_clean

	# gen-init-configurations generates .init/.service from debian/*.init.in
	rm -f debian/*.init debian/*.service debian/*.upstart

override_dh_auto_build:
	# Regenerates ovn_bgp_agent.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))
	# Package-specific
	# Known-failing or non-unit tests excluded.
	pkgos-dh_auto_test --no-py2 'ovn_bgp_agent\.tests\.unit\.(?!utils\.test_linux_net\.TestLinuxNet\.test_add_ips_to_dev|utils\.test_linux_net\.TestLinuxNet\.test_del_ips_from_dev|utils\.test_linux_net\.TestLinuxNet\.test_delete_ip_routes)'
endif

override_dh_auto_install:
	# --in-tmp: debian/*.install distribute from debian/tmp, which
	# also makes the dh_missing check below meaningful.
	pkgos-dh_auto_install --no-py2 --in-tmp

override_dh_install:
	# Package-specific
	# No etc/oslo-config-generator/ upstream, so name the namespaces.
	mkdir -p `dirname $(CURDIR)/debian/ovn-bgp-agent/etc/ovn-bgp-agent/bgp-agent.conf`
	PYTHONPATH=$(CURDIR) oslo-config-generator \
		--output-file $(CURDIR)/debian/ovn-bgp-agent/etc/ovn-bgp-agent/bgp-agent.conf \
		--wrap-width 140 \
		--namespace ovnbgpagent
	grep -qE '^\#?[a-z_0-9]+ = ' $(CURDIR)/debian/ovn-bgp-agent/etc/ovn-bgp-agent/bgp-agent.conf

	dh_install

	# Real only because --in-tmp staged into debian/tmp.
	dh_missing --fail-missing

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
