# Contributor: Stuart Cardall <developer@it-offshore.co.uk>
# Maintainer: Stuart Cardall <developer@it-offshore.co.uk>
pkgname=openvswitch
pkgver=2.17.9
pkgrel=1
pkgdesc="production quality, multilayer virtual switch"
url="https://www.openvswitch.org/"
arch="all"
license="Apache-2.0"
makedepends="
	$depends_dev
	libcap-ng-dev
	linux-headers
	openssl-dev
	perl
	py3-twisted
	python3-dev
	"
checkdepends="py3-sortedcontainers"
subpackages="
	$pkgname-doc
	$pkgname-dbg
	$pkgname-dev
	$pkgname-bash-completion
	$pkgname-test:_test
	py3-$pkgname:_py3:noarch
	$pkgname-openrc
	"
source="https://www.openvswitch.org/releases/openvswitch-$pkgver.tar.gz
	ovsdb-server.initd
	ovsdb-server.confd
	ovs-vswitchd.initd
	ovs-vswitchd.confd
	ovs-modules.initd
	ifupdown-alpine.patch
	readme.debian.patch
	"
options="!check" # FIXME: fails 1 test

# secfixes:
#   2.17.9-r0:
#     - CVE-2023-3966
#     - CVE-2023-5366
#   2.17.8-r0:
#     - CVE-2023-1668
#   2.17.6-r0:
#     - CVE-2023-1668
#   2.17.5-r0:
#     - CVE-2022-4337
#     - CVE-2022-4338
#   2.12.3-r2:
#     - CVE-2021-36980
#   2.12.3-r0:
#     - CVE-2020-35498
#   2.12.2-r0:
#     - CVE-2020-27827
#     - CVE-2015-8011
#   2.12.1-r0:
#     - CVE-2019-14818
#     - CVE-2020-10722
#     - CVE-2020-10723
#     - CVE-2020-10724

build() {
	CFLAGS="$CFLAGS -flto=auto" \
	./configure \
		--prefix=/usr \
		--host=$CHOST \
		--build=$CBUILD \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--localstatedir=/var \
		--enable-libcapng
	make
}

check() {
	make check
}

package() {
	make DESTDIR="$pkgdir" install

	local _py3site=$(python3 -c "import site; print(site.getsitepackages()[0])")
	mkdir -p "$pkgdir/$_py3site"
	cp -a "$pkgdir"/usr/share/openvswitch/python/* "$pkgdir/$_py3site"/
	rm -rf "$pkgdir"/usr/share/openvswitch/python

	mkdir -p "$pkgdir"/usr/share/bash-completion
	mv "$pkgdir"/etc/bash_completion.d "$pkgdir"/usr/share/bash-completion/completions

	rm -f "$pkgdir"/usr/lib/*.a
	install -Dm755 "$srcdir"/ovsdb-server.initd \
		"$pkgdir"/etc/init.d/ovsdb-server
	install -Dm755 "$srcdir"/ovs-vswitchd.initd \
		"$pkgdir"/etc/init.d/ovs-vswitchd
	install -Dm755 "$srcdir"/ovs-modules.initd \
		"$pkgdir"/etc/init.d/ovs-modules
	install -Dm644 "$srcdir"/ovsdb-server.confd \
		"$pkgdir"/etc/conf.d/ovsdb-server
	install -Dm644 "$srcdir"/ovs-vswitchd.confd \
		"$pkgdir"/etc/conf.d/ovs-vswitchd

	mkdir -p "$pkgdir"/etc/network/if-post-down.d
	install -Dm755 debian/ifupdown.sh \
		"$pkgdir"/etc/network/if-pre-up.d/openvswitch
	ln -s ../if-pre-up.d/openvswitch \
		"$pkgdir"/etc/network/if-post-down.d/openvswitch
	mkdir -p "$pkgdir"/usr/share/doc/openvswitch
	install -Dm644 debian/openvswitch-switch.README.Debian \
		"$pkgdir"/usr/share/doc/openvswitch/README.alpine
}

_test() {
	pkgdesc="Open vSwitch testing utilities"
	depends="py3-$pkgname=$pkgver-r$pkgrel py3-twisted"
	amove \
		usr/bin/ovs-pcap \
		usr/bin/ovs-tcpdump \
		usr/bin/ovs-tcpundump \
		usr/bin/ovs-testcontroller \
		usr/lib/python*/site-packages/ovstest
}

_py3() {
	pkgdesc="Python modules for Open vSwitch"
	amove usr/lib/python*
}

sha512sums="
b91212f9c10a208bca9d15cd5a219ae24b7e9bf8b5c8fdebc21cd9fa8229bc655f9f3591d79dd30c1a8212daed15d5a26bf9532b3496e3660bc8b62012f45636  openvswitch-2.17.9.tar.gz
097d4721a78fff749c534910d98543778474406bd61b469b88a0d981a2a380556444437ec44278cead6d8688c45a38b2acbf1551acb9ab38d048f413728e3b88  ovsdb-server.initd
b1588d076bbfc7ef2dd46fce8e46186f40cbbc4667697f7ac13ddc68e34568fdab315fde47838de7f6d32916853190336cfe3735f672ad7cb624ae14dbff55a5  ovsdb-server.confd
4bff37f8bed32d5327b4b0433984ab8ffdce77f618034aa6ffd1ad4a7caa00703ea2bef312876dce5aafa4c17fe06dddd75e5c05c10e13bf8f5c3805a0654a96  ovs-vswitchd.initd
346aea099f51707d2b4fc9fdc8c1502582723fb4e00c4d5d1624b0378c94dfb76674fa95e2af894f36169df52109dbe441ee6a45aa744584d9e4c74d15a46c1d  ovs-vswitchd.confd
1e08aa5ac6ce55b97256478b9243c8a4c92a42a97fc70ea0439c832b12a775af28a127224ae6c4ce01642dde65f76c610a44105912338bf443d8ea390c2d9ccf  ovs-modules.initd
eca5b19954e6df7dc17c582e22e4b27533710d077039a54a2ba291ae7d3a2706872f5bcad8795ca58e06ca7e45a9b8c4c51e99aedbabd0e87623972ebcdca230  ifupdown-alpine.patch
177f8c98cfa261b440c398d3a3be4aeba694ee3d43400082427cd3b238de9c84736193a25968a6029f4a09957d00c6fcfb3f87f9317feb71732f31c91933fd29  readme.debian.patch
"
