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

EAPI=8

CRATES="
	addr2line@0.24.2
	adler2@2.0.1
	aead@0.5.2
	aes-gcm@0.10.3
	aes@0.8.4
	ahash@0.8.12
	aho-corasick@1.1.3
	android-activity@0.6.0
	android-properties@0.2.2
	android-tzdata@0.1.1
	android_system_properties@0.1.5
	anstream@0.6.20
	anstyle-parse@0.2.7
	anstyle-query@1.1.4
	anstyle-wincon@3.0.10
	anstyle@1.0.11
	anyhow@1.0.99
	arraydeque@0.5.1
	asn1-rs-derive@0.4.0
	asn1-rs-derive@0.5.1
	asn1-rs-impl@0.1.0
	asn1-rs-impl@0.2.0
	asn1-rs@0.5.2
	asn1-rs@0.6.2
	assert_cmd@2.0.17
	assertor@0.0.3
	async-native-tls@0.4.0
	async-trait@0.1.89
	asynchronous-codec@0.6.2
	atoi@2.0.0
	atomic-waker@1.1.2
	autocfg@1.5.0
	backtrace@0.3.75
	base64@0.22.1
	base64ct@1.8.3
	bincode@1.3.3
	bitflags@1.3.2
	bitflags@2.11.1
	block-buffer@0.10.4
	block2@0.5.1
	bstr@1.12.0
	bumpalo@3.19.0
	byteorder@1.5.0
	bytes@1.12.0
	calloop@0.13.0
	cc@1.2.34
	cesu8@1.1.0
	cfg-if@1.0.3
	cfg_aliases@0.2.1
	chrono@0.4.41
	cipher@0.4.4
	clap@4.5.45
	clap_builder@4.5.44
	clap_derive@4.5.45
	clap_lex@0.7.5
	colorchoice@1.0.4
	combine@4.6.7
	concurrent-queue@2.5.0
	connection-string@0.2.0
	const-oid@0.9.6
	core-foundation-sys@0.8.7
	core-foundation@0.10.1
	core-foundation@0.9.4
	core-graphics-types@0.1.3
	core-graphics@0.23.2
	cpufeatures@0.2.17
	crc32fast@1.5.0
	crossbeam-deque@0.8.6
	crossbeam-epoch@0.9.18
	crossbeam-utils@0.8.21
	crypto-common@0.1.7
	ctr@0.9.2
	cursor-icon@1.2.0
	darling@0.13.4
	darling@0.20.11
	darling_core@0.13.4
	darling_core@0.20.11
	darling_macro@0.13.4
	darling_macro@0.20.11
	data-encoding@2.9.0
	der-parser@8.2.0
	der-parser@9.0.0
	der@0.7.10
	deranged@0.4.0
	derive_more-impl@1.0.0
	derive_more@1.0.0
	difflib@0.4.0
	digest@0.10.7
	dirs-sys@0.5.0
	dirs@6.0.0
	dispatch@0.2.0
	displaydoc@0.2.5
	dlib@0.5.2
	doc-comment@0.3.3
	dpi@0.1.2
	dyn-clone@1.0.20
	either@1.15.0
	encoding_rs@0.8.35
	enumflags2@0.7.12
	enumflags2_derive@0.7.12
	equivalent@1.0.2
	errno@0.3.13
	faccess@0.2.4
	fastrand@2.3.0
	flate2@1.1.2
	flexi_logger@0.29.8
	float-cmp@0.10.0
	fnv@1.0.7
	foreign-types-macros@0.2.3
	foreign-types-shared@0.1.1
	foreign-types-shared@0.3.1
	foreign-types@0.3.2
	foreign-types@0.5.0
	form_urlencoded@1.2.2
	futures-channel@0.3.31
	futures-core@0.3.31
	futures-executor@0.3.31
	futures-io@0.3.31
	futures-macro@0.3.31
	futures-sink@0.3.31
	futures-task@0.3.31
	futures-util@0.3.31
	futures@0.3.31
	generic-array@0.14.7
	gethostname@0.5.0
	getrandom@0.1.16
	getrandom@0.2.16
	getrandom@0.3.3
	ghash@0.5.1
	gimli@0.31.1
	h2@0.4.12
	hashbrown@0.12.3
	hashbrown@0.14.5
	hashbrown@0.15.5
	hashlink@0.9.1
	heck@0.5.0
	hermit-abi@0.5.2
	hex@0.4.3
	hmac@0.12.1
	hostname-validator@1.1.1
	http-body-util@0.1.3
	http-body@1.0.1
	http@1.3.1
	httparse@1.10.1
	httpdate@1.0.3
	hyper-rustls@0.27.7
	hyper-tls@0.6.0
	hyper-util@0.1.16
	hyper@1.7.0
	iana-time-zone-haiku@0.1.2
	iana-time-zone@0.1.63
	icu_collections@2.0.0
	icu_locale_core@2.0.0
	icu_normalizer@2.0.0
	icu_normalizer_data@2.0.0
	icu_properties@2.0.1
	icu_properties_data@2.0.1
	icu_provider@2.0.0
	ident_case@1.0.1
	idna@1.1.0
	idna_adapter@1.2.1
	indexmap@1.9.3
	indexmap@2.11.0
	inout@0.1.3
	io-uring@0.7.10
	ipnet@2.11.0
	iri-string@0.7.8
	is_elevated@0.1.2
	is_terminal_polyfill@1.70.1
	itoa@1.0.15
	jni-sys@0.3.0
	jni@0.21.1
	jobserver@0.1.34
	js-sys@0.3.77
	lazy_static@1.5.0
	libc@0.2.175
	libloading@0.8.8
	libm@0.2.16
	libredox@0.1.9
	linux-raw-sys@0.4.15
	linux-raw-sys@0.9.4
	litemap@0.8.0
	lock_api@0.4.13
	log@0.4.27
	lru-slab@0.1.2
	mail_slot@0.1.3
	matchers@0.2.0
	md5@0.6.1
	memchr@2.7.5
	mime@0.3.17
	mime_guess@2.0.5
	minimal-lexical@0.2.1
	miniz_oxide@0.8.9
	mio@1.0.4
	native-tls@0.2.18
	ndk-context@0.1.1
	ndk-sys@0.6.0+11769913
	ndk@0.9.0
	nix@0.29.0
	nom@7.1.3
	normalize-line-endings@0.3.0
	nu-ansi-term@0.50.3
	num-bigint-dig@0.8.6
	num-bigint@0.4.6
	num-conv@0.1.0
	num-integer@0.1.46
	num-iter@0.1.46
	num-traits@0.2.19
	num_enum@0.7.4
	num_enum_derive@0.7.4
	objc-sys@0.3.5
	objc2-app-kit@0.2.2
	objc2-cloud-kit@0.2.2
	objc2-contacts@0.2.2
	objc2-core-data@0.2.2
	objc2-core-image@0.2.2
	objc2-core-location@0.2.2
	objc2-encode@4.1.0
	objc2-foundation@0.2.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.2
	object@0.36.7
	odbc-api@10.2.0
	odbc-sys@0.25.1
	odpic-sys@0.1.1
	oid-registry@0.6.1
	oid-registry@0.7.1
	once_cell@1.21.3
	once_cell_polyfill@1.70.1
	opaque-debug@0.3.1
	openssl-macros@0.1.1
	openssl-probe@0.1.6
	openssl-probe@0.2.1
	openssl-src@300.5.2+3.5.2
	openssl-sys@0.9.116
	openssl@0.10.80
	option-ext@0.2.0
	oracle@0.6.3
	oracle_procmacro@0.1.2
	orbclient@0.3.48
	os_info@3.12.0
	parking_lot@0.12.4
	parking_lot_core@0.9.11
	password-hash@0.5.0
	paste@1.0.15
	pbkdf2@0.12.2
	pem-rfc7468@0.7.0
	percent-encoding@2.3.2
	pin-project-internal@1.1.10
	pin-project-lite@0.2.16
	pin-project@1.1.10
	pin-utils@0.1.0
	pkcs1@0.7.5
	pkcs8@0.10.2
	pkg-config@0.3.32
	plist@1.7.4
	polling@3.10.0
	polyval@0.6.2
	potential_utf@0.1.2
	powerfmt@0.2.0
	ppv-lite86@0.2.21
	predicates-core@1.0.9
	predicates-tree@1.0.12
	predicates@3.1.3
	pretty-hex@0.3.0
	proc-macro-crate@3.3.0
	proc-macro2@1.0.101
	quick-xml@0.38.3
	quinn-proto@0.11.12
	quinn-udp@0.5.13
	quinn@0.11.8
	quote@1.0.40
	r-efi@5.3.0
	rand@0.7.3
	rand@0.8.5
	rand@0.9.2
	rand_chacha@0.2.2
	rand_chacha@0.3.1
	rand_chacha@0.9.0
	rand_core@0.5.1
	rand_core@0.6.4
	rand_core@0.9.3
	rand_hc@0.2.0
	raw-window-handle@0.6.2
	rayon-core@1.13.0
	rayon@1.11.0
	rcgen@0.13.2
	redox_syscall@0.4.1
	redox_syscall@0.5.17
	redox_users@0.5.2
	ref-cast-impl@1.0.24
	ref-cast@1.0.24
	regex-automata@0.4.13
	regex-syntax@0.8.6
	regex@1.12.2
	reqwest@0.12.23
	ring@0.17.14
	rpassword@7.4.0
	rsa@0.9.10
	rtoolbox@0.0.3
	rustc-demangle@0.1.26
	rustc-hash@2.1.1
	rusticata-macros@4.1.0
	rustix@0.38.44
	rustix@1.0.8
	rustls-native-certs@0.8.1
	rustls-pemfile@2.2.0
	rustls-pki-types@1.12.0
	rustls-webpki@0.103.4
	rustls@0.23.31
	rustversion@1.0.22
	ryu@1.0.20
	salsa20@0.10.2
	same-file@1.0.6
	schannel@0.1.29
	schemars@0.9.0
	schemars@1.0.4
	scopeguard@1.2.0
	scrypt@0.11.0
	security-framework-sys@2.17.0
	security-framework@3.7.0
	serde@1.0.219
	serde_derive@1.0.219
	serde_json@1.0.143
	serde_spanned@0.6.9
	serde_urlencoded@0.7.1
	serde_with@3.14.0
	serde_with_macros@3.14.0
	sha2@0.10.8
	sharded-slab@0.1.7
	shellexpand@3.1.2
	shlex@1.3.0
	signal-hook-registry@1.4.6
	signature@2.2.0
	slab@0.4.11
	smallvec@1.15.1
	smol_str@0.2.2
	socket2@0.5.10
	socket2@0.6.0
	spin@0.9.9
	spki@0.7.3
	stable_deref_trait@1.2.0
	strsim@0.10.0
	strsim@0.11.1
	subtle@2.6.1
	syn@1.0.109
	syn@2.0.106
	sync_wrapper@1.0.2
	synstructure@0.12.6
	synstructure@0.13.2
	system-configuration-sys@0.6.0
	system-configuration@0.6.1
	tempfile@3.21.0
	termtree@0.5.1
	thiserror-impl@1.0.69
	thiserror-impl@2.0.16
	thiserror@1.0.69
	thiserror@2.0.16
	thread_local@1.1.8
	tiberius@0.12.3
	time-core@0.1.4
	time-macros@0.2.22
	time@0.3.41
	tinystr@0.8.1
	tinyvec@1.10.0
	tinyvec_macros@0.1.1
	tokio-macros@2.5.0
	tokio-native-tls@0.3.1
	tokio-rustls@0.26.2
	tokio-util@0.7.16
	tokio@1.47.1
	toml@0.8.23
	toml_datetime@0.6.11
	toml_edit@0.22.27
	toml_write@0.1.2
	tower-http@0.6.6
	tower-layer@0.3.3
	tower-service@0.3.3
	tower@0.5.2
	tracing-attributes@0.1.30
	tracing-core@0.1.34
	tracing-log@0.2.0
	tracing-subscriber@0.3.20
	tracing@0.1.41
	try-lock@0.2.5
	typed-builder-macro@0.18.2
	typed-builder@0.18.2
	typenum@1.20.1
	unicase@2.8.1
	unicode-ident@1.0.18
	unicode-segmentation@1.12.0
	unicode-xid@0.2.6
	universal-hash@0.5.1
	untrusted@0.9.0
	url@2.5.7
	utf8_iter@1.0.4
	utf8parse@0.2.2
	uuid@1.18.0
	valuable@0.1.1
	vcpkg@0.2.15
	version_check@0.9.5
	wait-timeout@0.2.1
	walkdir@2.5.0
	want@0.3.1
	wasi@0.11.1+wasi-snapshot-preview1
	wasi@0.14.2+wasi-0.2.4
	wasi@0.9.0+wasi-snapshot-preview1
	wasm-bindgen-backend@0.2.100
	wasm-bindgen-futures@0.4.50
	wasm-bindgen-macro-support@0.2.100
	wasm-bindgen-macro@0.2.100
	wasm-bindgen-shared@0.2.100
	wasm-bindgen@0.2.100
	web-sys@0.3.77
	web-time@1.1.0
	webpki-roots@1.0.9
	widestring@1.2.0
	winapi-i686-pc-windows-gnu@0.4.0
	winapi-util@0.1.10
	winapi-x86_64-pc-windows-gnu@0.4.0
	winapi@0.3.9
	winauth@0.0.4
	windows-core@0.61.2
	windows-implement@0.60.2
	windows-interface@0.59.3
	windows-link@0.1.3
	windows-link@0.2.1
	windows-registry@0.5.3
	windows-result@0.3.4
	windows-strings@0.4.2
	windows-sys@0.45.0
	windows-sys@0.48.0
	windows-sys@0.52.0
	windows-sys@0.59.0
	windows-sys@0.60.2
	windows-sys@0.61.2
	windows-targets@0.42.2
	windows-targets@0.48.5
	windows-targets@0.52.6
	windows-targets@0.53.3
	windows_aarch64_gnullvm@0.42.2
	windows_aarch64_gnullvm@0.48.5
	windows_aarch64_gnullvm@0.52.6
	windows_aarch64_gnullvm@0.53.0
	windows_aarch64_msvc@0.42.2
	windows_aarch64_msvc@0.48.5
	windows_aarch64_msvc@0.52.6
	windows_aarch64_msvc@0.53.0
	windows_i686_gnu@0.42.2
	windows_i686_gnu@0.48.5
	windows_i686_gnu@0.52.6
	windows_i686_gnu@0.53.0
	windows_i686_gnullvm@0.52.6
	windows_i686_gnullvm@0.53.0
	windows_i686_msvc@0.42.2
	windows_i686_msvc@0.48.5
	windows_i686_msvc@0.52.6
	windows_i686_msvc@0.53.0
	windows_x86_64_gnu@0.42.2
	windows_x86_64_gnu@0.48.5
	windows_x86_64_gnu@0.52.6
	windows_x86_64_gnu@0.53.0
	windows_x86_64_gnullvm@0.42.2
	windows_x86_64_gnullvm@0.48.5
	windows_x86_64_gnullvm@0.52.6
	windows_x86_64_gnullvm@0.53.0
	windows_x86_64_msvc@0.42.2
	windows_x86_64_msvc@0.48.5
	windows_x86_64_msvc@0.52.6
	windows_x86_64_msvc@0.53.0
	winit@0.30.12
	winnow@0.7.13
	winreg@0.52.0
	wit-bindgen-rt@0.39.0
	writeable@0.6.1
	x509-parser@0.15.1
	x509-parser@0.16.0
	xkbcommon-dl@0.4.2
	xkeysym@0.2.1
	yaml-rust2@0.9.0
	yasna@0.5.2
	yoke-derive@0.8.0
	yoke@0.8.0
	zerocopy-derive@0.8.26
	zerocopy@0.8.26
	zerofrom-derive@0.1.6
	zerofrom@0.1.6
	zeroize@1.8.1
	zerotrie@0.2.2
	zerovec-derive@0.11.1
	zerovec@0.11.4
