# Maintainer: Celeste <cielesti@protonmail.com>
maintainer="Celeste <cielesti@protonmail.com>"
pkgname=lemmy-ui
pkgver=0.19.8
# this needs to be synced with lemmy
_translations_commit=2e0ec6e334f0f158d8ef00baa1ba29f4687b50fa
pkgrel=0
pkgdesc="Link aggregator and forum for the Fediverse - Official webapp"
url="https://join-lemmy.org/"
# armhf, armv7, loongarch64, ppc64le, s390x, x86: follow lemmy aport
# riscv64: argument 'src' must be a string
arch="aarch64 x86_64"
license="AGPL-3.0-only"
depends="nodejs"
makedepends="
	glib-dev
	npm
	pnpm
	python3
	vips-dev
	"
checkdepends="
	cmd:start-stop-daemon
	curl
	"
install="$pkgname.pre-install"
pkgusers="lemmy-ui"
pkggroups="lemmy-ui"
subpackages="$pkgname-openrc"
source="https://github.com/LemmyNet/lemmy-ui/archive/$pkgver/lemmy-ui-$pkgver.tar.gz
	https://github.com/LemmyNet/lemmy-translations/archive/$_translations_commit/lemmy-translations-$pkgver.tar.gz
	lemmy-ui.confd
	lemmy-ui.initd
	"
options="net" # tests access localhost

# use system libvips
export SHARP_FORCE_GLOBAL_LIBVIPS=1

prepare() {
	default_prepare

	rmdir -v lemmy-translations
	ln -sv "$srcdir/lemmy-translations-$_translations_commit" \
		lemmy-translations

	sed -i "s/unknown version/$pkgver/" src/shared/version.ts

	# -U_FORTIFY_SOURCE: "error: inlining failed in call to 'always_inline'
	# 'vsnprintf': function body can be overwritten at link time"
	local extra_flags="-U_FORTIFY_SOURCE $(pkgconf --cflags glib-2.0)"
	export CFLAGS="$CFLAGS $extra_flags"
	export CPPFLAGS="$CPPFLAGS $extra_flags"
	export CXXFLAGS="$CXXFLAGS -std=c++17 $extra_flags"

	# Add modules needed to build sharp with system libvips
	pnpm add node-addon-api@8.0.0 node-gyp@10.1.0

	pnpm install --frozen-lockfile
}

build() {
	export GIT_DIR="$builddir"
	NODE_ENV=production pnpm build:prod
	unset GIT_DIR

	# Remove sharp for linux (glibc), leaving sharp for linuxmusl
	find node_modules -name "*sharp-*linux-*" -exec rm -vrf '{}' \+
}

check() {
	local tmpdir="${tmpdir:-"$(mktemp -d)"}"
	mkdir -p "$tmpdir"

	start-stop-daemon -Sbmp "$tmpdir"/test.pid -w 5000 \
		-e 'NODE_ENV=production' -x node dist/js/server.js
	curl -o "$tmpdir"/test.out http://127.0.0.1:1234 || true
	start-stop-daemon -Kp "$tmpdir"/test.pid
	grep -q "UI: $pkgver" "$tmpdir"/test.out
}

package() {
	mkdir -p "$pkgdir"/usr/share/webapps/$pkgname
	cp -r dist node_modules "$pkgdir"/usr/share/webapps/$pkgname/

	# cleanup unused files
	find "$pkgdir" -type f -a \( \
		-name "*.ts" \
		-o -name "webpack*" \
		-o -name "tsconfig*" \
		-o -name "babel.config*" \
		-o -name "README*" \
		-o -name "readme*" \
		-o -name "CHANGELOG*" \
		-o -name "*.map" \
		-o -name "LICENSE*" \
		-o -name "License" \
		-o -name "license" \
		-o -name "*.md" \
		\) \
		-delete
	find "$pkgdir" -type d -a \( \
		-name "example" \
		-o -name "examples" \
		-o -name "test" \
		-o -name "tests" \
		-o -name ".github" \
		\) \
		-exec rm -rf '{}' \+

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

sha512sums="
de627ebb1b2c29fbdfb74cc724c532a32650e3e1a99918d1b61699e1e8e86dd164333bbfcbc2e71e3e03c83474317759330724ca10968ff058fdb1ee2a0c4323  lemmy-ui-0.19.8.tar.gz
6760e3d67160cb806f322f865d216a92c369d1fb3e3fdc782160419dd9bdf5cd051a08d0fdde17c324f8a84eda09c500bcf4edabdce5cf7ae95fa193cb8ba48e  lemmy-translations-0.19.8.tar.gz
cac55baba8fa7cd49e41877e80296b1505aee35444d025c6c612a5c7091f36c92adeab17117d8f58d228f7bbe9f016a9e752a8b9c0f9a940bd8980132577e1a9  lemmy-ui.confd
59559ffab27cd9562b0f920a72df0a66676f96612960c573132ef1d8914d16ff36300e2514c0fd8e3d16ffd13af58fc129afc88b14f64a16468457e224b97fbd  lemmy-ui.initd
"
