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

EAPI=8

CRATES="
	ab_glyph@0.2.32
	ab_glyph_rasterizer@0.1.10
	adler2@2.0.1
	ahash@0.8.14
	allocator-api2@0.2.22
	android-activity@0.6.1
	android-properties@0.2.2
	android_system_properties@0.1.6
	anstream@1.0.0
	anstyle-parse@1.0.0
	anstyle-query@1.1.5
	anstyle-wincon@3.0.11
	anstyle@1.0.14
	arboard@3.7.0
	arrayref@0.3.9
	arrayvec@0.7.8
	as-raw-xcb-connection@1.0.1
	ash@0.38.0+1.3.281
	atomic-waker@1.1.2
	autocfg@1.5.1
	bit-set@0.8.0
	bit-vec@0.8.0
	bitflags@1.3.2
	bitflags@2.13.2
	block2@0.5.1
	block@0.1.6
	bumpalo@3.20.4
	bytemuck@1.25.2
	bytemuck_derive@1.12.1
	byteorder-lite@0.1.0
	bytes@1.12.2
	calloop-wayland-source@0.3.0
	calloop-wayland-source@0.4.1
	calloop@0.13.0
	calloop@0.14.5
	cassowary@0.3.0
	castaway@0.2.4
	cc@1.11.3
	cfg-if@1.0.5
	cfg_aliases@0.2.2
	cgl@0.3.2
	clap@4.6.10
	clap_builder@4.6.10
	clap_complete@4.6.12
	clap_derive@4.6.9
	clap_lex@1.1.2
	clap_mangen@0.2.33
	clipboard-win@5.4.3
	codespan-reporting@0.11.1
	colorchoice@1.0.5
	combine@4.6.9
	compact_str@0.8.2
	concurrent-queue@2.5.0
	core-foundation-sys@0.8.7
	core-foundation@0.9.4
	core-graphics-types@0.1.3
	core-graphics@0.23.2
	crc32fast@1.5.6
	crossbeam-utils@0.8.23
	crossterm@0.28.1
	crossterm_winapi@0.9.1
	crunchy@0.2.4
	cursor-icon@1.2.0
	darling@0.24.1
	darling_core@0.24.1
	darling_macro@0.24.1
	dispatch2@0.3.1
	dispatch@0.2.0
	displaydoc@0.2.7
	dlib@0.5.3
	document-features@0.2.12
	downcast-rs@1.2.1
	dpi@0.1.2
	ecolor@0.31.1
	eframe@0.31.1
	egui-wgpu@0.31.1
	egui-winit@0.31.1
	egui@0.31.1
	egui_glow@0.31.1
	egui_plot@0.31.0
	either@1.19.0
	emath@0.31.1
	epaint@0.31.1
	epaint_default_fonts@0.31.1
	equivalent@1.0.3
	errno@0.3.15
	error-code@3.4.0
	fastrand@2.5.0
	fax@0.2.7
	fdeflate@0.3.7
	find-msvc-tools@0.1.15
	flate2@1.1.11
	foldhash@0.1.5
	foreign-types-macros@0.2.4
	foreign-types-shared@0.3.1
	foreign-types@0.5.0
	form_urlencoded@1.2.3
	futures-core@0.3.34
	futures-task@0.3.34
	futures-util@0.3.34
	gethostname@1.1.0
	getrandom@0.4.3
	gl_generator@0.14.0
	glow@0.16.0
	glutin-winit@0.5.0
	glutin@0.32.3
	glutin_egl_sys@0.7.1
	glutin_glx_sys@0.6.1
	glutin_wgl_sys@0.6.1
	gpu-alloc-types@0.3.1
	gpu-alloc@0.6.2
	gpu-descriptor-types@0.2.0
	gpu-descriptor@0.3.2
	half@2.7.1
	hashbrown@0.15.5
	hashbrown@0.17.2
	heck@0.5.0
	hermit-abi@0.5.3
	hexf-parse@0.2.1
	icu_collections@2.4.0
	icu_locale_core@2.4.0
	icu_normalizer@2.4.0
	icu_normalizer_data@2.4.0
	icu_properties@2.4.0
	icu_properties_data@2.4.0
	icu_provider@2.4.0
	ident_case@1.0.1
	idna@1.1.0
	idna_adapter@1.2.2
	image@0.25.10
	indexmap@2.14.2
	indoc@2.0.8
	instability@0.3.14
	is_terminal_polyfill@1.70.2
	itertools@0.13.0
	itoa@1.0.19
	jni-macros@0.22.4
	jni-sys-macros@0.4.1
	jni-sys@0.3.1
	jni-sys@0.4.1
	jni@0.22.4
	jobserver@0.1.35
	js-sys@0.3.107
	khronos-egl@6.0.0
	khronos_api@3.1.0
	libc@0.2.191
	libloading@0.8.9
	libredox@0.1.25
	linux-raw-sys@0.12.1
	linux-raw-sys@0.4.15
	litemap@0.8.3
	litrs@1.1.0
	lock_api@0.4.14
	log@0.4.35
	lru@0.12.5
	malloc_buf@0.0.6
	memchr@2.8.3
	memmap2@0.9.12
	memoffset@0.9.1
	metal@0.31.0
	miniz_oxide@0.8.9
	miniz_oxide@0.9.1
	mio@1.2.5
	moxcms@0.8.1
	naga@24.0.0
	ndk-context@0.1.1
	ndk-sys@0.5.0+25.2.9519653
	ndk-sys@0.6.0+11769913
	ndk@0.9.0
	nohash-hasher@0.2.0
	num-traits@0.2.19
	num_enum@0.7.6
	num_enum_derive@0.7.6
	objc-sys@0.3.5
	objc2-app-kit@0.2.2
	objc2-app-kit@0.3.2
	objc2-cloud-kit@0.2.2
	objc2-contacts@0.2.2
	objc2-core-data@0.2.2
	objc2-core-foundation@0.3.2
	objc2-core-graphics@0.3.2
	objc2-core-image@0.2.2
	objc2-core-location@0.2.2
	objc2-encode@4.1.0
	objc2-foundation@0.2.2
	objc2-foundation@0.3.2
	objc2-io-surface@0.3.2
	objc2-link-presentation@0.2.2
	objc2-metal@0.2.2
	objc2-quartz-core@0.2.2
	objc2-symbols@0.2.2
	objc2-ui-kit@0.2.2
	objc2-uniform-type-identifiers@0.2.2
	objc2-user-notifications@0.2.2
	objc2@0.5.3
	objc2@0.6.5
	objc@0.2.7
	once_cell@1.21.4
	once_cell_polyfill@1.70.2
	orbclient@0.3.55
	ordered-float@4.6.0
	owned_ttf_parser@0.25.1
	parking_lot@0.12.5
	parking_lot_core@0.9.12
	paste@1.0.15
	percent-encoding@2.3.2
	pin-project-internal@1.2.0
	pin-project-lite@0.2.18
	pin-project@1.2.0
	pkg-config@0.3.34
	plain@0.2.3
	png@0.18.1
	polling@3.11.0
	potential_utf@0.1.6
	proc-macro-crate@3.5.0
	proc-macro2@1.0.107
	profiling@1.0.18
	pxfm@0.1.30
	quick-error@2.0.1
	quick-xml@0.41.0
	quote@1.0.47
	r-efi@6.0.0
	ratatui@0.29.0
	raw-window-handle@0.6.2
	redox_syscall@0.4.1
	redox_syscall@0.5.18
	redox_syscall@0.9.5
	renderdoc-sys@1.1.0
	roff@1.1.1
	rustc-hash@1.1.0
	rustc-hash@2.1.3
	rustc_version@0.4.1
	rustix@0.38.44
	rustix@1.1.6
	rustversion@1.0.23
	ryu@1.0.23
	same-file@1.0.6
	scoped-tls@1.0.1
	scopeguard@1.2.0
	sctk-adwaita@0.10.1
	semver@1.0.28
	serde@1.0.229
	serde_core@1.0.229
	serde_derive@1.0.229
	serde_json@1.0.154
	serde_spanned@0.6.9
	shlex@2.0.1
	signal-hook-mio@0.2.5
	signal-hook-registry@1.4.8
	signal-hook@0.3.18
	simd-adler32@0.3.10
	simd_cesu8@1.2.0
	simdutf8@0.1.5
	slab@0.4.12
	slotmap@1.1.1
	smallvec@1.16.6
	smithay-client-toolkit@0.19.2
	smithay-client-toolkit@0.20.0
	smithay-clipboard@0.7.3
	smol_str@0.2.2
	spirv@0.3.0+sdk-1.3.268.0
	stable_deref_trait@1.2.1
	static_assertions@1.1.0
	strict-num@0.1.1
	strsim@0.11.1
	strum@0.26.3
	strum_macros@0.26.4
	syn@2.0.119
	syn@3.0.10
	synstructure@0.14.0
	tempfile@3.27.0
	termcolor@1.4.1
	thiserror-impl@1.0.69
	thiserror-impl@2.0.21
	thiserror@1.0.69
	thiserror@2.0.21
	tiff@0.11.3
	tiny-skia-path@0.11.4
	tiny-skia@0.11.4
	tinystr@0.8.5
	tokio@1.54.1
	toml@0.8.23
	toml_datetime@0.6.11
	toml_datetime@1.1.3+spec-1.1.0
	toml_edit@0.22.27
	toml_edit@0.25.21+spec-1.1.0
	toml_parser@1.1.7+spec-1.1.0
	toml_write@0.1.2
	tracing-core@0.1.36
	tracing@0.1.44
	ttf-parser@0.25.1
	type-map@0.5.1
	unicode-ident@1.0.27
	unicode-segmentation@1.13.3
	unicode-truncate@1.1.0
	unicode-width@0.1.14
	unicode-width@0.2.0
	unicode-xid@0.2.6
	url@2.5.9
	utf8_iter@1.1.0
	utf8parse@0.2.2
	utf_types@1.0.0
	version_check@0.9.5
	walkdir@2.5.1
	wasi@0.11.1+wasi-snapshot-preview1
	wasm-bindgen-futures@0.4.80
	wasm-bindgen-macro-support@0.2.130
	wasm-bindgen-macro@0.2.130
	wasm-bindgen-shared@0.2.130
	wasm-bindgen@0.2.130
	wayland-backend@0.3.17
	wayland-client@0.31.15
	wayland-csd-frame@0.3.0
	wayland-cursor@0.31.14
	wayland-protocols-experimental@20250721.0.1
	wayland-protocols-misc@0.3.12
	wayland-protocols-plasma@0.3.12
	wayland-protocols-wlr@0.3.12
	wayland-protocols@0.32.13
	wayland-scanner@0.31.11
	wayland-sys@0.31.11
	web-sys@0.3.107
	web-time@1.1.0
	webbrowser@1.2.7
	weezl@0.1.12
	wgpu-core@24.0.5
	wgpu-hal@24.0.4
	wgpu-types@24.0.0
	wgpu@24.0.5
	winapi-i686-pc-windows-gnu@0.4.0
	winapi-util@0.1.11
	winapi-x86_64-pc-windows-gnu@0.4.0
	winapi@0.3.9
	windows-core@0.58.0
	windows-implement@0.58.0
	windows-interface@0.58.0
	windows-link@0.2.1
	windows-result@0.2.0
	windows-strings@0.1.0
	windows-sys@0.52.0
	windows-sys@0.59.0
	windows-sys@0.61.2
	windows-targets@0.52.6
	windows@0.58.0
	windows_aarch64_gnullvm@0.52.6
	windows_aarch64_msvc@0.52.6
	windows_i686_gnu@0.52.6
	windows_i686_gnullvm@0.52.6
	windows_i686_msvc@0.52.6
	windows_x86_64_gnu@0.52.6
	windows_x86_64_gnullvm@0.52.6
	windows_x86_64_msvc@0.52.6
	winit@0.30.13
	winnow@0.7.15
	winnow@1.0.5
	writeable@0.6.4
	x11-dl@2.21.0
	x11rb-protocol@0.13.2
	x11rb@0.13.2
	xcursor@0.3.11
	xkbcommon-dl@0.4.2
	xkeysym@0.2.1
	xml-rs@0.8.29
	yoke-derive@0.8.4
	yoke@0.8.4
	zerocopy-derive@0.8.66
	zerocopy@0.8.66
	zerofrom-derive@0.1.8
	zerofrom@0.1.8
	zerotrie@0.2.5
	zerovec-derive@0.11.7
	zerovec@0.11.9
	zlib-rs@0.6.9
	zmij@1.0.23
	zune-core@0.5.3
	zune-jpeg@0.5.15
