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

%:
	dh $@ --with autoreconf,python2,systemd --parallel

# use --as-needed only if supported by dh-autoreconf (to simplify backporting)
DH_AS_NEEDED=$(shell dpkg --compare-versions $$(dpkg --status dh-autoreconf | grep Version | cut -d' ' -f2) ge 6 && echo --as-needed)
override_dh_autoreconf:
	dh_autoreconf $(DH_AS_NEEDED)

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

override_dh_auto_configure:
	dh_auto_configure -- --enable-ssl --with-dpdk=/usr $(DATAPATH_CONFIGURE_OPTS)

override_dh_auto_test:
# NOTE(jamespage): skip tests on powerpc until
# https://bugs.launchpad.net/ubuntu/+source/openvswitch/+bug/1483572
# is resolved
ifneq (powerpc, $(DEB_BUILD_ARCH))
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	if $(MAKE) check TESTSUITEFLAGS='$(PARALLEL)' || \
                $(MAKE) check TESTSUITEFLAGS='--recheck'; then :; \
	else \
		cat tests/testsuite.log; \
		exit 1; \
	fi
endif
endif # powerpc

override_dh_auto_build:
	dh_auto_build -- distdir=openvswitch

override_dh_auto_clean:
	rm -f python/ovs/*.pyc python/ovs/db/*.pyc
	dh_auto_clean

override_dh_install:
	install -D vswitchd/ovs-vswitchd \
		$(CURDIR)/debian/openvswitch-switch-dpdk/usr/lib/openvswitch-switch-dpdk/ovs-vswitchd-dpdk

override_dh_installinit:
	:

override_dh_auto_install:
	:

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

get-orig-snapshot:
	rm -Rf openvswitch-upstream
	git clone https://github.com/openvswitch/ovs openvswitch-upstream
	cd openvswitch-upstream && \
		git checkout -b $(BRANCH) remotes/origin/$(BRANCH) && \
		export COMMIT=`git rev-parse --short HEAD` && \
		export UPSTREAM_VERSION=`head -1 NEWS | awk '{ print $$1 }' | sed 's/v//'` && \
		git archive --format tgz --prefix=openvswitch-$$UPSTREAM_VERSION+git$(DATE)/ \
			-o ../../openvswitch_$$UPSTREAM_VERSION~git$(DATE).$$COMMIT.orig.tar.gz $(BRANCH)
	rm -Rf openvswitch-upstream