"

RUST_OPTIONAL=1
RUST_MIN_VER="1.90.0"

inherit cargo toolchain-funcs

DESCRIPTION="Agent to report data to Check_MK for monitoring"
HOMEPAGE="https://checkmk.com/"

MY_PV="${PV/_p/p}"
MY_P="check-mk-community-${MY_PV}"

GENTOO_PLUGIN_COMMIT="85822ef83114688d6cc172a20285272dc1e2c0d7"
SRC_URI="
	https://download.checkmk.com/checkmk/${MY_PV}/${MY_P}.tar.gz
	portage? (
		https://raw.githubusercontent.com/mape2k/check_mk_extensions/${GENTOO_PLUGIN_COMMIT}/gentoo_portage/local/share/check_mk/agents/plugins/gentoo_portage.py
			-> gentoo_portage.py-${GENTOO_PLUGIN_COMMIT}
	)
	!xinetd? ( ${CARGO_CRATE_URIS} )
"

S="${WORKDIR}/${MY_P}"

LICENSE="GPL-2"
# Dependent crate licenses
LICENSE+="
	Apache-2.0 BSD CDLA-Permissive-2.0 ISC MIT MPL-2.0 Unicode-3.0
"

SLOT="0"
KEYWORDS="~amd64"

IUSE="apache docker iptables mysql +portage postgres smart xinetd"

