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

EAPI=8

CRATES=""
RUST_MIN_VER="1.92.0"

inherit cargo desktop git-r3 systemd xdg

DESCRIPTION="Graphical manager for snapper snapshots, configs and schedules (GTK4/libadwaita)"
HOMEPAGE="https://codeberg.org/NoBodyZ/gsnapper"
EGIT_REPO_URI="https://codeberg.org/NoBodyZ/gsnapper.git"
EGIT_BRANCH="develop"

LICENSE="GPL-2"
SLOT="0"
IUSE="cron systemd"

DEPEND="
	>=dev-libs/glib-2.80:2
	>=gui-libs/gtk-4.22:4
	>=gui-libs/libadwaita-1.9:1
	sys-apps/dbus
"
RDEPEND="
	${DEPEND}
	app-backup/snapper
	sys-auth/polkit
	sys-fs/btrfs-progs
	!app-backup/gsnapper-bin
	cron? ( sys-process/cronie )
	!systemd? ( sys-auth/elogind )
"
BDEPEND="
	sys-devel/gettext
	virtual/pkgconfig
"

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

src_unpack() {
	git-r3_src_unpack
	# cargo_live_src_unpack runs cargo fetch in the current directory.
	cd "${S}" || die
	cargo_live_src_unpack
}

src_compile() {
	cargo_src_compile --package gsnapper --package gsnapper-helper
}

src_test() {
	# Integration tests need root, a real snapperd and the test VM: they are
	# marked #[ignore] and skipped here.
	cargo_src_test --workspace
}

src_install() {
	dobin "$(cargo_target_dir)/gsnapper"
	exeinto /usr/libexec
	doexe "$(cargo_target_dir)/gsnapper-helper"

	# Root service: D-Bus policy and activation file, polkit actions.
	insinto /usr/share/dbus-1/system.d
	doins data/dbus/org.gentoo.GSnapper1.conf
	insinto /usr/share/dbus-1/system-services
	doins data/dbus/org.gentoo.GSnapper1.service
	insinto /usr/share/polkit-1/actions
	doins data/polkit/org.gentoo.gsnapper.policy

	# Helper unit (started by D-Bus activation, never enabled) and the
	# template units of the plans with their own calendar.
	if use systemd; then
		local unit
		for unit in data/systemd/*.service data/systemd/*.timer; do
			systemd_dounit "${unit}"
		done
	fi

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

	local po lang
	for po in po/*.po; do
		[[ -e ${po} ]] || continue
		lang=$(basename "${po}" .po)
		msgfmt --check -o "${T}/${lang}.mo" "${po}" || die "msgfmt failed for ${po}"
		insinto "/usr/share/locale/${lang}/LC_MESSAGES"
		newins "${T}/${lang}.mo" gsnapper.mo
	done

	einstalldocs
	dodoc CHANGELOG.md docs/ARCHITECTURE.md docs/HELPER-API.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
}