#!/sbin/openrc-run
# Copyright (c) 2008 Saleem Abdulrasool <compnerd@compnerd.org>
# Copyright 2013-2026 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

supervisor=supervise-daemon

description="NetworkManager daemon. The service is marked as started only \
when a network connection is established."
command="/usr/sbin/NetworkManager"
command_args_foreground="--no-daemon"
no_new_privs=1

depend() {
	need dbus
	use elogind
	provide net
}

start_pre() {
	checkpath -q -d -m 0755 /run/NetworkManager
}

start() {
	# If we are re-called by a dispatcher event, we want to mark the service
	# as started without starting the daemon again
	yesno "${IN_BACKGROUND}" && return 0

	[ -z "${INACTIVE_TIMEOUT}" ] && INACTIVE_TIMEOUT="1"

	default_start

	local _retval=$?

	if [ "x${_retval}" = 'x0' ] && ! nm-online -t "${INACTIVE_TIMEOUT}"; then
		einfo "Marking NetworkManager as inactive. It will automatically be marked"
		einfo "as started after a network connection has been established."
		mark_service_inactive
	fi
	return "${_retval}"
}

stop() {
	# If we are re-called by a dispatcher event, we want to mark the service
	# as inactive without stopping the daemon
	if yesno "${IN_BACKGROUND}"; then
		mark_service_inactive "${SVCNAME}"
		return 0
	fi

	default_stop
}

# vim: set ft=gentoo-init-d ts=4 :
