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

# Autogenerated by pycargoebuild 0.16.0

EAPI=8

RUST_MIN_VER="1.92.0"

CRATES="
"

declare -A GIT_CRATES=(
	[nucleo-matcher]='https://github.com/helix-editor/nucleo;5b74652e482f7c07d827f18c6d21e7540c242c69;nucleo-%commit%/matcher'
	[nucleo]='https://github.com/helix-editor/nucleo;5b74652e482f7c07d827f18c6d21e7540c242c69;nucleo-%commit%'
)

inherit cargo check-reqs shell-completion

# no tagged releases
MY_COMMIT="cb203a893d04ad15881dc0015ba77b7ff53f4ab5"
DESCRIPTION="SpaceXAI's coding agent harness and TUI"
HOMEPAGE="
	https://x.ai/cli
	https://x.ai/open-source
	https://github.com/xai-org/grok-build
"
SRC_URI="
	https://github.com/xai-org/${PN}/archive/${MY_COMMIT}.tar.gz -> ${P}.gh.tar.gz
	https://github.com/fasys-crate-dist/${PN}/releases/download/v${PV}/${P}-crates.tar.xz
	${CARGO_CRATE_URIS}
"
S="${WORKDIR}/${PN}-${MY_COMMIT}"

LICENSE="Apache-2.0"
# Dependent crate licenses
LICENSE+="
	Apache-2.0 BSD-2 BSD Boost-1.0 CC0-1.0 CDLA-Permissive-2.0 EPL-2.0
	ISC MIT MIT-0 MPL-2.0 Unicode-3.0 Unicode-DFS-2016 WTFPL-2 ZLIB
"
SLOT="0"
KEYWORDS="~amd64" # ~arm64: ugrep
# TODO add feature USE flags?
IUSE="test"

RESTRICT="!test? ( test )"

DEPEND="
	sys-apps/bfs
	sys-apps/ripgrep
	sys-apps/ugrep
"
RDEPEND="${DEPEND}"
BDEPEND="
	>=dev-libs/protobuf-25.6[protoc]
"

QA_FLAGS_IGNORED="usr/bin/.*"

CHECKREQS_DISK_BUILD=10G # 9.7G

pkg_setup() {
	check-reqs_pkg_setup
	rust_pkg_setup
}

src_prepare() {
	default

	# Do not bundle ripgrep
	local f
	for f in crates/codegen/xai-grok-{shell,tools}/build.rs; do
		sed \
			-e '1i #![allow(unreachable_code)]' \
			-e '/fn main() ->/a \    return Ok(());' \
			-i "$f" || die
	done
}

src_test() {
	# TODO more tests
	cargo_src_test --package xai-grok-pager-bin
}

src_install() {
	newbin "$(cargo_target_dir)/xai-grok-pager" grok
	dosym grok /usr/bin/agent

	local grok_bin="${ED}/usr/bin/grok"
	"${grok_bin}" completions bash > grok || die
	"${grok_bin}" completions fish > grok.fish || die
	"${grok_bin}" completions zsh > _grok || die

	dobashcomp grok
	dofishcomp grok.fish
	dozshcomp _grok
}