# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=lapce
pkgver=0.4.0
pkgrel=0
pkgdesc="Lightning-fast and Powerful Code Editor written in Rust"
url="https://lapce.dev"
# armhf, armv7, x86: wasmer-vm crate fails to build (unsupported platform)
# ppc64le: ring crate fails to build
# riscv64: would take eternity to build
# s390x: no desktop on this arch
arch="aarch64 x86_64"
license="Apache-2.0"
makedepends="
	cargo
	cargo-auditable
	cmd:patchelf
	libgit2-dev
	libssh2-dev
	libxkbcommon-dev
	openssl-dev
	vulkan-loader-dev
	wayland-dev
	zstd-dev
	"
subpackages="$pkgname-proxy"
source="https://github.com/lapce/lapce/archive/v$pkgver/lapce-$pkgver.tar.gz
	use-system-libs.patch
	cargo.lock.patch
	"
options="!check"  # no tests provided

export CARGO_PROFILE_RELEASE_OPT_LEVEL=2

export LIBSSH2_SYS_USE_PKG_CONFIG=1  # use system libssh2

prepare() {
	default_prepare

	# Rust target triple.
	local target=$(rustc -vV | sed -n 's/host: //p')

	# Build against system-provided libgit2 and libzstd.
	mkdir -p .cargo
	cat >> .cargo/config.toml <<-EOF
		[target.$target]
		git2 = { rustc-link-lib = ["git2"] }
		zstd = { rustc-link-lib = ["zstd"] }
	EOF

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

build() {
	export RELEASE_TAG_NAME="$pkgver"

	# Exclude feature all-languages and updater.
	cargo auditable build -p lapce-app --frozen --release --no-default-features

	cargo auditable build -p lapce-proxy --frozen --release

	# Add missing vulkan dependency.
	patchelf --add-needed libvulkan.so.1 target/release/lapce
}

package() {
	install -D -m755 target/release/lapce -t "$pkgdir"/usr/bin/
	install -D -m755 target/release/lapce-proxy -t "$pkgdir"/usr/bin/

	install -D -m644 extra/linux/dev.lapce.lapce.desktop -t "$pkgdir"/usr/share/applications/
	install -D -m644 extra/images/logo.svg \
		"$pkgdir"/usr/share/icons/hicolor/scalable/apps/dev.lapce.lapce.svg
}

proxy() {
	pkgdesc="Remote proxy server for the Lapce editor"

	amove usr/bin/lapce-proxy
}

sha512sums="
27b8a492cda8dfd5111b9eff12ac02781051f30319e6c83b81af5a31df8a44dd57beb2dd1af2414a95b0cc726de4701ea5876f3b79455dc08b0f4baa84b89bdd  lapce-0.4.0.tar.gz
be07e7af48cf4ac67f9a0ef8fcda382450b89e4e449c9c966c70e00b34a752c7355fd4eb9206ff1f72bee8f37bc13ac13d833ac69396d2187e452ca2f233fbda  use-system-libs.patch
b84269b9cf81987b6e3ec4e7486032574cc1f717808e57e74448e82acac8d81b75f8e0fb023d32ba16116fea5bc9d32630a2d7d9d3afb93c0e291089caaacdb9  cargo.lock.patch
"