BDEPEND="
	!xinetd? ( ${RUST_DEPEND} )
"
RDEPEND="
	app-shells/bash:*
	apache? ( www-servers/apache )
	docker? ( app-containers/docker )
	iptables? ( net-firewall/iptables )
	mysql? ( || ( dev-db/mariadb dev-db/mysql ) )
	postgres? ( dev-db/postgresql )
	smart? ( sys-apps/smartmontools )
	xinetd? ( sys-apps/xinetd )
	!xinetd? (
		acct-group/cmk-agent
		acct-user/cmk-agent
		net-misc/socat
	)
"
DEPEND="
	!xinetd? (
		acct-group/cmk-agent
		acct-user/cmk-agent
	)
"

QA_FLAGS_IGNORED="/usr/bin/cmk-agent-ctl"

PATCHES=(
	"${FILESDIR}"/${PN}-nullmailer.patch
)

pkg_setup() {
	use xinetd || rust_pkg_setup
}

src_prepare() {
	# Prepare cmk-agent-ctl on non-xinetd systems
	if ! use xinetd; then
		rm -f packages/cmk-agent-ctl/rust-toolchain.toml || die
		rm -f .cargo/config.toml || die
	fi
	default
}

src_compile() {
	# Compile cmk-agent-ctl on non-xinetd systems
	if ! use xinetd; then
		export CMK_VERSION="${MY_PV}"
		cargo_src_compile -p cmk-agent-ctl
	fi

	$(tc-getCC) ${CFLAGS} ${LDFLAGS} -o agents/waitmax agents/waitmax.c || die
}

