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

EAPI=8

CRATES="
	aho-corasick@1.1.5
	android_system_properties@0.1.6
	async-trait@0.1.92
	autocfg@1.5.1
	bitflags@2.13.2
	block@0.1.6
	bstr@1.14.0
	bumpalo@3.20.4
	bytes@1.12.2
	cairo-rs@0.22.11
	cairo-sys-rs@0.22.9
	cc@1.12.0
	cfg-expr@0.20.11
	cfg-if@1.0.5
	cfg_aliases@0.2.2
	chrono@0.4.45
	core-foundation-sys@0.8.7
	directories@6.0.0
	dirs-sys@0.5.0
	endi@1.1.1
	equivalent@1.0.3
	errno@0.3.15
	fastrand@2.5.0
	field-offset@0.3.6
	find-msvc-tools@0.1.16
	futures-channel@0.3.34
	futures-core@0.3.34
	futures-executor@0.3.34
	futures-io@0.3.34
	futures-macro@0.3.34
	futures-task@0.3.34
	futures-util@0.3.34
	gdk-pixbuf-sys@0.22.9
	gdk-pixbuf@0.22.11
	gdk4-sys@0.11.5
	gdk4@0.11.5
	getrandom@0.4.3
	gettext-rs@0.9.0
	gettext-sys@0.27.1
	gio-sys@0.22.11
	gio@0.22.11
	glib-build-tools@0.22.8
	glib-macros@0.22.11
	glib-sys@0.22.11
	glib@0.22.11
	gobject-sys@0.22.9
	graphene-rs@0.22.8
	graphene-sys@0.22.9
	gsk4-sys@0.11.5
	gsk4@0.11.5
	gtk4-macros@0.11.5
	gtk4-sys@0.11.5
	gtk4@0.11.5
	hashbrown@0.17.2
	heck@0.5.0
	hex@0.4.3
	iana-time-zone-haiku@0.1.2
	iana-time-zone@0.1.65
	indexmap@2.14.2
	js-sys@0.3.107
	lazy_static@1.5.1
	libadwaita-sys@0.9.2
	libadwaita@0.9.2
	libc@0.2.191
	libredox@0.1.25
	linux-raw-sys@0.12.1
	locale_config@0.3.0
	log@0.4.35
	malloc_buf@0.0.6
	memchr@2.8.3
	memoffset@0.9.1
	mio@1.2.5
	nix@0.32.0
	num-traits@0.2.19
	objc-foundation@0.1.1
	objc@0.2.7
	objc_id@0.1.1
	once_cell@1.21.4
	option-ext@0.2.0
	ordered-stream@0.2.0
	pango-sys@0.22.11
	pango@0.22.9
	pin-project-lite@0.2.18
	pkg-config@0.3.34
	proc-macro-crate@3.5.0
	proc-macro2@1.0.107
	quote@1.0.47
	r-efi@6.0.0
	redox_users@0.5.3
	regex-automata@0.4.19
	regex-syntax@0.8.12
	regex@1.13.2
	rustc_version@0.4.1
	rustix@1.1.6
	rustversion@1.0.23
	semver@1.0.28
	serde@1.0.229
	serde_core@1.0.229
	serde_derive@1.0.229
	serde_repr@0.1.21
	serde_spanned@1.1.3
	sharded-slab@0.1.7
	shlex@2.0.1
	signal-hook-registry@1.4.8
	similar@3.2.1
	slab@0.4.12
	smallvec@1.16.6
	socket2@0.6.5
	syn@2.0.119
	syn@3.0.10
	system-deps@7.0.8
	system-deps@9.0.1
	target-lexicon@0.13.5
	temp-dir@0.1.16
	tempfile@3.27.0
	thiserror-impl@2.0.21
	thiserror@2.0.21
	thread_local@1.1.10
	tokio-macros@2.7.2
	tokio@1.54.1
	toml@1.2.3+spec-1.1.0
	toml_datetime@1.1.3+spec-1.1.0
	toml_edit@0.25.21+spec-1.1.0
	toml_parser@1.1.7+spec-1.1.0
	toml_writer@1.2.0+spec-1.1.0
	tracing-attributes@0.1.31
	tracing-core@0.1.36
	tracing-journald@0.3.2
	tracing-subscriber@0.3.23
	tracing@0.1.44
	uds_windows@1.2.1
	unicode-ident@1.0.27
	uuid@1.29.3
	valuable@0.1.1
	version-compare@0.2.1
	wasi@0.11.1+wasi-snapshot-preview1
	wasm-bindgen-macro-support@0.2.130
	wasm-bindgen-macro@0.2.130
	wasm-bindgen-shared@0.2.130
	wasm-bindgen@0.2.130
	winapi-i686-pc-windows-gnu@0.4.0
	winapi-x86_64-pc-windows-gnu@0.4.0
	winapi@0.3.9
	windows-core@0.62.2
	windows-implement@0.60.2
	windows-interface@0.59.3
	windows-link@0.2.1
	windows-result@0.4.1
	windows-strings@0.5.1
	windows-sys@0.61.2
	winnow@1.0.5
	zbus@6.6.0
	zbus_macros@6.6.0
	zbus_utils@6.2.0
	zcheapstr@1.4.0
	zruntime@0.2.4
"
RUST_MIN_VER="1.92.0"

inherit cargo desktop systemd xdg

DESCRIPTION="Graphical manager for snapper snapshots, configs and schedules (GTK4/libadwaita)"
HOMEPAGE="https://codeberg.org/NoBodyZ/gsnapper"
SRC_URI="
	https://codeberg.org/NoBodyZ/gsnapper/archive/v${PV}.tar.gz -> ${P}.tar.gz
	${CARGO_CRATE_URIS}
"
S="${WORKDIR}/${PN}"

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"

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_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
}