#!/usr/bin/make -f
# -*- makefile -*-
#export DH_VERBOSE=1
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CFLAGS_MAINT_APPEND = -fPIC

# Files are automatically restored after binary build and during clean phase

%:
	dh $@
	@# Auto-restore control file after package building completes
	@if [ "$@" = "binary" ] || [ "$@" = "binary-arch" ] || [ "$@" = "binary-indep" ]; then \
		if [ -f debian/control.orig ]; then \
			cp debian/control.orig debian/control; \
			echo "Auto-restored debian/control after binary build"; \
		fi; \
		if [ -f debian/doca-openvswitch-source.install.orig ]; then \
			mv debian/doca-openvswitch-source.install.orig \
			   debian/doca-openvswitch-source.install; \
			echo "Auto-restored install file"; \
		fi; \
	fi

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
PARALLEL = -j$(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
else
PARALLEL =
endif

ifneq (,$(filter shared,$(DEB_BUILD_OPTIONS)))
BUILD_TYPE := --enable-shared --disable-static
EXCLUDE := --exclude .a
else
BUILD_TYPE := --disable-shared --enable-static
EXCLUDE := --exclude .so.* --exclude .so
endif

ifneq (,$(filter with-dpdk,$(DEB_BUILD_OPTIONS)))
DPDK_INSTALL ?= /opt/mellanox/dpdk
ifneq (,$(shell readlink -e $(DPDK_INSTALL)))
WITH_DPDK := --with-dpdk=$(DPDK_INSTALL)
DPDK_PKGCONFIG := $(shell find $(DPDK_INSTALL) -type f -name libdpdk.pc -exec dirname {} \;)
DPDK_LIB_DIR := $(shell PKG_CONFIG_PATH=$(DPDK_PKGCONFIG) pkg-config --libs-only-L libdpdk)
endif
else
WITH_DPDK =
endif

ifneq (,$(filter with-doca,$(DEB_BUILD_OPTIONS)))
# WITH_DOCA is set if the user provided 'with-doca' and DOCA_INSTALL exists,
# either it's default or the user provided value
DOCA_INSTALL ?= /opt/mellanox/doca
ifneq (,$(shell readlink -e $(DOCA_INSTALL)))
WITH_DOCA := --with-doca=$(DOCA_INSTALL)
DOCA_PKGCONFIG := $(shell find $(DOCA_INSTALL) -type f -name doca-flow.pc -exec dirname {} \;)
DOCA_LIB_DIR := $(shell PKG_CONFIG_PATH=$(DOCA_PKGCONFIG) pkg-config --libs-only-L doca-flow)
endif
PKG_PREFIX := doca-
else
WITH_DOCA =
PKG_PREFIX :=
endif

PYTHON3S:=$(shell py3versions -vr)
DEB_HOST_ARCH?=$(shell dpkg-architecture -qDEB_HOST_ARCH)

override_dh_auto_configure:
	# Restore original control file if it exists from a previous build
	if [ -f debian/control.orig ]; then \
		mv debian/control.orig debian/control; \
	fi
	# Keep original control file
	cp debian/control debian/control.orig
	# Backup the source install file if it exists
	if [ -f debian/doca-openvswitch-source.install ]; then \
		cp debian/doca-openvswitch-source.install \
		   debian/doca-openvswitch-source.install.orig; \
	fi
ifeq (,$(filter with-doca,$(DEB_BUILD_OPTIONS)))
	# Remove doca- prefix from package names and dependencies when not building with doca
	# Skip the Source: line to preserve source package name
	sed -i -e '/^Source:/!s/doca-openvswitch/openvswitch/g' debian/control
endif

	test -d _debian || mkdir _debian
	cd _debian && ( \
		test -e Makefile || \
		../configure --prefix=/usr --localstatedir=/var \
					--enable-ssl \
					--disable-afxdp \
					--sysconfdir=/etc \
					$(BUILD_TYPE) \
					$(WITH_DPDK) \
					$(WITH_DOCA) \
					$(DATAPATH_CONFIGURE_OPTS) \
					$(EXTRA_CONFIGURE_OPTS) \
					)


# NOTE(jamespage): by default, just run all tests
TEST_LIST =
TEST_LIST_DPDK =

# armhf:
#  30: bfd - check that BFD works together with RSTP   FAILED (bfd.at:829)
ifneq (,$(filter armhf, $(DEB_HOST_ARCH)))
TEST_LIST = 1-29 31-
TEST_LIST_DPDK = $(TEST_LIST)
endif # armhf

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	if $(MAKE) -C _debian check TESTSUITEFLAGS='$(PARALLEL) $(TEST_LIST)' || \
                $(MAKE) -C _debian check TESTSUITEFLAGS='--recheck'; then :; \
	else \
		cat _debian/tests/testsuite.log; \
		exit 1; \
	fi
# Skip DPDK testing on arm64 as builders don't have crc32 support
# which is used in aarch64 based crc optimization in ovs >= 2.12.0~
ifneq (,$(filter i386 amd64 ppc64el, $(DEB_HOST_ARCH)))
ifeq (,$(filter nodpdk, $(DEB_BUILD_OPTIONS)))
	if $(MAKE) -C _dpdk check TESTSUITEFLAGS='$(PARALLEL) $(TEST_LIST_DPDK)' || \
				$(MAKE) -C _dpdk check TESTSUITEFLAGS='--recheck'; then :; \
	else \
		cat _dpdk/tests/testsuite.log; \
		exit 1; \
	fi
endif # nodpdk
endif # i386/amd64/ppc64el
endif # nocheck

override_dh_auto_build:
	dh_auto_build --sourcedirectory=_debian -- dist distdir=$(PKG_PREFIX)openvswitch
	dh_auto_build --sourcedirectory=_debian
ifneq (,$(filter with-doca,$(DEB_BUILD_OPTIONS)))
	# When building with DOCA, update install file to look for doca-openvswitch.tar.gz
	if [ -f debian/doca-openvswitch-source.install ]; then \
		sed -i "s/_debian\/.*openvswitch\.tar\.gz/_debian\/doca-openvswitch.tar.gz/" \
			debian/doca-openvswitch-source.install; \
	fi
endif
	# Control file restoration happens only during clean phase

override_dh_clean:
	find . -name "*.pyc" -delete
	# Clean up symlinks and files created for non-doca builds
	cd debian && find . -maxdepth 1 -type l -name 'openvswitch-*' -delete
	cd debian && find . -maxdepth 1 -type l -name 'python3-openvswitch*' -delete
	# Remove modified .install files (they will be regenerated)
	cd debian && for f in openvswitch-*.install python3-openvswitch*.install; do \
		if [ -f "$$f" ] && [ ! -L "$$f" ]; then \
			rm -f "$$f"; \
		fi; \
	done
	# Always restore files before cleaning
	if [ -f debian/control.orig ]; then \
		cp debian/control.orig debian/control; \
	fi
	if [ -f debian/doca-openvswitch-source.install.orig ]; then \
		mv debian/doca-openvswitch-source.install.orig \
		   debian/doca-openvswitch-source.install; \
	fi
	dh_clean
	# This is a backup - should not be needed if override_dh_clean works
	if [ -f debian/control.orig ]; then \
		mv debian/control.orig debian/control; \
	fi
	if [ -f debian/doca-openvswitch-source.install.orig ]; then \
		mv debian/doca-openvswitch-source.install.orig \
		   debian/doca-openvswitch-source.install; \
	fi

override_dh_install:
ifeq (,$(filter with-doca,$(DEB_BUILD_OPTIONS)))
	# Create symlinks for all doca-openvswitch-* files to openvswitch-* equivalents
	set -e; cd debian && for f in doca-openvswitch-*; do \
		target=$$(echo $$f | sed 's/doca-openvswitch-/openvswitch-/'); \
		if [ ! -e "$$target" ] && [ -f "$$f" ]; then \
			ln -sf "$$f" "$$target"; \
		fi; \
	done
	# Special case for python3 package
	set -e; cd debian && for f in python3-doca-openvswitch*; do \
		if [ -f "$$f" ]; then \
			target=$$(echo $$f | sed 's/python3-doca-openvswitch/python3-openvswitch/'); \
			if [ ! -e "$$target" ]; then \
				ln -sf "$$f" "$$target"; \
			fi; \
		fi; \
	done
	# Update paths in symlinked .install files
	set -e; cd debian && for f in openvswitch-*.install; do \
		if [ -L "$$f" ]; then \
			cp -L "$$f" "$$f.tmp" && \
			sed -i 's/doca-openvswitch/openvswitch/g' "$$f.tmp" && \
			sed -i '/ovs-doca-tcpdump/d' "$$f.tmp" && \
			mv "$$f.tmp" "$$f"; \
		fi; \
	done
endif
	dh_install $(EXCLUDE)
	set -e && for pyvers in $(PYTHON3S); do \
		cd python; \
		export PKG_CONFIG_PATH=$(CURDIR)/debian/tmp/usr/lib/pkgconfig; \
		export PKG_CONFIG_SYSROOT_DIR=$(CURDIR)/debian/tmp; \
		export PKG_CONFIG_SYSTEM_INCLUDE_PATH=/; \
		export PKG_CONFIG_SYSTEM_LIBRARY_PATH=/; \
		enable_shared=no \
		extra_cflags="`pkg-config --cflags libopenvswitch`" \
		extra_libs="-Wl,-Bstatic -lopenvswitch -Wl,-Bdynamic `pkg-config --libs --static libopenvswitch`" \
		python$$pyvers setup.py install --install-layout=deb \
			--root $(CURDIR)/debian/python3-$(PKG_PREFIX)openvswitch; \
		cd ..; \
		mkdir -p $(CURDIR)/debian/$(PKG_PREFIX)openvswitch-test/usr/lib/python$$pyvers/dist-packages/ovstest; \
		install -v -D python/ovstest/*.py \
			$(CURDIR)/debian/$(PKG_PREFIX)openvswitch-test/usr/lib/python$$pyvers/dist-packages/ovstest; \
	done

override_dh_auto_install:
	dh_auto_install --sourcedirectory=_debian


override_dh_installinit:
	dh_installinit --restart-after-upgrade
	dh_installinit -p$(PKG_PREFIX)openvswitch-switch --name=ovsdb-server --no-start
	dh_installinit -p$(PKG_PREFIX)openvswitch-switch --name=ovs-vswitchd --no-start
	dh_installinit -p$(PKG_PREFIX)openvswitch-switch --name=ovs-exporter --no-start --no-enable
	dh_installinit -p$(PKG_PREFIX)openvswitch-switch --name=ovs-record-hostname --no-start
	dh_installinit -p$(PKG_PREFIX)openvswitch-switch --name=openvswitch-switch --no-start
	dh_installinit -p$(PKG_PREFIX)openvswitch-ipsec --name=openvswitch-ipsec --no-start
	dh_installinit -p$(PKG_PREFIX)openvswitch-testcontroller --name=openvswitch-testcontroller --no-start

override_dh_installsystemd:
	dh_installsystemd -p$(PKG_PREFIX)openvswitch-switch -Xovsdb-server.service --name=ovsdb-server --no-start
	dh_installsystemd -p$(PKG_PREFIX)openvswitch-switch -Xovs-vswitchd.service --name=ovs-vswitchd --no-start
	dh_installsystemd -p$(PKG_PREFIX)openvswitch-switch --name=ovs-exporter --no-start --no-enable
	dh_installsystemd -p$(PKG_PREFIX)openvswitch-switch -Xovs-record-hostname.service --name=ovs-record-hostname --no-start
	dh_installsystemd -p$(PKG_PREFIX)openvswitch-switch -Xopenvswitch-switch.service --name=openvswitch-switch --no-start
	dh_installsystemd -p$(PKG_PREFIX)openvswitch-ipsec --name=openvswitch-ipsec --no-start
	dh_installsystemd -p$(PKG_PREFIX)openvswitch-testcontroller --name=openvswitch-testcontroller --no-start
	dh_installsystemd --restart-after-upgrade \
		-Xopenvswitch-switch.service \
		-Xovs-vswitchd.service \
		-Xovsdb-server.service \
		-Xovs-record-hostname.service

override_dh_strip:
	dh_strip --dbgsym-migration='openvswitch-dbg (<< 2.17~)'

override_dh_python3:
	DEB_HOST_ARCH=$(DEB_HOST_ARCH) dh_python3 --shebang=/usr/bin/python3

# This override is needed because collectx-clxapi package is missing metadata.
# Once this package is fixed or we move to DOCA-integrated packages, remove this.
override_dh_shlibdeps:
	dh_shlibdeps --dpkg-shlibdeps-params=--ignore-missing-info

# Helper target for creating snapshots from upstream git
DATE=$(shell date +%Y%m%d)
# Upstream branch to track
BRANCH=branch-3.3
VERSION=3.3.0

get-orig-snapshot:
	rm -Rf openvswitch-upstream
	git clone --branch $(BRANCH) --depth 1 https://github.com/openvswitch/ovs openvswitch-upstream
	cd openvswitch-upstream && \
		export COMMIT=`git rev-parse --short HEAD` && \
		git archive --format tgz --prefix=openvswitch-$(VERSION)~git$(DATE).$$COMMIT/ \
			-o ../../openvswitch_$(VERSION)~git$(DATE).$$COMMIT.orig.tar.gz $(BRANCH)
	rm -Rf openvswitch-upstream