src_install() {
	# Install cmk-agent-ctl on non-xinetd systems
	if ! use xinetd; then
		cargo_src_install --path packages/cmk-agent-ctl
	fi

	# Install agent related files
	newbin agents/check_mk_agent.linux check_mk_agent
	newbin agents/check_mk_caching_agent.linux check_mk_caching_agent
	dobin agents/mk-job
	dobin agents/waitmax

	# Configuration
	insinto /etc/check_mk
	doins agents/conffiles/super-server.cfg

	# Create directories for cache, jobs and spool
	keepdir /var/lib/check_mk_agent/{cache,job,rtc_remotes,spool}

	if ! use xinetd; then

		# Create cmk-agent directory
		keepdir /var/lib/cmk-agent
		fowners cmk-agent:cmk-agent /var/lib/cmk-agent
		fperms 0700 /var/lib/cmk-agent

		# Install openrc init script
		newinitd "${FILESDIR}"/cmk-agent-ctl-daemon.initd cmk-agent-ctl-daemon
		newinitd "${FILESDIR}"/check-mk-agent-socket.initd check-mk-agent-socket
		newinitd "${FILESDIR}"/check-mk-agent-async.initd check-mk-agent-async
		newconfd "${FILESDIR}"/check-mk-agent-async.confd check-mk-agent-async
	else
		# Install xinetd service configuration
		insinto /etc/xinetd.d
		newins agents/scripts/super-server/1_xinetd/check-mk-agent check-mk-agent
	fi

	# Create directories for local checks and plugins
	keepdir /usr/lib/check_mk_agent/local

	# Install default plugins
	exeinto /usr/lib/check_mk_agent/plugins
	doexe agents/plugins/mk_filestats.py
	newexe agents/plugins/mk_inventory.linux mk_inventory
	doexe agents/plugins/mk_logins
	doexe agents/plugins/mk_sshd_config

	# Install use-dependent plugins
	use iptables && doexe agents/plugins/mk_iptables
	use mysql && doexe agents/plugins/mk_mysql
	use postgres && doexe agents/plugins/mk_postgres.py
	use smart && doexe agents/plugins/smart

	if use apache; then
		insinto /etc/check_mk
		doins agents/cfg_examples/apache_status.cfg
		exeinto /usr/lib/check_mk_agent/plugins
		doexe agents/plugins/apache_status.py
	fi

	if use docker; then
		insinto /etc/check_mk
		doins agents/cfg_examples/docker*.cfg
		exeinto /usr/lib/check_mk_agent/plugins
		doexe agents/plugins/mk_docker.py
	fi

	if use portage; then
		exeinto /usr/lib/check_mk_agent/plugins/3600
		newexe "${DISTDIR}"/gentoo_portage.py-${GENTOO_PLUGIN_COMMIT} gentoo_portage.py
	fi
}

