# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=zigbee2mqtt
pkgver=2.3.0
pkgrel=1
pkgdesc="Zigbee to MQTT bridge"
url="https://www.zigbee2mqtt.io/"
# ppc64le: fails to build @serialport/bindings
# s390x: tests fail due to @rollup/wasm-node
arch="all !ppc64le !s390x"
# zigbee2mqtt is GPL-3.0, other licenses are for its dependencies.
license="GPL-3.0-only AND MIT AND ISC AND BSD-3-Clause AND BlueOak-1.0.0 AND Python-2.0"
depends="nodejs"
makedepends="
	linux-headers
	nodejs-dev
	npm
	pnpm
	python3
	"
pkgusers="zigbee2mqtt"
pkggroups="$pkgusers"
install="
	$pkgname.pre-install
	$pkgname.pre-upgrade
	$pkgname.post-upgrade
	"
subpackages="
	$pkgname-dbg::noarch
	$pkgname-openrc
	"
source="https://github.com/Koenkk/zigbee2mqtt/archive/$pkgver/$pkgname-$pkgver.tar.gz
	dont-build-on-start.patch
	ungit.patch
	rollup-wasm-node.patch
	syslog-defaults.patch

	$pkgname.initd
	$pkgname.confd
	"
options="net"

case "$CARCH" in
	# Some tests timeout.
	riscv64) options="!check";;
esac

# Don't let pnpm pollute $HOME.
export XDG_CACHE_HOME="${tmpdir:-$srcdir}/.cache"
export XDG_STATE_HOME="${tmpdir:-$srcdir}/.state"
export PNPM_HOME="${tmpdir:-$srcdir}/.pnpm"
# Prevent node-gyp from downloading nodejs headers.
export npm_config_nodedir=/usr

prepare() {
	default_prepare

	pnpm install --frozen-lockfile
}

build() {
	# Rebuild serialport native bindings.
	rm -r node_modules/.pnpm/@serialport+bindings-cpp@*/node_modules/@serialport/bindings-cpp/prebuilds
	npm rebuild @serialport/bindings-cpp --build-from-source --nodedir=/usr --verbose

	pnpm run build
}

check() {
	pnpm run test
}

package() {
	local destdir="$pkgdir/usr/lib/$pkgname"

	mkdir -p "$destdir"
	cp -r dist node_modules cli.js index.js package.json pnpm-lock.yaml "$destdir"/

	cd "$pkgdir"

	mkdir -p usr/bin
	ln -s /usr/lib/$pkgname/cli.js usr/bin/$pkgname

	install -d -m750 -o "$pkgusers" -g "$pkggroups" var/lib/$pkgname
	ln -s /var/lib/$pkgname "$destdir"/data

	install -D -m755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -D -m644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname

	cd "$destdir"

	# Remove dev dependencies.
	pnpm prune --prod

	cd node_modules

	# Remove unnecessary files and garbage.
	find . -empty -type d -delete
	find . \( -name '.git*' \
			-o -name '.vscode' \
			-o -name 'doc' \
			-o -name 'fixture' \
			-o -name 'fixtures' \
			-o -name 'test' \
			-o -name 'tests' \
			-o -name '.nyc_output' \) \
		-type d -exec rm -rf '{}' +
	find . ../dist \( -name '.*' \
			-o -name '*.flow' \
			-o -name '*.gz' \
			-o -name '*.md' \
			-o -name '*.min.js' \
			-o -name '*.test.js' \
			-o -name '*.ts' \
			-o -name '*.ts.map' \
			-o -name 'CMakeLists.txt' \) \
		-type f -delete
	rm -rf moment/dist moment/min moment/src
	rm -rf jszip/dist
	rm -rf mqtt/dist
	rm -rf .pnpm/node_modules/dist

	cd unix-dgram
	rm -r src
	rm -r build/Release/obj.target build/Release/.deps
	rm build/Makefile build/binding* build/config.gypi build/*.mk
	strip build/Release/*.node
	cd - >/dev/null

	cd .pnpm/@serialport+bindings-cpp@*/node_modules/@serialport/bindings-cpp
	rm -r src
	rm -r build/Release/obj.target build/Release/.deps
	rm build/Makefile build/binding* build/config.gypi
	strip build/Release/*.node
	cd - >/dev/null
}

dbg() {
	pkgdesc="$pkgdesc (.map files for debugging)"
	depends="$pkgname=$pkgver-r$pkgrel"

	cd "$pkgdir"
	amove $(find usr/lib/$pkgname -type f -name '*.map')
}

sha512sums="
d1eee5e3fdbfb64ba2aec56a9fcd9d7293a21922a2b74f69b7f841d19bdd0e2cb6c9e4b40945c0f753ac6aac1bc72d27354cdce8854eacebc1564309a18d4574  zigbee2mqtt-2.3.0.tar.gz
8dae6f02d13c60aae1823994e55d739e4a2341a48707ab2100704ced483326221b943ef8a43aa29929864a1c5ca131cf510029c9ad817474c5df34049124256f  dont-build-on-start.patch
98e47c84451d73c94321dc8e881107b396a7cdc1bcc6927bbdfb16eaa067db5c675515551aa550e114542c7ae3c956b21000fecefb98db742adfd48d9796f8f4  ungit.patch
a4871c906de9770cacd2bf652c50983464ff16ec9231b5d5e9bba0c98c91ff78bfb9bfc48ab5416556483c542a3c961d01f9beaf76a54489b0b8d6eacd934bda  rollup-wasm-node.patch
d10d089fef516b001e605e6844beda598ad88d3c8927eb06705657f545785e65b0e17979af39f31c7b3e49ee812d74809807376270ed3e1daf48f35b4d099612  syslog-defaults.patch
6cfbd65780035951f0e4c1eae222c1964952a35ede38b6b227bc0c74baa019e03eb0c6ab5eb572286ec2b0b79b27123c4f1b83015cdb0683b99630cf0b265cce  zigbee2mqtt.initd
4ec52a0db61bb762ef0e576baa4a66566e7aeb5246f3b7b81bb17a342c8352c2a9b2ed371d69c35903e7c7c8be29cec30752255826443d363e7d92e6f5ea4ee5  zigbee2mqtt.confd
"
