#!/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 $(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:
	dh_install --list-missing
	cp debian/openvswitch-switch.default debian/openvswitch-switch/usr/share/openvswitch/switch/default.template
	mkdir -p $(CURDIR)/debian/openvswitch-switch/usr/lib/openvswitch-switch
	mv $(CURDIR)/debian/openvswitch-switch/usr/sbin/ovs-vswitchd \
		$(CURDIR)/debian/openvswitch-switch/usr/lib/openvswitch-switch/ovs-vswitchd

override_dh_installinit:
	dh_installinit -R
	dh_installinit -popenvswitch-switch --name=openvswitch-nonetwork --no-start
	dh_installinit -popenvswitch-switch --name=openvswitch-force-reload-kmod --no-start

override_dh_strip:
	dh_strip --dbg-package=openvswitch-dbg

# 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
