# Contributor: Daniel Corbe <daniel@corbe.net>
# Maintainer: Celeste <cielesti@protonmail.com>
pkgname=frr
pkgver=10.1.1
pkgrel=0
pkgdesc="Free Range Routing is a fork of Quagga"
pkgusers="frr"
pkggroups="frr frrvty"
url="https://frrouting.org/"
arch="all"
license="GPL-2.0-or-later"
depends="iproute2 bash"
makedepends="
	autoconf
	automake
	bison
	bsd-compat-headers
	c-ares-dev
	elfutils-dev
	flex
	json-c-dev
	libcap-dev
	libtool
	libyang-dev<2.2
	linux-headers
	net-snmp-dev
	protobuf-c-dev
	py3-sphinx
	python3-dev
	readline-dev
	rtrlib-dev
	texinfo
	"
checkdepends="py3-pytest"
install="$pkgname.pre-install"
subpackages="$pkgname-dbg
	$pkgname-doc::noarch
	$pkgname-openrc
	$pkgname-pythontools::noarch
	$pkgname-rpki
	$pkgname-snmp
	"
source="https://github.com/FRRouting/frr/archive/frr-$pkgver.tar.gz
	frr.initd

	allow-invalid-nlri-attributes.patch
	fix-basename.patch
	fix-py-syntaxwarning.patch
	"
builddir="$srcdir/frr-frr-$pkgver"

_sysconfdir=/etc/frr

# secfixes:
#   8.2.1-r0:
#     - CVE-2022-26125
#     - CVE-2022-26126
#     - CVE-2022-26127
#     - CVE-2022-26128
#     - CVE-2022-26129

prepare() {
	default_prepare
	./bootstrap.sh
}

build() {
	export PYTHON=python3

	CFLAGS="$CFLAGS -flto=auto" \
	./configure \
		--prefix=/usr \
		--localstatedir=/run/frr \
		--sbindir=/usr/lib/frr \
		--sysconfdir=$_sysconfdir \
		--libdir=/usr/lib/frr \
		--with-moduledir=/usr/lib/frr/modules \
		LIBTOOLFLAGS="-rpath /usr/lib/frr" \
		--disable-dependency-tracking \
		--enable-rpki \
		--with-libpam \
		--enable-doc \
		--enable-doc-html \
		--enable-snmp \
		--enable-fpm \
		--disable-protobuf \
		--disable-zeromq \
		--enable-ospfapi \
		--enable-bgp-vnc \
		--enable-multipath=256 \
		--enable-user=${pkgusers%% *} \
		--enable-group=${pkggroups%% *} \
		--enable-vty-group=frrvty \
		--enable-configfile-mask=0640 \
		--enable-logfile-mask=0640
	make
}

check() {
	export PYTHON=python3
	make check
}

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

	# Remove development files, static libraries and headers.
	# They are internal and should not be shipped as a package.
	# Compare https://github.com/FRRouting/frr/blob/frr-7.0/debian/rules#L89
	find "$pkgdir" \( -name '*.la' -o -name '*.a' -o -name 'lib*.so' \) -exec rm -f {} +
	rm -rf "$pkgdir"/usr/include
	rm -f "$pkgdir"/usr/lib/frr/ssd

	# FRR currently installs two flavours of scripts for starting and stopping it.
	# Remove the deprecated one.
	rm -f "$pkgdir"/usr/lib/frr/frr
	rm -f "$pkgdir"/usr/lib/frr/frr-reload

	install -d "$pkgdir"$_sysconfdir
	install -Dm644 tools/etc/frr/vtysh.conf \
		tools/etc/frr/frr.conf \
		tools/etc/frr/daemons \
		tools/etc/frr/support_bundle_commands.conf \
		-t "$pkgdir"$_sysconfdir
	chown -R ${pkgusers%% *}:${pkggroups%% *} "$pkgdir"$_sysconfdir
	install -Dm644 tools/etc/iproute2/rt_protos.d/frr.conf \
		-t "$pkgdir"/etc/iproute2/rt_protos.d
	install -Dm755 "$srcdir"/frr.initd "$pkgdir"/etc/init.d/frr
}

pythontools() {
	pkgdesc="$pkgdesc (tools)"
	depends="python3 py3-ipaddr"

	amove usr/lib/frr/frr-reload.py
}

rpki() {
	pkgdesc="$pkgdesc (BGP RPKI support)"

	amove usr/lib/frr/modules/bgpd_rpki.so
}

snmp() {
	pkgdesc="$pkgdesc (net-snmp bindings)"

	amove usr/lib/frr/modules/*_snmp.so \
		usr/lib/frr/libfrrsnmp.*
}

sha512sums="
c555bafc7437e8ba0216bd9cc7e0a432af4c9877255ab4ffdd97a0b7626eb3c9022051b91f6cd5d27d06531f6edef84742b67169ebbc040b8a904e58712cb654  frr-10.1.1.tar.gz
f86bdf8f6dd89ba69d03a69bbe645b3b752d3fbb38913c5155536b8043b0493901ab078b7acb6ac463d3473c6492900d8a695582f66610955246052b19c0d70c  frr.initd
5b2d735da13ea469c07b254d870fa67f36edb8ccf9eb7073f8858a830e5ec6356a6a9ebe304523ced475ad49beadf09666212eedb65b41edde2d12043ff4b900  allow-invalid-nlri-attributes.patch
6ae23e78930a9361f591619d36ca71682c926b070431ecbb58955a0ec1560f003608b235ca5b6a256610d2f72eeb361f4738c6d5dcaaa9812e43366c8b7faa5a  fix-basename.patch
31552271c2661549f0f040921ad5b7df7570965d6e16fa41d28e84744bc8e0a17a7ad1a90cb0cb949e5898b6bf03ec3cfef8e39a57cac49bcccacdddb36d4a47  fix-py-syntaxwarning.patch
"
