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

EAPI=8

inherit shell-completion toolchain-funcs

MY_PN=${PN%-bin}
MY_URI="https://github.com/BRO3886/gtasks/releases/download/v${PV}/${MY_PN}_linux"

DESCRIPTION="Prebuilt CLI client for Google Tasks, written in Go"
HOMEPAGE="
	https://gtasks.sidv.dev/
	https://github.com/BRO3886/gtasks
"
SRC_URI="
	amd64? ( ${MY_URI}_amd64_v${PV}.tar.gz )
	arm64? ( ${MY_URI}_arm64_v${PV}.tar.gz )
"
S="${WORKDIR}"

# Includes the statically linked Go dependencies from the source package.
LICENSE="Apache-2.0 BSD BSD-2 MIT"
SLOT="0"
KEYWORDS="-* ~amd64 ~arm64"

RDEPEND="!app-misc/gtasks"

QA_PREBUILT="usr/bin/${MY_PN}"

src_compile() {
	if ! tc-is-cross-compiler; then
		mkdir -p "${T}/comp" || die
		local -x HOME="${T}" XDG_CONFIG_HOME="${T}/.config"
		./${MY_PN} completion bash > "${T}/comp/${MY_PN}" || die
		./${MY_PN} completion zsh > "${T}/comp/_${MY_PN}" || die
		./${MY_PN} completion fish > "${T}/comp/${MY_PN}.fish" || die
	fi
}

src_install() {
	dobin ${MY_PN}

	if ! tc-is-cross-compiler; then
		dobashcomp "${T}/comp/${MY_PN}"
		dozshcomp "${T}/comp/_${MY_PN}"
		dofishcomp "${T}/comp/${MY_PN}.fish"
	fi
}

pkg_postinst() {
	if [[ -z ${REPLACING_VERSIONS} ]]; then
		elog "Before running 'gtasks login', configure your Google OAuth2 credentials"
		elog "via GTASKS_CLIENT_ID and GTASKS_CLIENT_SECRET or ~/.config/gtasks/config.toml."
		elog "See https://gtasks.sidv.dev/docs/configuration/ for setup instructions."
	fi
}