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

EAPI=8

inherit desktop systemd xdg-utils

DESCRIPTION="Graphical package manager for Gentoo Linux (pre-compiled binary)"
HOMEPAGE="https://codeberg.org/NoBodyZ/gpkg"
SRC_URI="https://codeberg.org/NoBodyZ/gpkg/releases/download/v${PV}/${PN/-bin/}-${PV}-x86_64-v3.tar.xz"

S="${WORKDIR}/${PN/-bin/}-${PV}"

LICENSE="GPL-2"
LICENSE+=" Apache-2.0 BSD ISC MIT MPL-2.0 Unicode-3.0"
SLOT="0"
KEYWORDS="-* ~amd64"
IUSE="systemd"
RESTRICT="mirror strip"

RDEPEND="
	!app-portage/gpkg
	>=gui-libs/gtk-4.12:4
	>=gui-libs/libadwaita-1.4:1
	>=dev-libs/glib-2.76:2
	sys-apps/dbus
	media-libs/graphene
	x11-libs/cairo
	x11-libs/pango
	x11-libs/gdk-pixbuf:2
	media-libs/freetype:2
	media-libs/fontconfig
	>=gui-libs/vte-0.74:2.91-gtk4
	>=gui-libs/gtksourceview-5.10:5
	sys-apps/portage
	app-portage/gentoolkit
	app-portage/eix
	sys-auth/polkit
"

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

src_install() {
	# Binaries
	dobin usr/bin/gpkg
	dobin usr/bin/gpkg-daemon

	# D-Bus system bus configuration
	insinto /etc/dbus-1/system.d
	doins usr/share/dbus-1/system.d/org.gentoo.PkgMngt.conf

	# D-Bus service activation file
	insinto /usr/share/dbus-1/system-services
	doins usr/share/dbus-1/system-services/org.gentoo.PkgMngt.service

	# Polkit authorization policies
	insinto /usr/share/polkit-1/actions
	doins usr/share/polkit-1/actions/org.gentoo.pkgmngt.policy

	# Desktop file
	domenu usr/share/applications/org.gentoo.PkgMngt.desktop

	# Icons - install entire hicolor tree
	insinto /usr/share
	doins -r usr/share/icons

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

	# CSS stylesheet
	insinto /usr/share/gpkg
	doins usr/share/gpkg/style.css

	# Systemd service unit
	if use systemd; then
		systemd_dounit usr/lib/systemd/system/gpkg-daemon.service
	fi

	# OpenRC init script and config
	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
}