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

# Autogenerated by pycargoebuild 0.15.0

EAPI=8

CRATES="
	aes@0.9.4
	aho-corasick@1.1.5
	anstream@1.0.0
	anstyle-parse@1.0.0
	anstyle-query@1.1.5
	anstyle-wincon@3.0.11
	anstyle@1.0.14
	anyhow@1.0.104
	ashpd@0.14.0
	async-channel@2.5.0
	async-fs@2.2.0
	async-io@2.6.0
	async-lock@3.5.0
	async-task@4.7.1
	async-trait@0.1.92
	atomic-waker@1.1.2
	autocfg@1.5.1
	base64@0.23.1
	bitflags@2.13.2
	block-buffer@0.12.1
	block-padding@0.4.2
	block@0.1.6
	blocking@1.7.0
	blowfish@0.10.0
	bumpalo@3.20.3
	byteorder@1.5.0
	bytes@1.12.1
	cargo-credential@0.4.11
	cbc@0.2.1
	cc@1.11.0
	cfg-if@1.0.5
	cipher@0.5.2
	clap@4.6.7
	clap_builder@4.6.7
	clap_derive@4.6.7
	clap_lex@1.1.1
	cmov@0.5.4
	colorchoice@1.0.5
	concurrent-queue@2.5.0
	const-oid@0.10.2
	cpubits@0.1.1
	cpufeatures@0.3.1
	crossbeam-utils@0.8.23
	crypto-common@0.2.2
	ctutils@0.4.8
	deranged@0.5.9
	digest@0.11.3
	ecb@0.2.1
	endi@1.1.1
	errno@0.3.14
	event-listener-strategy@0.5.4
	event-listener@5.4.2
	fastrand@2.5.0
	find-msvc-tools@0.1.15
	foreign-types-shared@0.1.1
	foreign-types@0.3.2
	formatx@0.4.2
	futures-channel@0.3.34
	futures-core@0.3.34
	futures-io@0.3.34
	futures-lite@2.6.1
	futures-macro@0.3.34
	futures-task@0.3.34
	futures-util@0.3.34
	getrandom@0.3.4
	getrandom@0.4.3
	gettext-rs@0.8.0
	gettext-sys@0.27.0
	heck@0.5.0
	hermit-abi@0.5.3
	hex@0.4.3
	hkdf@0.13.0
	hmac@0.13.0
	hybrid-array@0.4.15
	inout@0.2.5
	is_terminal_polyfill@1.70.2
	itoa@1.0.18
	js-sys@0.3.107
	lazy_static@1.5.1
	libc@0.2.191
	libm@0.2.16
	linux-raw-sys@0.12.1
	locale_config@0.3.0
	lock_api@0.4.14
	log@0.4.35
	malloc_buf@0.0.6
	matchers@0.2.0
	md-5@0.11.0
	memchr@2.8.3
	memoffset@0.9.1
	mio@1.2.4
	nu-ansi-term@0.50.3
	num-bigint-dig@0.9.1
	num-bigint@0.4.8
	num-complex@0.4.6
	num-conv@0.2.2
	num-integer@0.1.47
	num-iter@0.1.46
	num-rational@0.4.2
	num-traits@0.2.19
	num@0.4.3
	num_threads@0.1.8
	objc-foundation@0.1.1
	objc@0.2.7
	objc_id@0.1.1
	once_cell@1.21.4
	once_cell_polyfill@1.70.2
	openssl-macros@0.1.1
	openssl-sys@0.9.118
	openssl@0.10.82
	ordered-stream@0.2.0
	parking@2.2.1
	parking_lot@0.12.5
	parking_lot_core@0.9.12
	pbkdf2@0.13.0
	pin-project-lite@0.2.17
	piper@0.2.5
	pkg-config@0.3.34
	polling@3.11.0
	portable-atomic@1.15.0
	powerfmt@0.2.1
	ppv-lite86@0.2.21
	proc-macro2@1.0.107
	pyo3-async-runtimes@0.30.0
	pyo3-build-config@0.30.1
	pyo3-ffi@0.30.1
	pyo3-macros-backend@0.30.1
	pyo3-macros@0.30.1
	pyo3@0.30.1
	quote@1.0.47
	r-efi@5.3.0
	r-efi@6.0.0
	rand@0.9.5
	rand_chacha@0.9.0
	rand_core@0.9.5
	redox_syscall@0.5.18
	regex-automata@0.4.18
	regex-syntax@0.8.11
	regex@1.13.1
	rpassword@7.5.4
	rtoolbox@0.0.6
	rustix@1.1.6
	rustversion@1.0.23
	scopeguard@1.2.0
	serde@1.0.229
	serde_bytes@0.11.19
	serde_core@1.0.229
	serde_derive@1.0.229
	serde_json@1.0.154
	serde_repr@0.1.21
	sha1@0.11.0
	sha2@0.11.1
	sharded-slab@0.1.7
	shlex@2.0.1
	signal-hook-registry@1.4.8
	slab@0.4.12
	smallvec@1.16.4
	socket2@0.6.5
	strsim@0.11.1
	subtle@2.6.1
	syn@2.0.119
	syn@3.0.9
	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
	time-core@0.1.9
	time-macros@0.2.32
	time@0.3.55
	tokio-macros@2.7.2
	tokio-stream@0.1.19
	tokio@1.54.1
	tracing-attributes@0.1.31
	tracing-core@0.1.36
	tracing-journald@0.3.2
	tracing-log@0.2.0
	tracing-subscriber@0.3.23
	tracing@0.1.44
	typenum@1.20.1
	uds_windows@1.2.1
	unicode-ident@1.0.27
	utf8parse@0.2.2
	uuid@1.29.0
	valuable@0.1.1
	vcpkg@0.2.15
	wasi@0.11.1+wasi-snapshot-preview1
	wasip2@1.0.4+wasi-0.2.12
	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-link@0.2.1
	windows-sys@0.61.2
	winnow@1.0.5
	wit-bindgen@0.57.1
	zbus@6.2.1
	zbus_macros@6.2.1
	zbus_utils@6.1.0
	zcheapstr@1.2.0
	zerocopy-derive@0.8.63
	zerocopy@0.8.63
	zeroize@1.9.1
	zeroize_derive@1.5.0
	zgvariant@2.0.0
	zgvariant_derive@2.0.0
	zmij@1.0.23
	zruntime@0.2.2
