# 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=(
	[async-openai-macros]='https://github.com/our-forks/async-openai;95b52ebdedf42143083cf3d6f0e0be7c84e9c808;async-openai-%commit%/async-openai-macros'
	[async-openai]='https://github.com/our-forks/async-openai;95b52ebdedf42143083cf3d6f0e0be7c84e9c808;async-openai-%commit%/async-openai'
	[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="98a7d5949053c02d631757dd9272b09b73c463e0"
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 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

	# Disable auto-updates
	sed \
		-e '1i #![allow(unreachable_code)]' \
		-e '/fn env_installer(/a \    return Some("portage");' \
		-i crates/codegen/xai-grok-update/src/auto_update.rs || die

	# Cargo offline fetch workaround
	local url commit path
	IFS=";" read -r url commit path <<<"${GIT_CRATES[async-openai]}"
	local ASYNC_OPENAI_GIT="async-openai = { git = \"${url}.git\", rev = \"${commit}\""
	local ASYNC_OPENAI_PATH="async-openai = \\{ path = \"${WORKDIR}/async-openai-${commit}/async-openai\""

	sed "s|${ASYNC_OPENAI_GIT}|${ASYNC_OPENAI_PATH}|" \
		-i "${S}/Cargo.toml" || die
}

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
}