# Contributor: Fabricio Silva <hi@fabricio.dev>
# Maintainer: Fabricio Silva <hi@fabricio.dev>
pkgname=porla
pkgver=0.41.0
pkgrel=2
pkgdesc="A high performance BitTorrent client for servers and seedboxes."
url="https://porla.org/"
arch="all !riscv64" # riscv64: nodejs keeps failing
license="MIT"
_lua=5.4
_boost=1.84
makedepends="
	linux-headers
	cmake
	samurai
	libtorrent-rasterbar-dev
	boost$_boost-dev
	openssl-dev
	openssl-libs-static
	libsodium-dev
	sqlite-dev
	zlib-dev
	libgit2-dev
	lua$_lua-dev
	nettle-dev
	mbedtls-dev
	nlohmann-json
	gnutls-dev
	libzip-dev
	tomlplusplus-dev
	uriparser-dev
	npm
	zip
	"
install="$pkgname.pre-install"
subpackages="$pkgname-doc $pkgname-openrc"
options="!check net" # no tests / net for npm
_uwebsockets=20.44.0
_usockets=0.8.6
source="https://github.com/porla/porla/archive/v$pkgver/porla-$pkgver.tar.gz
	https://github.com/uNetworking/uWebSockets/archive/v$_uwebsockets/uwebsockets-$_uwebsockets.tar.gz
	https://github.com/uNetworking/uSockets/archive/v$_usockets/usockets-$_usockets.tar.gz
	cmake.libraries.patch
	porla.initd
	porla.confd
	"

prepare() {
	default_prepare

	# link uSockets to uWebSockets
	rm -rf ../uWebSockets-$_uwebsockets/uSockets
	ln -sfv ../uSockets-$_usockets ../uWebSockets-$_uwebsockets/uSockets
	# link uWebSockets to vendor
	rm -rf vendor/uWebSockets
	ln -sfv ../../uWebSockets-$_uwebsockets vendor/uWebSockets

	npm --prefix html ci
}

build() {
	npm --prefix html run build
	(
		cd html/dist
		zip -9 -r ../webui.zip ./*
	)

	export GITVERSION_SEMVER="$pkgver-r$pkgrel.alpine"
	cmake -S . -B build -G Ninja -Wno-dev \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DLUA_INCLUDE_DIR="$(pkg-config --variable=includedir lua$_lua)" \
		-DLUA_LIBRARIES="$(pkg-config --variable=libdir lua$_lua)/liblua.so"
	cmake --build build
}

package() {
	install -Dm755 build/$pkgname "$pkgdir"/usr/bin/$pkgname
	install -Dm644 LICENSE -t "$pkgdir"/usr/share/licenses/$pkgname/

	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -Dm644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
}

sha512sums="
d0aff4972b450dfc2eeb1debf12aff01d2a29be3286b930dd4ce582e5d9f804c6ba9261b04e06a5e24f5103ef7af50cad2a8c4675c9a28cec88c4159cd849298  porla-0.41.0.tar.gz
ceb950a3d4c2ffd58867ef1cc92d5405441dff603ceb6958ef5660f93c8e1da2346cecb43d209ab08d4feac9c1f023f7bff22a13f537dbb3eab0f122f27f70f6  uwebsockets-20.44.0.tar.gz
a4f71e146003056c4a3e99512e6989fd4a37b5943dfcaf593b7b276231c426abd2e1f60de752443fb62ac5926ecf9812866af3c8ce6c8eeeddffc92fa0eb5afb  usockets-0.8.6.tar.gz
87c9deb042f49b1fc2a2b988339133687c1d1bc31b9096facb98acbbb8e8c554485f7dc62058d85c7e463790ce9c287d6b7a81795e5fcd4c3840f1fc0e0d2d08  cmake.libraries.patch
27e9321fb186bc6239a27dcf57a2821828f188f0563181aa3aa02daa4d6ffaa68aa8821a127f13444600f5826989d7650e56b12d425dd1dd64c30ecae11bc8ba  porla.initd
24a89c8abdc0f63ccfc90644abb55fe85d8bbf559bbbfc139daade60d29ca77b7e594b7e633a9e4dec68ebc094175e1ffa9e17eccb05c5e9f10ea4ba0dd4fd3d  porla.confd
"
