#!/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:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	if $(MAKE) check TESTSUITEFLAGS='$(PARALLEL) 1-710 714-' ||\
                $(MAKE) check TESTSUITEFLAGS='--recheck'; then :; \
	else \
		cat tests/testsuite.log; \
		exit 1; \
	fi
endif

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
	cp debian/openvswitch-switch.default debian/openvswitch-switch/usr/share/openvswitch/switch/default.template

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.3

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 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).orig.tar.gz $(BRANCH)
	rm -Rf openvswitch-upstream