pkg_postinst() {
	if ! use xinetd; then
		elog "To enable Checkmk agent - Asynchronous background tasks:"
		elog "  rc-update add check-mk-agent-async default"
		elog "  rc-service check-mk-agent-async start"
		elog " "
		elog "To enable the Checkmk agent controller run:"
		elog "  rc-update add check-mk-agent-socket default"
		elog "  rc-service check-mk-agent-socket start"
		elog "  rc-update add cmk-agent-ctl-daemon default"
		elog "  rc-service cmk-agent-ctl-daemon start"
		elog " "
		elog "Register the agent with your Checkmk site first:"
		elog "  cmk-agent-ctl register --hostname HOST --server SERVER --site SITE --user USER"

		# Enable legacy mode by default (like Debian package does)
		# but do not reenable on update
		if [[ ! -e ${EROOT}/var/lib/cmk-agent/allow-legacy-pull ]] &&
		   [[ ! -e ${EROOT}/var/lib/cmk-agent/registered_connections.json ]]; then

			elog " "
			ewarn "cmk-agent-ctl is configured to allow unencrypted legacy agent pull mode."
			ewarn "It will be disabled automatically on first successful agent registration."

			cat > "${EROOT}/var/lib/cmk-agent/allow-legacy-pull" <<-'EOF' || die
				This file has been placed as a marker for cmk-agent-ctl
				to allow unencrypted legacy agent pull mode.
				It will be removed automatically on first successful agent registration.
				You can remove it manually to disallow legacy mode, but note that
				for regular operation you need to register the agent anyway.

				To secure the connection run `cmk-agent-ctl register`.
			EOF
			chown cmk-agent:cmk-agent \
				"${EROOT}"/var/lib/cmk-agent/allow-legacy-pull || die
		fi
	else
		elog "Remember to check /etc/xinetd.d/check-mk-agent and restart xinetd afterwards."
	fi
}