# Contributor: lauren n. liberda <lauren@selfisekai.rocks>
maintainer="lauren n. liberda <lauren@selfisekai.rocks>"
pkgname=intiface-central
pkgver=2.6.5
pkgrel=1
pkgdesc="Frontend application for the Buttplug Sex Toy Control Library"
url="https://intiface.com/central/"
arch="aarch64 x86_64"	# flutter
license="GPL-3.0-only"
makedepends="
	cargo
	corrosion
	dbus-dev
	eudev-dev
	flutter-desktop
	openssl-dev
	patchelf
	"
options="net !check"	# no tests
source="
	https://github.com/intiface/intiface-central/archive/refs/tags/v$pkgver/intiface-central-$pkgver.tar.gz

	system-corrosion.patch
	system-flutter.patch
	pmos-fit-screen.patch
	no-werror.patch
	flutter-3.29.patch

	intiface-central
	intiface-central.desktop
	"

case "$CARCH" in
	x86_64) _flutter_arch="x64" ;;
	aarch64) _flutter_arch="arm64" ;;
esac

# FIXME: i'm sorry. workarounds https://gitlab.alpinelinux.org/alpine/aports/-/issues/16556
real_so_path() {
	local so="$1"
	shift
	while [ $# -gt 0 ]; do
		[ -e "$1"/$so ] && realpath "$1/$so" && return 0
		shift
	done
	error "$so: path not found"
	return 0
}

prepare() {
	default_prepare

	flutter pub get --enforce-lockfile

	(
		cd intiface-engine-flutter-bridge

		cargo fetch --target="$CTARGET" --locked
	)
}

build() {
	flutter build linux --release

	for _elf in build/linux/"$_flutter_arch"/release/bundle/lib/lib*.so; do
		# running patchelf on libapp (the AOT snapshot of the whole Dart codebase)
		# breaks it with "[FATAL:flutter/runtime/dart_vm_initializer.cc(89)]
		# Error while initializing the Dart VM: Invalid vm isolate snapshot seen".
		#
		# it has no rpath and only links to libc soname anyway. 🤷‍♀️
		[ "$(basename "$_elf")" != "libapp.so" ] || continue

		patchelf --set-rpath '$ORIGIN' "$_elf"
	done
}

package() {
	local bundle="$builddir"/build/linux/$_flutter_arch/release/bundle

	install -Dm755 "$bundle"/${pkgname/-/_} "$pkgdir"/usr/lib/$pkgname/${pkgname/-/_}
	cp -r "$bundle"/lib "$pkgdir"/usr/lib/$pkgname/lib
	cp -r "$bundle"/data "$pkgdir"/usr/lib/$pkgname/data

	install -Dm755 "$srcdir"/$pkgname "$pkgdir"/usr/bin/$pkgname

	install -Dm644 "$srcdir"/$pkgname.desktop "$pkgdir"/usr/share/applications/$pkgname.desktop
	install -Dm644 assets/icons/intiface_central_icon.png "$pkgdir"/usr/share/icons/hicolor/512x512/apps/$pkgname.png
}

sha512sums="
af1bd239fbcefe6387e6423c24f1e9443d7f1fa1c21953d5db544431cda7871d4eb99c6c0e9458f8ff9a3d134b826e9a979c6c92f16b251cb2112de199b8c6c4  intiface-central-2.6.5.tar.gz
ecad3c5cee9c53fc88b539d3088907539aff0b28980a89d4be2acb3c329a7a2154cb12743c0786e5c4a33de78af06b61b5b35f8eee633989cc943dc14e87dba6  system-corrosion.patch
91014708ab7bb81518ce93db0612209d6479dfa503064d9e9d3186cd5e861165a3f63e286d271d5aac8a1d676868fc3acf51648ac01c16a215062a317ccb6623  system-flutter.patch
a090ec306117675c555683a836a5368b7165423509439c5d4a75ffcf9a125ab5d7a2598f2c2253f044fc77785276f696b495aece74d4ab8eea9126404966e061  pmos-fit-screen.patch
1d2b7d28a09e26ed48d89aa5f5dffb738ba367fa2760b12980225239705e16fa1ec89901d7aecf26c8527de721a5f50f50cc77ea009cd5f25f219d1af8730ed8  no-werror.patch
e47dfa95c98d5e28f043e57db54e5b1d2203313a86146d1de4a4db9adcade26baeab52d05fad57a0432d46fde6b2337bb8a99c60b12fda3bb95ea74217092714  flutter-3.29.patch
a69d49b59ebd0dbeeace74954d72cd97c38aa79597887648f0c8f5ec4ecb191a1b47c43d3007e7c826323800a601951e621ee60ed739f17f67fca2f8855d95b0  intiface-central
4015b388f997eb3abc2ecd69947bb703926e01de82807a940a42902f46e90d6759f609921215ba865cb6c91f5c6d404115c16a0a7ebe4bcc35315a4910adc081  intiface-central.desktop
"
