# Copyright 2026 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

# Autogenerated by pycargoebuild 0.15.1

EAPI=8

CRATES="
"

declare -A GIT_CRATES=(
	[clash_verge_logger]='https://github.com/clash-verge-rev/clash-verge-logger;e4768e3852c4868ed86e7210df82c1178467820d;clash-verge-logger-%commit%'
	[clash_verge_service_ipc]='https://github.com/clash-verge-rev/clash-verge-service-ipc;360f205a12c6c2fe229d09252a41e3253ed6be3e;clash-verge-service-ipc-%commit%'
	[dark-light]='https://github.com/rust-dark-light/dark-light;0f18d2fbcaa5d1c175db8aae7d53428988d7e961;dark-light-%commit%'
	[sysproxy]='https://github.com/clash-verge-rev/sysproxy-rs;6aa9ecb1c6bb9779b9dc8e4adc68780fc1d5edb6;sysproxy-rs-%commit%'
	[tauri-plugin-mihomo]='https://github.com/clash-verge-rev/tauri-plugin-mihomo;1cc80bc0fbe1245315617f4cecd93710a152325b;tauri-plugin-mihomo-%commit%'
)

RUST_MIN_VER="1.91"
inherit cargo desktop dot-a xdg

DESCRIPTION="GUI client based on Tauri for tailored proxy experience"
HOMEPAGE="
	https://www.clashverge.dev
	https://github.com/clash-verge-rev/clash-verge-rev
"
# Web dist tarball generared from liuyujielol/gentoo-go-deps/blob/cvr/.github/workflows/generator.yml
SRC_URI="
	https://github.com/clash-verge-rev/clash-verge-rev/archive/refs/tags/v${PV}.tar.gz -> ${P}.tar.gz
	https://github.com/liuyujielol/gentoo-go-deps/releases/download/${P}/${P/-rev/}-crates.tar.xz
	https://github.com/liuyujielol/gentoo-go-deps/releases/download/${P}/${P}-web.tar.xz
"
SERVICE_PV="2.3.0"
SERVICE_P="clash-verge-service-ipc-${SERVICE_PV}"
SRC_URI+="
	https://github.com/clash-verge-rev/clash-verge-service-ipc/archive/refs/tags/v${SERVICE_PV}.tar.gz -> ${SERVICE_P}.tar.gz
	https://github.com/liuyujielol/gentoo-go-deps/releases/download/${SERVICE_P}/clash_verge_service_ipc-${SERVICE_PV}-crates.tar.xz
"
SRC_URI+=" ${CARGO_CRATE_URIS}"
LICENSE="GPL-3"
# Dependent crate licenses
LICENSE+="
	0BSD Apache-2.0 Apache-2.0-with-LLVM-exceptions BSD Boost-1.0
	CC0-1.0 CDLA-Permissive-2.0 GPL-3 ISC MIT MPL-2.0 MPL-2.0 openssl
	Unicode-3.0 WTFPL-2 ZLIB BZIP2
"
SLOT="0"
KEYWORDS="~amd64"

COMMON_DEPEND="
	!net-proxy/clash-verge-bin
	dev-libs/openssl:0/3
	gnome-base/librsvg:2
	net-proxy/mihomo
	net-libs/webkit-gtk:4.1
"
RDEPEND="
	${COMMON_DEPEND}
	dev-libs/libayatana-appindicator
"
BDEPEND="
	${COMMON_DEPEND}
	app-misc/jq
	dev-build/cmake
	dev-util/patchelf
	sys-apps/moreutils
"

src_prepare() {
	default

	# FIXME: tempfile or moreutils?
	#jq ... "${S}/package.json" > tmp.json && mv tmp.json "${S}/package.json" || die
	pushd "${T}" >/dev/null || die
	jq '.bundle.createUpdaterArtifacts = false' "${S}/src-tauri/tauri.conf.json" | sponge "${S}/src-tauri/tauri.conf.json" || die
	popd >/dev/null || die

	mv -v "${WORKDIR}/dist" "${S}/" || die
}

