# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=ipfixprobe
pkgver=4.16.0
pkgrel=0
pkgdesc="High-performance net flow exporter"
url="https://github.com/CESNET/ipfixprobe"
arch="all"
license="BSD-3-Clause"
makedepends="
	autoconf
	automake
	bsd-compat-headers
	cesnet-telemetry-dev
	libpcap-dev
	libtool
	linux-headers
	openssl-dev
	lz4-dev
	"
checkdepends="bash"
install="$pkgname.pre-install"
subpackages="
	$pkgname-doc
	$pkgname-openrc
	$pkgname-bash-completion
	"
source="https://github.com/CESNET/ipfixprobe/archive/v$pkgver/ipfixprobe-$pkgver.tar.gz
	$pkgname.initd
	$pkgname.confd
	"

_with_dpdk=no
case "$CARCH" in
# dpdk is supported only on these architectures:
aarch64 | ppc64le | riscv64 | x86_64)
	_with_dpdk=yes
	makedepends="$makedepends dpdk-dev"
	subpackages="$subpackages $pkgname-dpdk"
esac

prepare() {
	default_prepare
	autoreconf -iv
}

build() {
	if [ "$_with_dpdk" = yes ]; then
		_build --with-dpdk
		mv ipfixprobe ipfixprobe.dpdk
	fi

	_build --without-dpdk
}

_build() {
	export CXXFLAGS="${CXXFLAGS/-Os/-O2}"

	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--localstatedir=/var \
		--with-pcap \
		"$@"
	make
}

check() {
	make check
}

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

	cd "$pkgdir"

	rm -rf usr/lib/systemd

	mv usr/share/doc/ipfixprobe/ipfixprobe/* usr/share/doc/ipfixprobe/
	rmdir usr/share/doc/ipfixprobe/ipfixprobe

	mv etc/ipfixprobe/*.example usr/share/doc/ipfixprobe/
	rmdir etc/ipfixprobe

	mkdir -p usr/share/bash-completion/completions
	mv etc/bash_completion.d/ipfixprobe.bash usr/share/bash-completion/completions/ipfixprobe
	rmdir etc/bash_completion.d

	install -D -m755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -D -m644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
}

dpdk() {
	pkgdesc="$pkgdesc (with DPDK interface)"
	replaces="$pkgname"

	install -D -m755 "$builddir"/ipfixprobe.dpdk "$subpkgdir"/usr/bin/ipfixprobe
}

sha512sums="
a08f04753d73b45d5465a5cf2b06e19390f38be9a71b9d687fcf1f5eed6a1ace52bceeb3bd4d0374190a9a2a77d56865c088aaa0f37fed905f639ab5995634a2  ipfixprobe-4.16.0.tar.gz
f249b80012909231f01469016ff1f78cb214d0b0e9590950a0143437028ec58e9ba7ac2e10020261bf99d3d0c260a901d453828be7388612257f286137fe84a8  ipfixprobe.initd
7d86866beb9797910ebc4970cf8d2fca3c72071084fa637edf8e882024467f8337e02b8d7d0a69372c46968bc3589721cd81815277162e2f1510779d6c52beec  ipfixprobe.confd
"
