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

# Canonical OpenStack debian/rules template for Neutron plugins

export PYBUILD_NAME = neutron_fwaas

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

UPSTREAM_GIT := https://opendev.org/openstack/neutron-fwaas.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-neutron-fwaas-doc

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

override_dh_auto_clean:
	# A-clean: no dh_auto_clean, so the tarball neutron_fwaas.egg-info
	# survives as the entry-point anchor. Also required because
	# pyproject.toml uses PEP 639 `license = "Apache-2.0"`,
	# unparsable by setuptools < 77.
	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

	# Package-specific
	# sphinxcontrib.apidoc generates into the tracked contributor
	# tree, so only the generated api/ subdir may go.
	rm -rf doc/source/contributor/api

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-neutron-fwaas/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 ; \
		grep -qE '^\#?[a-z_0-9]+ = ' $$out \
			|| { echo "ERROR: $$out has no options" >&2 ; exit 1 ; } ; \
	done

	# Upstream defaults leave this unusable. pkgos-fix-config-default
	# returns silently if a directive is absent, hence each assert.
	pkgos-fix-config-default etc/neutron/fwaas_driver.ini fwaas driver iptables_v2
	grep -q '^driver = iptables_v2$$' etc/neutron/fwaas_driver.ini
	pkgos-fix-config-default etc/neutron/fwaas_driver.ini fwaas enabled True
	grep -q '^enabled = True$$' etc/neutron/fwaas_driver.ini
	pkgos-fix-config-default etc/neutron/fwaas_driver.ini fwaas conntrack_driver netlink_conntrack
	grep -q '^conntrack_driver = netlink_conntrack$$' etc/neutron/fwaas_driver.ini
	pkgos-fix-config-default etc/neutron/fwaas_driver.ini fwaas firewall_l2_driver ovs
	grep -q '^firewall_l2_driver = ovs$$' etc/neutron/fwaas_driver.ini
	pkgos-fix-config-default etc/neutron/neutron_fwaas.conf service_providers service_provider FIREWALL_V2:fwaas_db:neutron_fwaas.services.firewall.service_drivers.agents.agents.FirewallAgentDriver:default
	grep -q '^service_provider = FIREWALL_V2:' etc/neutron/neutron_fwaas.conf

	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

override_dh_fixperms:
	# Package-specific
	# Upstream ships executable __init__.py files.
	find debian/python3-neutron-fwaas -name __init__.py -exec chmod a-x {} \;
	dh_fixperms