src_compile() {
	#aws-lc-sys-0.38.0/aws-lc/third_party/jitterentropy/jitterentropy-library/src/jitterentropy-base.c:
	#The CPU Jitter random number generator must not be compiled with optimizations.
	#Force use of cmake to auto apply -O0 to related parts
	export AWS_LC_SYS_CMAKE_BUILDER=1

	lto-guarantee-fat

	# clash-verge-service-ipc
	pushd "${WORKDIR}/${SERVICE_P}" >/dev/null || die
	cargo_src_compile --features standalone
	popd >/dev/null || die

	# FIXME: Can't use $(rust_abi) here, build scripts check for -gnu nonetheless
	# FIXME: Maybe musl is supported, just name it -gnu and call it a day
	# NOTE: This list is from https://github.com/clash-verge-rev/clash-verge-rev/blob/dev/scripts/prebuild.mjs
	einfo "CHOST: ${CHOST}"
	einfo "CARCH: ${ARCH}"
	# Taken from gentoo/eclass/rust-toolchain.eclass
	my_rust_abi() {
		local CTARGET=${1:-${CHOST}}
		case ${CTARGET%%*-} in
			aarch64*gnu)      echo aarch64-unknown-linux-gnu;;
			aarch64*musl)     echo aarch64-unknown-linux-gnu;; # -musl changed to -gnu
			armv7a*h*)        echo armv7-unknown-linux-gnueabihf;;
			i?86*)            echo i686-unknown-linux-gnu;;
			loongarch64*)     echo loongarch64-unknown-linux-gnu;;
			riscv64*gnu)      echo riscv64gc-unknown-linux-gnu;;
			riscv64*musl)     echo riscv64gc-unknown-linux-gnu;; # -musl changed to -gnu
			x86_64*gnu)       echo x86_64-unknown-linux-gnu;;
			x86_64*musl)      echo x86_64-unknown-linux-gnu;; # -musl changed to -gnu
			*)                die "Unsupported";;
		esac
	}

	mkdir "${S}"/src-tauri/sidecar || die
	mkdir "${S}"/src-tauri/resources || die
	CVR_SERVICE_TARGET="${WORKDIR}/${SERVICE_P}/$(cargo_target_dir)/clash-verge-service"

	cp -v "${CVR_SERVICE_TARGET}" "${S}/src-tauri/sidecar/clash-verge-service-$(my_rust_abi)" || die
	cp -v "${CVR_SERVICE_TARGET}"-install "${S}/src-tauri/sidecar/clash-verge-service-install-$(my_rust_abi)" || die
	cp -v "${CVR_SERVICE_TARGET}"-uninstall "${S}/src-tauri/sidecar/clash-verge-service-uninstall-$(my_rust_abi)" || die
	# Pretend we have those prebuilt mihomo binaries
	touch "${S}"/src-tauri/sidecar/verge-mihomo{,-alpha}-"$(my_rust_abi)" || die

	cd "${S}" || die
	cargo_src_compile
}

src_install() {
	dobin "$(cargo_target_dir)/clash-verge"

	dobin "$(cargo_target_dir)/clash-verge-service"
	dobin "$(cargo_target_dir)/clash-verge-service-install"
	dobin "$(cargo_target_dir)/clash-verge-service-uninstall"

	for size in 32 128 256; do
		if [[ "${size}" == 256 ]]; then
			newicon -s 256 "${S}/src-tauri/icons/128x128@2x.png" clash-verge.png
		else
			newicon -s "${size}" "${S}/src-tauri/icons/${size}x${size}.png" clash-verge.png
		fi
	done
	cat <<-EOF > "${S}/clash_verge.desktop" || die
	[Desktop Entry]
	Categories=Development;
	Comment=Clash Verge Rev
	Exec=clash-verge %u
	StartupWMClass=clash-verge
	Icon=clash-verge
	Name=Clash Verge
	Terminal=false
	Type=Application
	MimeType=x-scheme-handler/clash;

	EOF

	domenu "${S}/clash_verge.desktop"

	dosym -r /usr/bin/mihomo /usr/bin/verge-mihomo
	dosym -r /usr/bin/mihomo /usr/bin/verge-mihomo-alpha
}