# Contributor: V.Krishn <vkrishn4@gmail.com>
# Contributor: Eivind Uggedal <eu@eju.no>
# Contributor: TBK <alpine@jjtc.eu>
# Maintainer: fossdd <fossdd@pwned.life>
pkgname=redict
pkgver=7.3.3
pkgrel=2
pkgdesc="A distributed key/value database"
url="https://redict.io/"
arch="all"
license="LGPL-3.0-only"
pkgusers="redict"
pkggroups="redict"
makedepends="linux-headers openssl-dev>3"
checkdepends="tcl procps"
install="
	$pkgname-compat.pre-install
	$pkgname.post-install
	$pkgname.post-upgrade
	$pkgname.pre-install
	"
subpackages="
	$pkgname-benchmark
	$pkgname-cli
	$pkgname-compat::noarch
	$pkgname-openrc
	"
builddir="$srcdir/$pkgname"
source="redict-$pkgver.tar.gz::https://codeberg.org/redict/redict/archive/$pkgver.tar.gz
	redict.conf.patch
	sentinel.conf.patch
	$pkgname.initd
	$pkgname.confd
	$pkgname-sentinel.initd
	$pkgname.logrotate
	CVE-2025-27151.patch
	CVE-2025-32023.patch
	CVE-2025-48367.patch
	"
options="!check" # very flaky

# secfixes:
#   7.3.3-r2:
#     - CVE-2025-32023
#     - CVE-2025-48367
#   7.3.3-r1:
#     - CVE-2025-27151
#   7.3.3-r0:
#     - CVE-2025-21605
#   7.3.2-r0:
#     - CVE-2024-46981
#     - CVE-2024-51741
#   7.3.1-r0:
#     - CVE-2024-31227
#     - CVE-2024-31228
#     - CVE-2024-31449

build() {
	export CFLAGS="$CFLAGS -DUSE_MALLOC_USABLE_SIZE -O2 -flto=auto"
	make USE_JEMALLOC=no \
		MALLOC=libc \
		BUILD_TLS=yes \
		all
}

check() {
	make test
}

package() {
	make install PREFIX="$pkgdir/usr" INSTALL_BIN="$pkgdir/usr/bin"

	cd "$pkgdir"

	install -D -m 644 "$builddir"/redict.conf etc/redict.conf

	# This file must be writable for redict, otherwise Sentinel fails to start.
	install -D -m 644 -o redict -g redict "$builddir"/sentinel.conf etc/sentinel.conf

	install -D -m 755 "$srcdir"/redict.initd etc/init.d/redict
	install -D -m 755 "$srcdir"/redict-sentinel.initd etc/init.d/redict-sentinel
	install -D -m 644 "$srcdir"/redict.confd etc/conf.d/redict
	install -D -m 644 "$srcdir"/redict.logrotate etc/logrotate.d/redict

	install -d -o redict -g redict \
		var/lib/redict \
		var/log/redict
}

benchmark() {
	pkgdesc="Redict benchmarking tool"

	amove usr/bin/redict-benchmark
}

cli() {
	pkgdesc="Redict CLI client"

	amove usr/bin/redict-cli
}

compat() {
	pkgdesc="Redict command symlinks and system group for compatibility with redis"
	depends="!redis !valkey-compat"

	mkdir -p "$subpkgdir"/usr/bin
	for i in benchmark check-aof check-rdb cli sentinel server; do
		ln -s redict-$i "$subpkgdir"/usr/bin/redis-$i
	done
}

sha512sums="
7fbdee54f26d59d74bab2b9fa619cb7c0135aebb38ff891423f4963ca593e20018f9f789288eb3ebc769f02a05da3fe35033f75d42902bb99421cae5689f5e88  redict-7.3.3.tar.gz
96740880cd0caac2cecb11066a555055e7c3f10feb312b2d4c7a206fb92c1003415cf7ab81bec4ac96f7631724d9097b1ca130621c8b564a7987dcd75d2f6e25  redict.conf.patch
a3cec12566097fe78a31d09db77ca956ba52ee1fbb8ac4f5ffc49d0299e29f94ef55830f7760324a875bd47391eee116e0c4efb4bef00d6045bf98b81f392dfc  sentinel.conf.patch
555edff7dd7768c9579a101df86e91cc14cb9941365f051e0e993f1c341cd2b46fcfab6f4a6fd4c1d589242363e323734b453652d687d2d46cb651d84d42caee  redict.initd
4a0b04388115b5af6d4173aca66b081740432c19e1085c0bd5251fbbfb190c06aaa0540575e160c1e907cf504e33b3dcb67eb916dda3347cf394123d64adffb2  redict.confd
7cc571f9f977a167d61bd952cf74d043ef17e5211113928c77a2f088df1cc8f31dd19e7de79e2795479f7afccd4dd32b66e61b67c99a21ffa8d9560811185728  redict-sentinel.initd
68e0b0c4c8def5d42149b895ac681d91b8c691a5fd90bce64e6bf6c7780f33c6ac02a923642ebdc85f6bb41d8039cc38b7d3be49e5e9f86f097e3d1f6d41ecd4  redict.logrotate
4009aa507fc0ffc3e8ad93e62447a8039c0a68abff8cfc612543238eeca1a78fa0edad5ee8e27646f36edbd5a68fae59bb320eb4f872ebe4031b8d855af59a63  CVE-2025-27151.patch
2998492265050707ad0a2d82b3005ad447a845f0986cd1c0e0597eb57befd18a5fe908cfd15125fa053c56059e1ad9ea26ef0af22809d530a3005d0bf9c51ab3  CVE-2025-32023.patch
96b8c421df39f0acf01008a65088d96e961bc5231acfb3b16c987d5b46368a398722563c81123eabe7a72406a46c131f89a361472959c6f232a646b776389d1d  CVE-2025-48367.patch
"
