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

EAPI=8

inherit desktop systemd xdg

MY_PN="${PN%-bin}"

DESCRIPTION="Graphical manager for snapper snapshots, configs and schedules (pre-compiled)"
HOMEPAGE="https://codeberg.org/NoBodyZ/gsnapper"
SRC_URI="https://codeberg.org/NoBodyZ/gsnapper/releases/download/v${PV}/${MY_PN}-${PV}-x86_64-v3.tar.xz"
S="${WORKDIR}/${MY_PN}-${PV}"

LICENSE="GPL-2"
# Dependent crate licenses
LICENSE+=" Apache-2.0 Apache-2.0-with-LLVM-exceptions MIT MPL-2.0 Unicode-3.0 || ( Apache-2.0 Apache-2.0-with-LLVM-exceptions MIT ) || ( Apache-2.0 LGPL-2.1+ MIT ) || ( Apache-2.0 MIT ) || ( MIT Unlicense )"
SLOT="0"
KEYWORDS="-* ~amd64"
IUSE="cron systemd"
RESTRICT="mirror strip"

RDEPEND="
	>=dev-libs/glib-2.80:2
	>=gui-libs/gtk-4.22:4
	>=gui-libs/libadwaita-1.9:1
	media-libs/graphene
	x11-libs/pango
	sys-apps/dbus
	sys-auth/polkit
	app-backup/snapper
	sys-fs/btrfs-progs
	!app-backup/gsnapper
	cron? ( sys-process/cronie )
	!systemd? ( sys-auth/elogind )
"

QA_PREBUILT="
	usr/bin/gsnapper
	usr/libexec/gsnapper-helper
"

src_install() {
	dobin usr/bin/gsnapper
	exeinto /usr/libexec
	doexe usr/libexec/gsnapper-helper

	insinto /usr/share/dbus-1/system.d
	doins usr/share/dbus-1/system.d/org.gentoo.GSnapper1.conf
	insinto /usr/share/dbus-1/system-services
	doins usr/share/dbus-1/system-services/org.gentoo.GSnapper1.service
	insinto /usr/share/polkit-1/actions
	doins usr/share/polkit-1/actions/org.gentoo.gsnapper.policy

	if use systemd; then
		local unit
		for unit in usr/lib/systemd/system/*; do
			systemd_dounit "${unit}"
		done
	fi

	domenu usr/share/applications/org.gentoo.GSnapper.desktop
	insinto /usr/share/metainfo
	doins usr/share/metainfo/org.gentoo.GSnapper.metainfo.xml
	insinto /usr/share/icons
	doins -r usr/share/icons/hicolor

	if [[ -d usr/share/locale ]]; then
		insinto /usr/share
		doins -r usr/share/locale
	fi

	dodoc README.md CHANGELOG.md
}

pkg_postinst() {
	xdg_pkg_postinst

	elog "gsnapper talks to gsnapper-helper, a root service started on demand"
	elog "by D-Bus: there is no service to enable. polkit asks for the"
	elog "administrator password before any change to snapshots or configs."
	elog ""
	elog "If dbus was already running, reload its configuration once so that it"
	elog "knows the new service:"
	if use systemd; then
		elog "  systemctl reload dbus    (or: busctl call org.freedesktop.DBus"
		elog "  /org/freedesktop/DBus org.freedesktop.DBus ReloadConfig)"
		if use cron; then
			elog ""
			elog "Both cronie and the timers of snapper can run its timeline and"
			elog "cleanup: the hourly cron script of snapper does the same work as"
			elog "the timers. Enable only one of them."
		fi
	else
		elog "  rc-service dbus reload"
		elog ""
		elog "Without systemd, gsnapper needs a session known to elogind, and"
		elog "sys-auth/polkit built with USE=elogind:"
		elog "  rc-update add elogind boot"
		elog "  rc-service elogind start"
		elog ""
		if use cron; then
			elog "The timeline and the plans are run by cronie:"
			elog "  rc-update add cronie default"
			elog "  rc-service cronie start"
		else
			elog "The timeline and the plans need cronie, which USE=cron pulls in."
			elog "Other cron daemons are not supported."
		fi
		if systemd_is_booted; then
			ewarn "This system runs systemd but gsnapper was built with USE=-systemd:"
			ewarn "its units are missing, so the gsnapper service cannot start."
			ewarn "Rebuild it with USE=systemd."
		fi
	fi
}