# Contributor: Kay Thomas <kaythomas@pm.me>
# Maintainer: Kay Thomas <kaythomas@pm.me>
pkgname=thelounge
pkgver=4.4.1
pkgrel=1
pkgdesc="Modern, responsive, cross-platform, self-hosted web IRC client"
url="https://thelounge.chat"
# x86: textrels
# s390x: fails to check
# riscv64: fails to build
arch="all !x86 !s390x !riscv64"
license="MIT"
depends="nodejs"
makedepends="yarn npm python3"
subpackages="$pkgname-openrc"
pkgusers="thelounge"
pkggroups="thelounge"
install="$pkgname.pre-install $pkgname.post-install"
source="$pkgname-$pkgver.tar.gz::https://github.com/thelounge/thelounge/archive/v$pkgver.tar.gz
	no-version-test.patch
	thelounge.initd
	allow-https-for-connect-src.patch
	"
options="net" # npm

prepare() {
	default_prepare

	yarn install --frozen-lockfile
}

build() {
	NODE_ENV=production yarn run build
}

check() {
	CI=yes yarn run test:mocha
}

package() {
	yarn install --production --ignore-scripts --prefer-offline
	NODE_ENV=production npm install --unsafe-perm -g --prefix "$pkgdir"/usr

	# Remove incorrect symlink, copy correct files
	rm "$pkgdir"/usr/lib/node_modules/thelounge
	mkdir -p "$pkgdir"/usr/lib/node_modules/thelounge
	cp -a index.js client public node_modules dist package.json "$pkgdir"/usr/lib/node_modules/thelounge/

	# 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 "CHANGELOG*" \
		-o -name "*.map" \
		-o -name "LICENSE" \
		\) \
		-delete

	# Set home location
	echo /var/lib/thelounge > \
		"$pkgdir"/usr/lib/node_modules/$pkgname/.thelounge_home

	# Add default config
	# this is only read from 'home' so we have to just put everything in var/lib
	install -dm755 -o thelounge -g thelounge \
		"$pkgdir"/var/lib/thelounge
	install -m644 -o thelounge -g thelounge \
		"$pkgdir"/usr/lib/node_modules/$pkgname/dist/defaults/config.js \
		"$pkgdir"/var/lib/thelounge/config.js

	install -Dm755 "$srcdir"/$pkgname.initd \
		"$pkgdir"/etc/init.d/$pkgname
}

sha512sums="
7695121a713a23688bc6f52dae2574bab1288eea930fd50d4dd85037233e9f23bd8e460980c69cdd14ea8648da4720d84e8196547b6a18e69d2f478b43d6e29a  thelounge-4.4.1.tar.gz
cbf80e23b0af8f0185699d6b03816c645c51b85fff7f163d3cd3d00296ed816b6ab01529b359fbfd549a79e8adb72bbc83bc7a389cf13e0afd50636ff79a138e  no-version-test.patch
f367d27ebcc412ff03c12ae98e50aeae5051fb5ffa9da6220f664c59993ed0e330b55b3b41fe941d546634901163d006e318891b4b886f6c49a93e0888fccd3e  thelounge.initd
212e468d6cedaa528b7fad534b8ba0e7a3d69137940cdabd22dbe34375491900d5b7d577550aa3b245ad7775488a90e308019db55ff15e8ede105c4b9c15b015  allow-https-for-connect-src.patch
"
