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

EAPI=8

CRATES=""
RUST_MIN_VER="1.75.0"

inherit cargo git-r3 systemd xdg-utils

DESCRIPTION="Graphical package manager for Gentoo Linux (GTK4/libadwaita Portage frontend)"
HOMEPAGE="https://git.xenonet.cc/N3oTraX/gpkg"
EGIT_REPO_URI="https://git.xenonet.cc/N3oTraX/gpkg.git"

LICENSE="GPL-2"
SLOT="0"
IUSE="+vte sourceview systemd"

DEPEND="
	>=gui-libs/gtk-4.12:4
	>=gui-libs/libadwaita-1.4:1
	>=dev-libs/glib-2.76:2
	sys-apps/dbus
	vte? ( >=gui-libs/vte-0.74:2.91-gtk4 )
	sourceview? ( >=gui-libs/gtksourceview-5.10:5 )
"
RDEPEND="
	${DEPEND}
	sys-apps/portage
	app-portage/gentoolkit
	app-portage/eix
	sys-auth/polkit
"
BDEPEND="
	virtual/pkgconfig
"

QA_FLAGS_IGNORED="
	usr/bin/gpkg
	usr/bin/gpkg-daemon
"

src_unpack() {
	git-r3_src_unpack
	cargo_live_src_unpack
}

src_configure() {
	cargo_src_configure
}

src_compile() {
	# Build daemon (no features needed)
	cargo_src_compile -p gpkg-daemon

	# Build GUI with optional features
	local gui_args=( -p gpkg-gui )

	local features=()
	use vte && features+=( vte )
	use sourceview && features+=( sourceview )

	if [[ ${#features[@]} -gt 0 ]]; then
		local myfeatures
		myfeatures=$(IFS=,; echo "${features[*]}")
		gui_args+=( --features "${myfeatures}" )
	fi

	cargo_src_compile "${gui_args[@]}"
}

src_test() {
	cargo_src_test --workspace
}

src_install() {
	# Binaries
	dobin "$(cargo_target_dir)/gpkg"
	dobin "$(cargo_target_dir)/gpkg-daemon"

	# D-Bus system bus config (required for daemon to claim bus name)
	insinto /etc/dbus-1/system.d
	doins data/dbus/org.gentoo.PkgMngt.conf

	# D-Bus activatable service (auto-start daemon on first D-Bus call)
	insinto /usr/share/dbus-1/system-services
	doins data/dbus/org.gentoo.PkgMngt.service

	# Polkit policy (authorization for privileged operations)
	insinto /usr/share/polkit-1/actions
	doins data/polkit/org.gentoo.pkgmngt.policy

	# Desktop file
	insinto /usr/share/applications
	doins data/org.gentoo.PkgMngt.desktop

	# Icon
	insinto /usr/share/icons/hicolor/scalable/apps
	doins data/icons/hicolor/scalable/apps/org.gentoo.PkgMngt.svg

	# AppStream metainfo
	insinto /usr/share/metainfo
	doins data/org.gentoo.PkgMngt.metainfo.xml

	# Systemd service (with hardening)
	if use systemd; then
		systemd_dounit systemd/gpkg-daemon.service
	fi

	# OpenRC init script (always install, even with systemd)
	newinitd "${FILESDIR}/gpkg-daemon.initd" gpkg-daemon
	newconfd "${FILESDIR}/gpkg-daemon.confd" gpkg-daemon
}

pkg_postinst() {
	xdg_icon_cache_update
	xdg_desktop_database_update

	elog ""
	elog "To start the gpkg daemon:"
	elog ""
	if use systemd; then
		elog "  systemctl enable --now gpkg-daemon"
	else
		elog "  rc-update add gpkg-daemon default"
		elog "  rc-service gpkg-daemon start"
	fi
	elog ""
	elog "Then launch the GUI:  gpkg"
	elog ""
	elog "The console tab works without the daemon."
	elog "All other tabs require a running gpkg-daemon."
	elog ""
}

pkg_postrm() {
	xdg_icon_cache_update
	xdg_desktop_database_update
}