# Contributor: Natanael Copa <ncopa@alpinelinux.org>
# Contributor: Fabricio Silva <hi@fabricio.dev>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=znc
pkgver=1.9.1
pkgrel=0
pkgdesc="Advanced IRC bouncer"
url="https://wiki.znc.in/ZNC"
arch="all"
license="Apache-2.0"
makedepends="
	argon2-dev
	c-ares-dev
	cmake
	cyrus-sasl-dev
	gettext-dev
	icu-dev
	openssl-dev
	perl-dev
	python3-dev
	samurai
	tcl-dev
	"
options="!check" # no test suite
pkgusers="$pkgname"
pkggroups="$pkgusers"
install="$pkgname.pre-install"
subpackages="$pkgname-dev $pkgname-doc $pkgname-extra $pkgname-modtcl
	$pkgname-modperl $pkgname-modpython $pkgname-openrc"
source="https://znc.in/releases/archive/znc-$pkgver.tar.gz
	$pkgname.initd
	$pkgname.confd"

# secfixes:
#   1.9.1-r0:
#     - CVE-2024-39844
#   1.8.1-r0:
#     - CVE-2020-13775
#   1.7.4-r0:
#     - CVE-2019-12816
#   1.7.3-r0:
#     - CVE-2019-9917
#   1.7.1-r0:
#     - CVE-2018-14055
#     - CVE-2018-14056

build() {
	export CFLAGS="$CFLAGS -flto=auto -DNDEBUG -D_GNU_SOURCE"
	export CXXFLAGS="$CXXFLAGS -flto=auto -DNDEBUG -Wno-deprecated-declarations"

	cmake -B build -G Ninja \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_SYSCONFDIR=/etc \
		-DCMAKE_INSTALL_LOCALSTATEDIR=/var \
		-DCMAKE_INSTALL_MANDIR=/usr/share/man \
		-DCMAKE_INSTALL_INFODIR=/usr/share/info \
		-DWANT_CYRUS=YES \
		-DWANT_TCL=YES \
		-DWANT_PERL=YES \
		-DWANT_PYTHON=YES \
		-DWANT_ARGON=YES
	cmake --build build
}

package() {
	DESTDIR="$pkgdir" cmake --install build

	install -D -m755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -D -m644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
	install -d -m750 -o $pkgusers -g $pkggroups "$pkgdir"/var/lib/znc
}

dev() {
	default_dev
	_mv_to_sub usr/bin/znc-buildmod
}

extra() {
	pkgdesc="Extra modules for ZNC"

	_mv_mod \
		autovoice.so \
		block_motd.so \
		clearbufferonmsg.so \
		ctcpflood.so \
		flooddetach.so \
		imapauth.so \
		listsockets.so \
		log.so \
		notify_connect.so \
		send_raw.so \
		shell.so
}

modtcl() {
	pkgdesc="TCL module for ZNC"
	depends="znc"

	_mv_mod modtcl.so
	_mv_to_sub usr/share/znc/modtcl
}

modperl() {
	pkgdesc="Perl module for ZNC"
	depends="znc"

	_mv_mod modperl modperl.so
}

modpython() {
	pkgdesc="Python modules for ZNC"
	depends="znc"

	_mv_mod modpython modpython.so
}

_mv_mod() {
	local i; for i in "$@"; do
		_mv_to_sub usr/lib/znc/$i
	done
}

_mv_to_sub() {
	local i; for i in "$@"; do
		mkdir -p "$subpkgdir"/${i%/*}
		mv "$pkgdir"/$i "$subpkgdir"/$i
	done
}

sha512sums="
939eafbb2f20569d1b15b66e38b7da7a5210f2023e6fc98018566bd757d62d8ef2682d4b4e3b326a933a99cd7d9d65596ff0e2c43a2315c70e27c64f02d526a6  znc-1.9.1.tar.gz
47f9bd00f07861e195333d2cda5b1c7386e2324a1842b890837a7936a94b65b7a269f7fee656a522ec86b58a94bd451a2a3629bd6465578681b8d0733c2c77dc  znc.initd
00360f9b487ed5a9d50c85ce597e65c89cf869cabb893c294d0bc7fcd88f9610ecb63ba6df7af1ba1dd977b6d5b05da625a3ee799a46d381f17ac04b976a1f29  znc.confd
"
