# Copyright 1999-2024 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

inherit meson toolchain-funcs xdg-utils

DESCRIPTION="A window switcher, run dialog and dmenu replacement (fork with Wayland support)"
HOMEPAGE="https://github.com/lbonn/rofi"

if [[ ${PV} = *9999 ]]; then
	inherit git-r3
	EGIT_REPO_URI="https://github.com/lbonn/rofi"
	#EGIT_SUBMODULES=()
	EGIT_BRANCH="wayland"
	KEYWORDS=""
else
	MY_PV="${PV}+wayland3"
	SRC_URI="https://github.com/lbonn/rofi/releases/download/${MY_PV}/${PN}-${MY_PV}.tar.xz"
	KEYWORDS="~amd64 ~x86 ~arm ~arm64"
	S=${WORKDIR}/${PN}-${MY_PV}
fi

LICENSE="MIT"
SLOT="0"
IUSE="X +drun examples imdkit man test wayland +windowmode"
RESTRICT="!test? ( test )"

BDEPEND="
	dev-build/meson
	app-alternatives/ninja
	virtual/pkgconfig
	virtual/libc
	man? (
		>=dev-go/go-md2man-2.0.3
		app-text/doxygen[dot]
	)
	wayland? ( >=dev-libs/wayland-protocols-1.17 )
"
RDEPEND="
	dev-libs/glib:2
	x11-libs/gdk-pixbuf:2
	x11-libs/cairo[X?]
	x11-libs/libxkbcommon[X?]
	x11-libs/pango[X?]
	X? (
		x11-libs/startup-notification
		x11-libs/xcb-util
		x11-libs/xcb-util-cursor
		x11-libs/xcb-util-wm
		x11-misc/xkeyboard-config
	)
	imdkit? ( x11-libs/xcb-imdkit )
	wayland? (
		>=dev-libs/wayland-protocols-1.17
		>=dev-libs/wayland-1.17.0
	)
	!x11-misc/rofi
"
DEPEND="
	${RDEPEND}
	X? ( x11-base/xorg-proto )
	test? (
		>=dev-libs/check-0.11
		dev-util/cppcheck
	)
"
RESTRICT="mirror"

src_prepare() {
	use man || ( sed -i -e '/install_man(/{:1;/)/!{N;b1};d}' -e "/subdir('doc')/d" meson.build || die )
	use test || ( sed -i -e '/test(/{:1;/))/!{N;b1};d}' meson.build || die )
	default
}

src_configure() {
	tc-export CC CXX LD
	local emesonargs=(
		$(meson_use drun)
		$(meson_use windowmode window)
		$(meson_feature X xcb)
		$(meson_feature wayland)
		$(meson_feature test check)
		$(meson_use imdkit imdkit)
	)

	meson_src_configure
}

src_install() {
	meson_src_install
	use examples && ( insinto /usr/share/rofi/examples/; doins Examples/*.sh || die "Examples install failed.")
	use man && ( doman doc/${PN}*.1 doc/${PN}*.5 || die "Manpages install failed.")
	cp "${FILESDIR}/rofi.svg" "${D}/usr/share/icons/hicolor/scalable/apps/" || die "Rofi default icon install failed."
}

src_test() {
	meson_src_test
}

pkg_postinst() {
	for v in ${REPLACING_VERSIONS}; do
		if ver_test "${v}" -lt 1.7.0; then
			elog "Rofi 1.7.0 removed the (deprecated) xresources based configuration setup."
			elog "If you are still using old configuration setup, please convert it to new format manually."
			elog "The new format configuration can be generated by 'rofi -dump-config > ~/.config/rofi/config.rasi'."
			elog "For more information, please see https://github.com/davatorium/rofi/releases/tag/1.7.0"
		fi
	done

	xdg_icon_cache_update
}

pkg_postrm() {
	xdg_icon_cache_update
}
