#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
#
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.
#
# Modified to make a template file for a multi-binary package with separated
# build-arch and build-indep targets  by Bill Allombert 2001
#export DH_VERBOSE=1

PACKAGE=openvswitch
PACKAGE_DKMS=openvswitch-datapath-dkms
include /usr/share/dpkg/pkg-info.mk

HAS_PYTHON2_DEB = $(shell if [ "`dpkg-query -W -f '$${Version}\n' python2 2>/dev/null`" ]; then echo 1; else echo 0; fi)

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

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

override_dh_autoreconf:
	dh_autoreconf --as-needed

WITH_DPDK=$(shell dirname /opt/mellanox/dpdk/share/dpdk/*/.config)
override_dh_auto_configure:
	dh_auto_configure -- --enable-ssl \
		--enable-shared --disable-static \
		--with-dpdk=$(WITH_DPDK) \
		$(DATAPATH_CONFIGURE_OPTS)

override_dh_auto_test:

override_dh_auto_build:
	dh_auto_build -- dist distdir=openvswitch

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

override_dh_install-arch:
	dh_install
	# openvswitch-switch
	mkdir -p debian/openvswitch-switch/usr/share/openvswitch/switch
	cp debian/openvswitch-switch.template debian/openvswitch-switch/usr/share/openvswitch/switch/default.template

	# ovn-host
	mkdir -p debian/ovn-host/usr/share/ovn/host
	cp debian/ovn-host.template debian/ovn-host/usr/share/ovn/host/default.template

	# ovn-central
	mkdir -p debian/ovn-central/usr/share/ovn/central
	cp debian/ovn-central.template debian/ovn-central/usr/share/ovn/central/default.template

override_dh_install-indep:
	dh_install

	# openvswitch-datapath-source
	mkdir -p debian/openvswitch-datapath-source/usr/src/modules/openvswitch-datapath/debian
	cp debian/rules.modules debian/openvswitch-datapath-source/usr/src/modules/openvswitch-datapath/debian/rules
	chmod 755 debian/openvswitch-datapath-source/usr/src/modules/openvswitch-datapath/debian/rules
	cd debian/openvswitch-datapath-source/usr/src && tar -c modules | bzip2 -9 > openvswitch-datapath.tar.bz2 && rm -rf modules

	# openvswitch-datapath-dkms
	# setup the dirs
	dh_installdirs -p$(PACKAGE_DKMS) usr/src/$(PACKAGE)-$(DEB_VERSION_UPSTREAM)

	# copy the source
	cd debian/$(PACKAGE_DKMS)/usr/src/$(PACKAGE)-$(DEB_VERSION_UPSTREAM) && tar xvzf $(CURDIR)/openvswitch.tar.gz && mv openvswitch/* openvswitch/.[a-z]* . && rmdir openvswitch

	# check we can get kernel module names
	$(MAKE) -C datapath print-build-modules

	# Prepare dkms.conf from the dkms.conf.in template
	sed "s/__VERSION__/$(DEB_VERSION_UPSTREAM)/g; s/__MODULES__/$(shell $(MAKE) -C datapath print-build-modules | grep -v make)/" debian/dkms.conf.in > debian/$(PACKAGE_DKMS)/usr/src/$(PACKAGE)-$(DEB_VERSION_UPSTREAM)/dkms.conf

	# We don't need the debian folder in there, just upstream sources...
	rm -rf debian/$(PACKAGE_DKMS)/usr/src/$(PACKAGE)-$(DEB_VERSION_UPSTREAM)/debian
	# We don't need the rhel stuff in there either
	rm -rf debian/$(PACKAGE_DKMS)/usr/src/$(PACKAGE)-$(DEB_VERSION_UPSTREAM)/rhel
	# And we should also clean useless license files, which are already
	# described in our debian/copyright anyway.
	rm -f debian/$(PACKAGE_DKMS)/usr/src/$(PACKAGE)-$(DEB_VERSION_UPSTREAM)/xenserver/LICENSE

override_dh_shlibdebs:
	dh_shlibdeps $@ -l$(WITH_DPDK)/lib

override_dh_installinit:
	dh_installinit -R

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

override_dh_usrlocal:

override_dh_installman:
	dh_installman --language=C

ifeq ($(HAS_PYTHON2_DEB),1)
override_dh_gencontrol:
	dh_gencontrol
	sed -i '/^Depends:/s/python\([ ,]\)/python2\1/' debian/*/DEBIAN/control
endif
