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

EAPI=8

VALA_MIN_API_VERSION="0.48"
PYTHON_COMPAT=( python3_{11..14} )

inherit meson vala gnome2-utils xdg python-any-r1

DESCRIPTION="Additional enhancements for the user experience. Contains many applets. Made for Budgie Desktop."
HOMEPAGE="https://github.com/UbuntuBudgie/${PN}"
SRC_URI="https://github.com/UbuntuBudgie/${PN}/releases/download/v${PV}/${P}.tar.xz"
LICENSE="GPL-3"
SLOT="0"
KEYWORDS="~amd64 ~x86 ~arm ~arm64"

# NO WAYLAND SUPPORT YET (or not coming):
#
# - budgie_applets_window-previews
# - budgie_applets_hotcorners
# - budgie_applets_workspace-stopwatch
# - budgie_applets_quickchar
# - budgie_applets_window-shuffler
# - budgie_applets_keyboard-autoswitch
# - budgie_applets_rotation-lock
#
# Will be added back in in future versions when support has been added.

IUSE_BUDGIE_APPLETS="
	+budgie_applets_recommended
	budgie_applets_workspace-switcher
	budgie_applets_quicknote
	budgie_applets_showtime
	budgie_applets_countdown
	budgie_applets_clockworks
	budgie_applets_dropby
	budgie_applets_kangaroo
	budgie_applets_show-weather
	budgie_applets_app-launcher
	budgie_applets_recently-used
	budgie_applets_take-a-break
	budgie_applets_fuzzyclock
	budgie_applets_brightness-controller
	budgie_applets_visualspace
	budgie_applets_wallstreet
	budgie_applets_applications-menu
	budgie_applets_network-manager
	budgie_applets_extras-daemon
	budgie_applets_screencast
"
IUSE="stateless ${IUSE_BUDGIE_APPLETS} +wayland"

# The idea is simple, either 'recommended' is on, or any of the others, none is not allowed.
# By default the 'recommended' is on, so most users are not affected by this.
# Kept it in list for to keep it readable.
#
# Also special case for networkmanager applets as they are optional by 
# either default options or systemwide useflags.
REQUIRED_USE="
	|| ( 
		budgie_applets_recommended	
		budgie_applets_quicknote
		budgie_applets_showtime
		budgie_applets_countdown
		budgie_applets_clockworks
		budgie_applets_dropby
		budgie_applets_kangaroo
		budgie_applets_show-weather
		budgie_applets_app-launcher
		budgie_applets_recently-used
		budgie_applets_take-a-break
		budgie_applets_fuzzyclock
		budgie_applets_brightness-controller
		budgie_applets_visualspace
		budgie_applets_wallstreet
		budgie_applets_applications-menu
		budgie_applets_workspace-switcher
		budgie_applets_extras-daemon
		budgie_applets_screencast
	)"
#	budgie_applets_window-previews? ( budgie_applets_extras-daemon )
#	budgie_applets_quickchar? ( budgie_applets_extras-daemon )
#	budgie_applets_window-shuffler? ( budgie_applets_extras-daemon )
#"

COMMON_DEPEND="
	>=budgie-base/budgie-desktop-10.10.0
	dev-libs/appstream[vala]
	dev-libs/granite:6
	gui-libs/libhandy:1[vala]
	>=dev-libs/json-glib-1.0
	dev-libs/keybinder
	net-libs/libsoup:3.0
"

BDEPEND="${PYTHON_DEPS}"

DEPEND="
	${COMMON_DEPEND}
	budgie_applets_network-manager? (
		>=net-libs/libnma-1.8.0
		>=net-misc/networkmanager-1.24.0:=[modemmanager]
		>=net-misc/modemmanager-0.7 )
"

RDEPEND1="
	!budgie_applets_recommended? (
		budgie_applets_clockworks? (
			media-gfx/cairosvg
			dev-python/svgwrite
			dev-python/pillow
		)

		budgie_applets_countdown? (
			dev-python/psutil
			x11-themes/sound-theme-freedesktop
			media-sound/vorbis-tools
		)

		budgie_applets_dropby? (
			dev-python/psutil
			dev-python/pyudev
			x11-misc/wmctrl
		)

		budgie_applets_kangaroo? (
			x11-misc/xdg-utils
			dev-python/psutil
		)

		budgie_applets_screencast? (
			gui-apps/wf-recorder
		)"

#		budgie_applets_keyboard-autoswitch? (
#			dev-python/psutil
#			x11-misc/wmctrl
#		)
#
#		budgie_applets_window-previews? (
#			x11-apps/xinput
#			x11-misc/xprintidle
#		)
#
#		budgie_applets_quickchar? (
#			x11-misc/wmctrl
#			dev-python/pyperclip
#			dev-python/python-xlib
#		)
#
#		budgie_applets_hotcorners? ( x11-misc/xdotool )
RDEPEND2="		budgie_applets_recently-used? ( x11-misc/xdg-utils )
		budgie_applets_take-a-break? ( x11-misc/xprintidle )
		budgie_applets_show-weather? ( sys-process/procps )
	)

	budgie_applets_recommended? (
		dev-python/psutil
		dev-python/pyudev
		dev-python/pyperclip
		dev-python/python-xlib
		dev-python/svgwrite
		dev-python/pillow
		media-gfx/cairosvg
		media-sound/vorbis-tools
		sys-process/procps
		x11-apps/xinput
		x11-misc/wmctrl
		x11-misc/xdg-utils
		x11-misc/xdotool
		x11-misc/xprintidle
		x11-themes/sound-theme-freedesktop
		gui-apps/wf-recorder
	)
