#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/pkg-info.mk

FENCE_AGENTS_EXTRA_DIR="$(CURDIR)/debian/fence-agents-extra"
FENCE_AGENTS_COMMON_DIR="$(CURDIR)/debian/fence-agents-common"

%:
	dh $@ --with python3

override_dh_autoreconf:
	# fix version reported by agents
	echo $(DEB_VERSION_UPSTREAM) > .tarball-version
	dh_autoreconf ./autogen.sh

override_dh_auto_configure:
	dh_auto_configure -- \
		--libexecdir=/usr/libexec/fence-agents \
		PING_CMD=/bin/ping \
		PING6_CMD=/bin/ping6 \
		PING4_CMD="/bin/ping -4" \
		PYTHON=/usr/bin/python3 \
		SBD_PATH=/usr/sbin/sbd \
		VGS_PATH=/sbin/vgs

override_dh_auto_build:
	LC_ALL=C dh_auto_build

override_dh_auto_install:
	$(MAKE) install DESTDIR=$(FENCE_AGENTS_EXTRA_DIR)

override_dh_install:
	rm -v $(FENCE_AGENTS_EXTRA_DIR)/usr/share/doc/fence-agents/COPY*
	rm -v $(FENCE_AGENTS_EXTRA_DIR)/usr/share/doc/fence-agents/README.licence
	rmdir $(FENCE_AGENTS_EXTRA_DIR)/usr/share/doc/fence-agents
ifneq ($(shell dpkg-architecture -qDEB_HOST_ARCH),s390x)
	# remove fence_zvm because it doesn't do anything except on 390x
	rm -v $(FENCE_AGENTS_EXTRA_DIR)/usr/sbin/fence_zvm
	rm -v $(FENCE_AGENTS_EXTRA_DIR)/usr/share/man/man8/fence_zvm.8
endif

	# Move the curated agents to the right binary package
	debian/install-curated-agents

	# Move the common python code to a separate binary package to be used
	# by -base and -extra.
	mkdir -vp $(FENCE_AGENTS_COMMON_DIR)/usr/share/fence
	mv -v $(FENCE_AGENTS_EXTRA_DIR)/usr/share/fence/*.py \
	      $(FENCE_AGENTS_COMMON_DIR)/usr/share/fence/

	# Move var directory to fence-agents-common
	mv -v $(FENCE_AGENTS_EXTRA_DIR)/var \
	      $(FENCE_AGENTS_COMMON_DIR)
	# /var/run/... is temporary and recreated on demand by the agents
	rm -vrf $(FENCE_AGENTS_COMMON_DIR)/var/run

	# Move pkgconfig directory to fence-agents-common
	mv -v $(FENCE_AGENTS_EXTRA_DIR)/usr/share/pkgconfig \
	      $(FENCE_AGENTS_COMMON_DIR)/usr/share/

	dh_install
	# test agents (skip ack_manual, it doesn't have --help, skip vmware_helper, it wants VIRuntime.pm)
	# we test in tmp after install so the .pyc files generated don't make it into the package
	for agent in $(CURDIR)/debian/fence-agents*/usr/sbin/*; do \
		case $${agent##*/} in fence_ack_manual|fence_vmware_helper) continue;; esac; \
		echo "Testing $$agent"; \
		PYTHONPATH=$(FENCE_AGENTS_COMMON_DIR)/usr/share/fence $$agent --help >/dev/null || exit; \
	done

# tests are slow and require network access so moved to debian/tests/{delay,metadata}
override_dh_auto_test:

override_dh_python3:
	dh_python3
	dh_python3 /usr/share/fence

override_dh_gencontrol:
	# extract agent descriptions for use in debian/control
	debian/extract-agents-description
	dh_gencontrol
