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

EAPI=8

inherit go-module shell-completion toolchain-funcs

DESCRIPTION="CLI client for Google Tasks, written in Go"
HOMEPAGE="
	https://gtasks.sidv.dev/
	https://github.com/BRO3886/gtasks
"
# TODO: replace the self-hosted vendor tarball with the upstream release asset
# once merged: https://github.com/BRO3886/gtasks/pull/68
SRC_URI="
	https://github.com/BRO3886/gtasks/archive/refs/tags/v${PV}.tar.gz
		-> ${P}.tar.gz
	https://github.com/vitaly-zdanevich/gentoo-overlay/releases/download/${P}/${P}-vendor.tar.xz
"

LICENSE="Apache-2.0"
# Dependent module licenses
LICENSE+="
	BSD BSD-2 MIT
"
SLOT="0"
KEYWORDS="~amd64 ~arm64"

RDEPEND="!app-misc/gtasks-bin"
BDEPEND=">=dev-lang/go-1.26.10"

DOCS=( README.md )

src_compile() {
	ego build -ldflags "-X github.com/BRO3886/gtasks/cmd.Version=v${PV}" -o ${PN} .

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

src_install() {
	dobin ${PN}
	einstalldocs

	if ! tc-is-cross-compiler; then
		dobashcomp "${T}/comp/${PN}"
		dozshcomp "${T}/comp/_${PN}"
		dofishcomp "${T}/comp/${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
}