# Copyright 2023-2024 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

# Autogenerated by pycargoebuild 0.13.2

EAPI=8

CRATES="
"

declare -A GIT_CRATES=(
	[gunzip]='https://github.com/TechHara/gunzip;adfed97f7fa4ad705364e7fc151eaaf0950cd6ba;gunzip-%commit%'
)

inherit cargo desktop xdg

DESCRIPTION="Clash GUI based on tauri"
HOMEPAGE="https://github.com/LibNyanpasu/clash-nyanpasu"
SRC_URI="
	https://github.com/LibNyanpasu/clash-nyanpasu/archive/refs/tags/v${PV}.tar.gz -> ${P}.tar.gz
	https://github.com/gentoo-zh-drafts/clash-nyanpasu/releases/download/v${PV}/${P}-crates.tar.xz
	https://github.com/liuyujielol/gentoo-go-deps/releases/download/${P}/${P}-node_modules-pnpm.tar.xz
	${CARGO_CRATE_URIS}
"
PATCHES=(
	"${FILESDIR}"/clash-nyanpasu-1.5.1-fix-rust-1.80-compile-failure.patch
)

LICENSE="GPL-3"
# Dependent crate licenses
LICENSE+="
	Apache-2.0 Apache-2.0-with-LLVM-exceptions BSD-2 BSD CC0-1.0 ISC MIT
	MPL-2.0 Unicode-DFS-2016 WTFPL-2
"
SLOT="0"
KEYWORDS="~amd64"
IUSE="clash-rs +mihomo"
REQUIRED_USE="
	|| ( clash-rs mihomo )
"

DEPEND="
	app-alternatives/v2ray-geoip
	app-alternatives/v2ray-geosite
	dev-libs/glib:2
	dev-libs/openssl:=
	dev-libs/libayatana-appindicator
	net-libs/libsoup:2.4
	net-libs/webkit-gtk:4=
	x11-libs/cairo
	x11-libs/gdk-pixbuf:2
	x11-libs/gtk+:3
"
RDEPEND="
	${DEPEND}
	clash-rs? ( net-proxy/clash-rs )
	mihomo? ( net-proxy/mihomo )
"
BDEPEND="
	app-misc/jq
	dev-lang/typescript
	dev-lang/quickjs
	sys-apps/moreutils
	sys-apps/pnpm
"

src_prepare() {
	ln -sv "${WORKDIR}/node_modules" "${S}/node_modules" || die
	# pretend we have prebuilt binaries
	mkdir -pv "${S}/backend/tauri/"{sidecar,resources} || die
	touch "${S}/backend/tauri/sidecar/clash-x86_64-unknown-linux-gnu"
	touch "${S}/backend/tauri/sidecar/clash-rs-x86_64-unknown-linux-gnu"
	touch "${S}/backend/tauri/sidecar/mihomo-x86_64-unknown-linux-gnu"
	touch "${S}/backend/tauri/sidecar/mihomo-alpha-x86_64-unknown-linux-gnu"
	touch "${S}/backend/tauri/resources/Country.mmdb"

	jq 'del(.scripts.prepare)' package.json|sponge package.json
	local tauri_conf_json="backend/tauri/tauri.conf.json"
	# only build the excutable
	jq '.tauri.bundle.active = false' ${tauri_conf_json}|sponge ${tauri_conf_json}
	# disable updater
	jq '.tauri.updater.active = false' ${tauri_conf_json}|sponge ${tauri_conf_json}

	default
}

src_compile() {
	set -- pnpm tauri -v build $(usev debug "--debug") -c ./backend/tauri/tauri.conf.json
	einfo "${@}"
	"${@}" || die "tauri build failed"
}

src_install() {
	dobin backend/target/$(usex debug "debug" "release")/clash-nyanpasu
	
	newicon -s 32 backend/tauri/icons/32x32.png clash-nyanpasu.png
	newicon -s 128 backend/tauri/icons/128x128.png clash-nyanpasu.png
	newicon -s 256 backend/tauri/icons/128x128@2x.png clash-nyanpasu.png
	newicon -s scalable src/assets/image/logo.svg clash-nyanpasu.svg

	make_desktop_entry clash-nyanpasu clash-nyanpasu clash-nyanpasu 'Network;Development' 'Terminal=false'

	dosym -r "/usr/share/v2ray/geosite.dat" "/usr/lib/${PN}/resources/geosite.dat"
	dosym -r "/usr/share/v2ray/geoip.dat" "/usr/lib/${PN}/resources/geoip.dat"
}