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

EAPI=8

VALA_MIN_API_VERSION="0.52"
GNOME_MIN_VERSIOM="40" # Its what is said in the mason.build file

inherit gnome2-utils meson vala xdg

DESCRIPTION="Desktop Environment based on GNOME 3"
HOMEPAGE="https://github.com/BuddiesOfBudgie/"
SRC_URI="https://github.com/BuddiesOfBudgie/${PN}/releases/download/v${PV}/${PN}-v${PV}.tar.xz -> ${P}.tar.xz"

LICENSE="GPL-2 LGPL-2.1"
SLOT="0"
KEYWORDS="~amd64 ~x86 ~arm ~arm64"
IUSE="gtk-doc +policykit stateless +bluetooth +hibernate networkmanager systemd modemmanager smartcard"

COMMON_DEPEND="
	>=sys-apps/accountsservice-0.6.55[vala]

	>=budgie-base/budgie-desktop-services-1.0.2
	>=budgie-base/budgie-session-1.0.0

	>=gnome-base/gnome-desktop-${GNOME_MIN_VERSIOM}:3
	>=gnome-base/gnome-settings-daemon-47.2
	>=gnome-base/gsettings-desktop-schemas-${GNOME_MIN_VERSIOM}
	>=gnome-base/gnome-menus-3.10.3:3[introspection]
	>=x11-libs/gtk+-3.24.35:3[X,introspection]
	gui-libs/gtk-layer-shell[introspection,vala]
	x11-libs/gdk-pixbuf:2

	gnome-base/libgtop
	media-libs/libcanberra-gtk3:=
	>=xfce-base/libxfce4windowing-4.20.4[vala]
	>=dev-libs/libgee-0.20.0:0.8=
	>=dev-libs/libpeas-2.0.7:2=[vala]
	
	>=media-libs/libpulse-2.0[glib]
	media-libs/gstreamer:1.0
	>=x11-libs/libnotify-0.7

	>=sys-apps/xdg-desktop-portal-gtk-1.14.0
	gui-libs/xdg-desktop-portal-wlr

	gnome-extra/zenity
	sys-power/upower
	sys-apps/util-linux

	gui-apps/wdisplays
	gui-wm/labwc
	dev-python/psutil
	x11-apps/mesa-progs

	policykit? ( >=sys-auth/polkit-0.105[introspection] )
	networkmanager? ( gnome-extra/nm-applet )
"
# Removed for testing release for 10.10.3:
#	>=app-i18n/ibus-1.5.10[vala]
#	>=x11-libs/libwnck-3.36:3
# Added: libgtop
# 
# networkmanager useflag for applet support

RDEPEND="
	gui-apps/grim
	gui-apps/slurp
	x11-misc/gammastep
	gui-apps/wlopm
	gui-apps/swaybg[gdk-pixbuf]
	gui-apps/swayidle
	
	|| (
		gui-apps/gtklock 
		gui-apps/swaylock
	)
	media-gfx/imagemagick[jpeg,jpegxl]
	bluetooth? ( 
		net-wireless/bluez
		net-wireless/blueman
	)
	modemmanager? ( gnome-base/gnome-settings-daemon[modemmanager] )
	smartcard? ( gnome-base/gnome-settings-daemon[smartcard] )

	systemd? ( dev-python/python-systemd )
	sys-apps/gnome-disk-utility[gnome]
	
	x11-wm/mutter
"

BDEPEND="
	$(vala_depend)
	dev-lang/sassc
	sys-libs/glibc
	dev-libs/glib
	gtk-doc? ( dev-util/gtk-doc )
"
# Removed for 10.10.3 release testing: 	dev-util/intltool

DEPEND="
	${COMMON_DEPEND}
	>=dev-libs/gobject-introspection-1.44.0
"

src_unpack() {
	unpack ${P}.tar.xz
}

src_prepare() {
	sed -i -e "/add_install_script.*meson_post_install\.sh/d" meson.build || die

	# New release, I want to leave as much logging in but for non-systemd users I cannot force systemd purely for logging only.
	# I hate this approach but other than outright removing the logging as whole I rather go this way..
	#if use !systemd; then
	#	PATCHES+=( "${FILESDIR}"/10.10.X/remove_labwc_journald_logging.patch )
	#fi
	#
	# Should not be needed for 10.10.3 anymore.. 
	# useflag systemd left in due to allowing logging for system consistency..

	vala_setup
	default
}

src_configure() {
	# sort out the right GSD components to get based on gnome-settings-daemon useflags
	# The only 2 useflags that are applied to this are modemmanager and smartcard, rest seems fine
	GSD_COMPONENTS="A11ySettings,Datetime,DiskUtilityNotify,Housekeeping,Power,PrintNotifications,Rfkill,Sharing,Sound,UsbProtection"
	GSD_COMPONENTS_EXCLUDED=""
	if use modemmanager ; then 
		GSD_COMPONENTS="${GSD_COMPONENTS},Wwan"
	else
		GSD_COMPONENTS_EXCLUDED="${GSD_COMPONENTS_EXCLUDED}Wwan"
	fi
	if use smartcard ; then 
		GSD_COMPONENTS="${GSD_COMPONENTS},Smartcard"
	else
		if [[ $GSD_COMPONENTS_EXCLUDED ]]
		then
			GSD_COMPONENTS_EXCLUDED="${GSD_COMPONENTS_EXCLUDED},Smartcard"
		else
			GSD_COMPONENTS_EXCLUDED="${GSD_COMPONENTS_EXCLUDED}Smartcard"
		fi
	fi


	local emesonargs=(
		$(meson_use gtk-doc with-gtk-doc)
		$(meson_use policykit with-polkit)
		$(meson_use stateless with-stateless)
		$(meson_use bluetooth with-bluetooth)
		$(meson_use hibernate with-hibernate)
		-Dwith-oo7=false
		-Dgsd-components=${GSD_COMPONENTS}
		-Dgsd-components-excluded=${GSD_COMPONENTS_EXCLUDED}
	)
	meson_src_configure
}
# 007 remains disabled since 007 does not exist for gentoo, its fedora secret service provider.
# basically, think passwords and app tokens, by default use gnome keyring instead.
#
# Its part of rust in gentoo? vague, skipping for now

pkg_postinst() {
	xdg_pkg_postinst
	gnome2_schemas_update

	ewarn "If upgrading from 10.9.3 and currently you are in a budgie-desktop session. Please logout and login again."
	ewarn ""
	ewarn "If upgrading from 10.10.2 and upon relog seeing errors about 'command' please remove `~/.config/budgie-desktop/labwc/rc.xml` and relog again please."
}

pkg_postrm() {
	xdg_pkg_postrm
	gnome2_schemas_update
}