"

inherit cargo bash-completion-r1 desktop systemd tmpfiles xdg

DESCRIPTION="AMD Ryzen SMU control and telemetry CLI, TUI and GUI"
HOMEPAGE="https://github.com/brauliobo/zensmu"
SRC_URI="
	https://github.com/brauliobo/zensmu/archive/refs/tags/v${PV}.tar.gz
		-> ${P}.tar.gz
	${CARGO_CRATE_URIS}
"

LICENSE="MIT"
SLOT="0"
KEYWORDS="~amd64"
IUSE="+gui systemd"

RDEPEND="
	>=app-admin/ryzen_smu-0.1.5-r1
	gui? (
		sys-auth/polkit
		media-libs/libglvnd
		dev-libs/wayland
		x11-libs/libxkbcommon
		x11-libs/libX11
		x11-libs/libxcb
		x11-libs/libXcursor
		x11-libs/libXi
		x11-libs/libXrandr
	)
"

DEPEND="${RDEPEND}"

BDEPEND="
	gui? ( virtual/pkgconfig )
"

QA_FLAGS_IGNORED="usr/bin/zensmu.*"

PATCHES=(
    "${FILESDIR}/${P}-wayland-adwaita-csd.patch"
	"${FILESDIR}/${P}-wayland-app-id.patch"
)

