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

EAPI=8

RUST_MIN_VER="1.88.0"

inherit cargo shell-completion

DESCRIPTION="CLI app for Topiary, the universal code formatter."
HOMEPAGE="https://topiary.tweag.io"
SRC_URI="https://github.com/tweag/topiary/releases/download/v${PV}/source.tar.gz -> ${P}.tar.gz"
SRC_URI+=" https://github.com/gentoo-crate-dist/${PN}/releases/download/v${PV}/${P}-crates.tar.xz"

S="${WORKDIR}/${PN}-cli-${PV}"

LICENSE="MIT"
# Dependent crate licenses
LICENSE+="
	Apache-2.0 BSD CDLA-Permissive-2.0 ISC LGPL-3 MIT MPL-2.0 MPL-2.0
	Unicode-3.0 ZLIB
"
SLOT="0"
KEYWORDS="~amd64"

DEPEND="
	dev-libs/openssl:0=
"
RDEPEND="${DEPEND}"
BDEPEND="
	virtual/pkgconfig
"

src_configure() {
	# high magic to allow system-libs
	export OPENSSL_NO_VENDOR=true
	export RUSTONIG_SYSTEM_LIBONIG=1
	export LIBSSH2_SYS_USE_PKG_CONFIG=1
	export PKG_CONFIG_ALLOW_CROSS=1

	cargo_src_configure
}

src_compile() {
	cargo_src_compile

	TOPIARY_BIN="$(cargo_target_dir)/${PN}"

	# Prepare shell completion generation
	mkdir completions || die
	local shell
	for shell in bash fish zsh; do
		"${TOPIARY_BIN}" completion ${shell} > "completions/${PN}.${shell}" || die
	done
}

src_install() {
	dobin "$(cargo_target_dir)/${PN}"

	newbashcomp "completions/${PN}.bash" "${PN}"
	dofishcomp "completions/${PN}.fish"
	newzshcomp "completions/${PN}.zsh" "_${PN}"
}
