# Maintainer: Peter van Dijk <peter.van.dijk@powerdns.com>
pkgname=pdns-recursor
pkgver=5.1.1
pkgrel=0
pkgdesc="PowerDNS Recursive Server"
url="https://www.powerdns.com/"
arch="all !s390x" # broken context
license="GPL-2.0-only WITH OpenSSL-Exception"
depends="$pkgname-common"
makedepends="autoconf
	automake
	boost-dev
	cargo
	curl
	curl-dev
	libtool
	libsodium-dev
	lua5.4-dev
	net-snmp-dev
	openssl-dev>3
	"
install="$pkgname-common.pre-install"
subpackages="$pkgname-common::noarch $pkgname-doc $pkgname-openrc"
pkgusers="recursor"
pkggroups="recursor"
source="https://downloads.powerdns.com/releases/pdns-recursor-${pkgver//_/-}.tar.bz2
	$pkgname.initd
	recursor.conf
	"
builddir="$srcdir/$pkgname-${pkgver//_/-}"

case "$CARCH" in
loongarch64|ppc64le|riscv64)
	_luajit=OFF
	;;
*)
	makedepends="$makedepends luajit-dev"
	subpackages="$subpackages $pkgname-luajit"
	_luajit=ON
	;;
esac

# secfixes:
#   5.0.4-r0:
#     - CVE-2024-25583
#   5.0.2-r0:
#     - CVE-2023-50387
#     - CVE-2023-50868
#   4.8.1-r0:
#     - CVE-2023-22617
#   4.7.2-r0:
#     - CVE-2022-37428
#   4.6.1-r0:
#     - CVE-2022-27227
#   4.3.5-r0:
#     - CVE-2020-25829
#   4.3.2-r0:
#     - CVE-2020-14196
#   4.3.1-r0:
#     - CVE-2020-10995
#     - CVE-2020-12244
#   4.1.9-r0:
#     - CVE-2019-3806
#     - CVE-2019-3807
#   4.1.8-r0:
#     - CVE-2018-16855
#   4.1.7-r0:
#     - CVE-2018-10851
#     - CVE-2018-14644
#     - CVE-2018-14626
#   4.1.1-r0:
#     - CVE-2018-1000003
#   4.0.7-r0:
#     - CVE-2017-15090
#     - CVE-2017-15092
#     - CVE-2017-15093
#     - CVE-2017-15094

prepare() {
	default_prepare
	if [ "$_luajit" = "ON" ]; then
		cp -a "$builddir" "$builddir-jit"
	fi
}

_configure() {
	local arg="$1"
	local lua

	if [ "$arg" = '-jit' ]; then
		lua="luajit"
	else
		lua="lua5.4"
	fi

	cd "$builddir$arg"

	./configure --prefix=/usr \
		--sysconfdir=/etc/pdns \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--localstatedir=/var \
		--libdir=/usr/lib/pdns \
		--enable-dns-over-tls \
		--enable-unit-tests \
		--with-lua=$lua \
		--with-libcrypto=/usr
}

build() {
	_configure
	make

	[ -d "$builddir-jit" ] || return 0

	_configure -jit
	make
}

_cat_found_file() {
	local _rc="$?"
	find . -name "$1" -type f -print -exec cat {} \;
	return "$_rc"
}

check() {
	make check || _cat_found_file 'test-suite.log'

	if cd "$builddir-jit"; then
		make check || _cat_found_file 'test-suite.log'
	fi
}

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

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

common() {
	pkgdesc="$pkgname common files"
	depends=""

	mkdir -p "$subpkgdir/etc/pdns"

	mv "$pkgdir"/etc/pdns/*  "$subpkgdir"/etc/pdns/
	mv "$subpkgdir"/etc/pdns/recursor.yml-dist "$subpkgdir"/etc/pdns/recursor.conf-dist
	cp "$srcdir/recursor.conf" "$subpkgdir"/etc/pdns/recursor.conf
}

luajit() {
	pkgdesc="$pkgname with luajit bindings"
	depends="$pkgname-common"
	provides="$pkgname=$pkgver-r$pkgrel"

	cd "$builddir-jit"
	make DESTDIR="$subpkgdir" install-exec
	rm -rf "$subpkgdir"/etc/pdns
}

sha512sums="
59129312e79bb48ce5af4de0c90f36765084708fd7b728655b07ca0cf68ba618a4bf517a9e695caaecc57e5edef4f6fa373719aa0b3e82e25d17a077883784d0  pdns-recursor-5.1.1.tar.bz2
71f7be51d3a9a78f22f3946e95534d4f5b37b3bcbd1891ad3a17095be545fdb0eaf239d58c9f40ccc642542150c0b35ed025bf69acabfa779c0ab10c67a5e796  pdns-recursor.initd
4bd244afe3ca888292eb1a9528b6f308ef7c131626f2e62a7b58e381710c392c671f96bb4932be52bb4213e58c349ed3ae9cf9ddf13fd92d6a43150ab4b54b11  recursor.conf
"
