# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=njs
# Keep it in sync with njs in main/nginx.
pkgver=0.8.7
pkgrel=0
pkgdesc="njs scripting language CLI utility"
url="https://nginx.org/en/docs/njs/"
arch="all"
license="BSD-2-Clause"
# Note: njs is compatible with pcre2, but better to use the same PCRE library
# as for njs module in nginx.
makedepends="
	libedit-dev
	libxml2-dev
	openssl-dev
	pcre-dev
	"
checkdepends="expect"
subpackages="$pkgname-debug $pkgname-libs-static"
source="$pkgname-$pkgver.tar.gz::https://github.com/nginx/njs/archive/$pkgver.tar.gz
	fix-tests-libxml.patch
	mktemp-busybox-compat.patch
	no-werror.patch
	"

case "$CARCH" in
	# arm*: https://github.com/nginx/njs/issues/821
	arm* | x86) options="!check";;
esac

# secfixes:
#   0.7.3-r0:
#     - CVE-2021-46462
#     - CVE-2021-46463
#     - CVE-2022-25139
#   0.7.1-r0:
#     - CVE-2021-46461

build() {
	./configure \
		--build-dir='build-debug' \
		--debug=YES
	make njs

	./configure --cc-opt='-Os' --ld-opt='-Os'
	make njs
}

check() {
	make test
}

package() {
	install -m 755 -D build/njs "$pkgdir"/usr/bin/njs

	install -m 644 -D build/libnjs.a "$pkgdir"/usr/lib/libnjs.a
	install -m 644 -D build-debug/libnjs.a "$pkgdir"/usr/lib/libnjs-debug.a
}

debug() {
	pkgdesc="njs built with additional runtime checks and debug symbols"
	options="!strip"

	install -m 755 -D "$builddir"/build-debug/njs "$subpkgdir"/usr/bin/njs-debug
}

sha512sums="
6a31d73c458bd5a21d1d1ba3eb4a8922cb3dd9ed900c91a76964203ee1418e4694b017e97272361710da9260412bfc1d4d45290d3488ae87983a0d4a6b0916e8  njs-0.8.7.tar.gz
de3509ab76e8cd433cc3e73c7f5755efa73f974e3752e95ac828a82ef01f212612cc48f395c916f9ed0ac2b76e946c3a47cbc432becf4cb47307e7236da6a689  fix-tests-libxml.patch
4db527d663dbe9e8b503c3cbaa4eae34b45990a5359b3bb98ce970c705faefcac98de49439f2557756a2be8e2e06acc67f98942de01674c498832d80c3cb90c3  mktemp-busybox-compat.patch
b8ed822bf0d2ac99682cdecde5ce0136ac46cc9e6077be1369c631ac4caf22f22750f39fced061233f44e3e290e8ca6135da5d121bd53551cbd460419bc90662  no-werror.patch
"