src_configure() {
	local mycargoargs=(
		--package zensmu-cli
	)

	if use gui; then
		mycargoargs+=( --package zensmu-gui )
	fi

	cargo_src_configure "${mycargoargs[@]}"
}

src_install() {
	local release_dir="$(cargo_target_dir)"

	dobin "${release_dir}/zensmu"

	# Allow unprivileged access to read-only SMU telemetry.
	if use gui; then
		cat > "${T}/zensmu.conf" <<-EOF || die
			z /sys/kernel/ryzen_smu_drv/codename         0444 root root -
			z /sys/kernel/ryzen_smu_drv/drv_version      0444 root root -
			z /sys/kernel/ryzen_smu_drv/version          0444 root root -
			z /sys/kernel/ryzen_smu_drv/mp1_if_version   0444 root root -
			z /sys/kernel/ryzen_smu_drv/pm_table         0444 root root -
			z /sys/kernel/ryzen_smu_drv/pm_table_size    0444 root root -
			z /sys/kernel/ryzen_smu_drv/pm_table_version 0444 root root -
		EOF

		dotmpfiles "${T}/zensmu.conf"
	fi

	"${release_dir}/zensmu" completions bash \
		> "${T}/zensmu.bash" || die

	"${release_dir}/zensmu" completions zsh \
		> "${T}/_zensmu" || die

	"${release_dir}/zensmu" completions fish \
		> "${T}/zensmu.fish" || die

	"${release_dir}/zensmu" manpage \
		> "${T}/zensmu.1" || die

	newbashcomp "${T}/zensmu.bash" zensmu

	insinto /usr/share/zsh/site-functions
	doins "${T}/_zensmu"

	insinto /usr/share/fish/vendor_completions.d
	doins "${T}/zensmu.fish"

	doman "${T}/zensmu.1"

	if use gui; then
		dobin "${release_dir}/zensmu-gui"

		domenu packaging/desktop/zensmu-gui.desktop

		insinto /usr/share/icons/hicolor/scalable/apps
		doins packaging/desktop/zensmu.svg

		insinto /usr/share/polkit-1/actions
		doins packaging/polkit/org.zensmu.policy
	fi

	if use systemd; then
		systemd_dounit \
			packaging/systemd/zensmu-apply.service

		systemd_douserunit \
			packaging/systemd/zensmu-record.service \
			packaging/systemd/zensmu-exporter.service

		exeinto /usr/lib/systemd/system-sleep
		doexe packaging/systemd/zensmu-resume
	fi

	keepdir /etc/zensmu

	einstalldocs
}

pkg_postinst() {
	xdg_pkg_postinst

	if use gui; then
		tmpfiles_process zensmu.conf
	fi

	elog "Load the ryzen_smu kernel module before using zensmu:"
	elog "  modprobe ryzen_smu"
	elog
	elog "To check hardware and driver compatibility:"
	elog "  zensmu driver status"
	elog "  sudo zensmu doctor"
	elog
	elog "The kernel module is managed by Portage."
	elog "Do not use the built-in DKMS driver installer."
}