"

RUST_MIN_VER="1.95"

inherit cargo meson pam systemd

DESCRIPTION="Secret Service provider, CLI and portal backend written in Rust"
HOMEPAGE="https://github.com/linux-credentials/oo7"
SRC_URI="
	https://github.com/linux-credentials/oo7/archive/refs/tags/${PV}.tar.gz
		-> ${P}.tar.gz
	${CARGO_CRATE_URIS}
"

LICENSE="MIT"
# Dependent crate licenses
LICENSE+="
	Apache-2.0 Apache-2.0-with-LLVM-exceptions BSD MIT Unicode-3.0
"
SLOT="0"
KEYWORDS="~amd64"
IUSE="pam +portal +server systemd"
REQUIRED_USE="pam? ( server )"
# The suites need a live D-Bus session bus and a PAM stack; neither exists
# in the Portage sandbox.
RESTRICT="test"

DEPEND="pam? ( sys-libs/pam )"
RDEPEND="
	${DEPEND}
	sys-apps/dbus
	portal? ( sys-apps/xdg-desktop-portal )
"
BDEPEND="
	>=dev-build/meson-1.7
	server? ( sys-devel/gettext )
"

QA_FLAGS_IGNORED="
	usr/bin/.*
	usr/libexec/oo7-.*
	.*/security/pam_oo7.so
"

# Each meson project runs "cargo build" from its build directory, so cargo
# resolves the workspace from there: the build directories live inside ${S},
# and each one's "src" output directory is the shared cargo target, so the
# three projects reuse the artifacts instead of compiling the tree three times.
oo7_meson_projects() {
	usev server
	usev portal
	usev pam
}

src_prepare() {
	default

	# Build only the crate a meson project installs. Unrestricted, "cargo
	# build" from the workspace builds every member, the pyo3 bindings
	# included. A silent no-op here would bring that back, hence the guard.
	local dir crate
	for dir in server:oo7-daemon portal:oo7-portal pam:oo7-pam; do
		crate=${dir#*:}
		dir=${dir%%:*}
		grep -qF "cargo_options = []" "${dir}/src/meson.build" ||
			die "${dir}/src/meson.build: cargo_options anchor not found"
		sed -i "s/^cargo_options = \[\]$/cargo_options = [ '--package', '${crate}' ]/" \
			"${dir}/src/meson.build" || die
		grep -qF "'--package', '${crate}'" "${dir}/src/meson.build" ||
			die "${dir}/src/meson.build: --package was not applied"
	done
}

src_configure() {
	# gettext-rs links the libc's gettext instead of building its bundled copy.
	export GETTEXT_SYSTEM=1

	local proj emesonargs
	for proj in $(oo7_meson_projects); do
		emesonargs=()
		case ${proj} in
			server|portal)
				emesonargs+=( $(meson_feature systemd) )
				use systemd && emesonargs+=(
					-Dsystemduserunitdir="$(systemd_get_userunitdir)"
				)
				;;
			pam)
				emesonargs+=( -Dpam_moduledir="$(getpam_mod_dir)" )
				;;
		esac
		EMESON_SOURCE="${S}/${proj}" BUILD_DIR="${S}/build-${proj}" \
			meson_src_configure
		rmdir "${S}/build-${proj}/src" || die
		ln -s "${S}/target" "${S}/build-${proj}/src" || die
	done
}

src_compile() {
	cargo_src_compile \
		--package oo7-cli \
		--package cargo-credential-oo7 \
		--package git-credential-oo7

	local proj
	for proj in $(oo7_meson_projects); do
		BUILD_DIR="${S}/build-${proj}" cargo_env meson_src_compile
	done
}

src_install() {
	local proj
	for proj in $(oo7_meson_projects); do
		BUILD_DIR="${S}/build-${proj}" meson_src_install
	done

	dobin "$(cargo_target_dir)"/{oo7-cli,cargo-credential-oo7,git-credential-oo7}

	# The systemd user units above are gated behind USE=systemd; the OpenRC
	# user scripts never are (overlay policy: no systemd-only daemons).
	exeinto /etc/user/init.d
	use server && newexe "${FILESDIR}"/oo7-daemon.initd oo7-daemon
	use portal && newexe "${FILESDIR}"/oo7-portal.initd oo7-portal

	dodoc README.md
}

pkg_postinst() {
	if use server; then
		elog "oo7-daemon provides org.freedesktop.secrets. Run only one Secret"
		elog "Service provider per session: gnome-keyring-daemon and KDE's"
		elog "ksecretd claim the same D-Bus name, and whichever starts first"
		elog "wins."
		elog
		elog "OpenRC: rc-update -U add oo7-daemon"
		use systemd && elog "systemd: systemctl --user enable --now oo7-daemon.service"
	fi
	if use pam; then
		elog
		elog "To unlock the keyring at login, add pam_oo7.so to the display"
		elog "manager's PAM stack, e.g. in /etc/pam.d/<dm>:"
		elog "  auth     optional  pam_oo7.so"
		elog "  session  optional  pam_oo7.so"
		elog "The module stays inactive until it is listed there."
	fi
}