"

RDEPEND="${RDEPEND1} ${RDEPEND2}"
BDEPEND="$(vala_depend)"

PATCHES=(
	"${FILESDIR}"/exclude-network-applet-build-all.patch
)

python_check_deps() {
	python_has_version "dev-python/pyperclip[${PYTHON_USEDEP}]"
	python_has_version "dev-python/python-xlib[${PYTHON_USEDEP}]"
	python_has_version "dev-python/psutil[${PYTHON_USEDEP}]"
	python_has_version "dev-python/pyudev[${PYTHON_USEDEP}]"
	python_has_version "dev-python/svgwrite[${PYTHON_USEDEP}]"
	python_has_version "dev-python/pillow[${PYTHON_USEDEP}]"
}

pkg_setup() {
	python-any-r1_pkg_setup
}

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

src_prepare() {
	vala_setup
	default
}

src_configure() {
	local emesonargs=(
		-Dwith-zeitgeist=false
		$(meson_use wayland for-wayland)
		$(meson_use stateless with-stateless)
		$(meson_use budgie_applets_recommended with-default-schema)
		$(meson_use budgie_applets_recommended build-recommended)
#		$(meson_use budgie_applets_window-previews build-wpreviews)
		$(meson_use budgie_applets_workspace-switcher build-wswitcher)
#		$(meson_use budgie_applets_hotcorners build-hotcorners)
		$(meson_use budgie_applets_quicknote build-quicknote)
		$(meson_use budgie_applets_showtime build-showtime)
		$(meson_use budgie_applets_countdown build-countdown)
#		$(meson_use budgie_applets_keyboard-autoswitch build-keyboard-autoswitch)
#		$(meson_use budgie_applets_rotation-lock build-rotation-lock)
		$(meson_use budgie_applets_clockworks build-clockworks)
		$(meson_use budgie_applets_dropby build-dropby)
		$(meson_use budgie_applets_kangaroo build-kangaroo)
		$(meson_use budgie_applets_show-weather build-weathershow)
		$(meson_use budgie_applets_app-launcher build-app-launcher)
		$(meson_use budgie_applets_recently-used build-recentlyused)
		$(meson_use budgie_applets_take-a-break build-takeabreak)
#		$(meson_use budgie_applets_workspace-stopwatch build-workspacestopwatch)
		$(meson_use budgie_applets_extras-daemon build-extrasdaemon)
#		$(meson_use budgie_applets_quickchar build-quickchar)
		$(meson_use budgie_applets_fuzzyclock build-fuzzyclock)
		$(meson_use budgie_applets_brightness-controller build-brightness-controller)
		$(meson_use budgie_applets_visualspace build-visualspace)
		$(meson_use budgie_applets_wallstreet build-wallstreet)
		$(meson_use budgie_applets_applications-menu build-applications-menu)
		$(meson_use budgie_applets_network-manager build-network-manager)
#		$(meson_use budgie_applets_window-shuffler build-window-shuffler)
		$(meson_use budgie_applets_screencast build-screencast)
	)
	meson_src_configure
}

src_compile() {
	meson_src_compile
}

pkg_postinst() {
	xdg_pkg_postinst
	gnome2_schemas_update

	if use budgie_applets_recommended ; then
		ewarn "The useflag 'recommended' of 'BUDGIE_APPLETS' has been turned on."
		ewarn "This will install every applet except network-manager."
		ewarn "If these are still wanted please add them to 'BUDGIE_APPLETS' and remerge this package."
		ewarn ""
		ewarn "For more information on how to configure this please check the following link:"
		ewarn "<<insert repository wiki link>>"
	fi

	elog "In order for all the applets to appear after installation without relogging it is recommended to run the following as your current logged in user:"
	elog "  nohup budgie-panel --replace > /dev/null 2>&1 &"
}

pkg_postrm() {
	xdg_pkg_postrm
	gnome2_schemas_update

	elog "In order for all the applets to be removed from the budgie-settings applets without relogging it is recommended to run the following as your current logged in user in budgie:"
	elog "  nohup budgie-panel --replace > /dev/null 2>&1 &"
}