#!/bin/sh
#
# NetworkManager:   NetworkManager daemon
#
# chkconfig: - 12 90
# description:  This is a daemon for automatically switching network \
#               connections to the best available connection.
#
# processname: NetworkManager
# pidfile: /var/run/NetworkManager/NetworkManager.pid
#
### BEGIN INIT INFO
# Provides: network_manager
# Required-Start: $local_fs messagebus
# Required-Stop: $local_fs messagebus
# Default-Start:  3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop NetworkManager
# Description: NetworkManager is a tool for easily managing network connections
### END INIT INFO

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions
. shell-config

# Source networking configuration.
# Check that networking is up.
SourceIfNotEmpty /etc/sysconfig/network || exit 1
is_yes "$NETWORKING" || exit 0

# Source config.
SourceIfNotEmpty /etc/sysconfig/NetworkManager

OLD_PIDFILE=/var/run/NetworkManager/NetworkManager.pid
PIDFILE=/var/run/NetworkManager.pid
LOCKFILE=/var/lock/subsys/NetworkManager
DBUS_SEND=/usr/bin/dbus-send
IFDOWN=/sbin/ifdown
RETVAL=0

interface_down()
{
	for i in /etc/net/ifaces/*; do
	    local iface="${i##*/}"
	    case "$iface" in
		default|unknown|lo) continue;;
	    esac
	    local nm_controlled="$(shell_config_get "$i/options" NM_CONTROLLED)"
	    local disabled="$(shell_config_get "$i/options" DISABLED)"
	    if [ -n "$nm_controlled" ] && is_yes "$nm_controlled"; then
		if [ -z "$disabled" ] || is_no "$disabled"; then
		    [ -x "$IFDOWN" ] && action "Taking down $iface:" $IFDOWN "$iface"
		fi
	    fi
	done
}

nm_status()
{
	local pidfile="$PIDFILE"
	[ -s "$OLD_PIDFILE" ] && pidfile="$OLD_PIDFILE"

	status --pidfile "$pidfile" --expect-user root -- NetworkManager
	RETVAL=$?
}

start()
{
	if [ -n "$NM_SYSCTL_CONF" ]; then
	    action "Setting network parameters:" sysctl -e -p "$NM_SYSCTL_CONF"
	fi
	start_daemon --lockfile "$LOCKFILE" --pidfile "$PIDFILE" --expect-user root -- NetworkManager --pid-file=$PIDFILE
	RETVAL=$?
	if [ -n "$NM_CONNECTION_WAIT" ]; then
	    action "Waiting for connection:" nm-online -q -t "$NM_CONNECTION_WAIT"
	fi
	return $RETVAL
}

stop()
{
	local in_restart="${1-}"
	local pidfile="$PIDFILE"
	[ -s "$OLD_PIDFILE" ] && pidfile="$OLD_PIDFILE"

	if [ -z "$in_restart" ] && is_yes "$NM_STOP_ONEXIT"; then
		for i in $(LANG=C nmcli -t -f device,state dev status); do
			local iface="${i%%:*}"
			[ "${i##*:}" = "connected" ] &&
				action "Disconnecting $iface:" nmcli dev disconnect iface "$iface"
		done
	fi
	stop_daemon --pidfile "$pidfile" --lockfile "$LOCKFILE" --expect-user root \
	    --retry "${NM_STOP_TIMEOUT:-4}" -- NetworkManager
	RETVAL=$?

	# nm-system-settings may be present from NM 0.7.x while upgrading, so kill it.
	killall nm-system-settings >/dev/null 2>&1

	killall nm-dispatcher.action >/dev/null 2>&1
	is_yes "$NM_STOP_MODEMMANAGER" && killall modem-manager >/dev/null 2>&1
	return $RETVAL
}

restart()
{
	stop restart
	start
}

nm_sleep()
{
	[ -x "$DBUS_SEND" ] &&
	action "Put NetworkManager to sleep" "$DBUS_SEND" --system --print-reply \
						--dest=org.freedesktop.NetworkManager  \
						/org/freedesktop/NetworkManager        \
						org.freedesktop.NetworkManager.Sleep   \
						boolean:true
}

nm_wake()
{
	[ -x "$DBUS_SEND" ] &&
	action "To wake NetworkManager" "$DBUS_SEND" --system --print-reply \
						--dest=org.freedesktop.NetworkManager  \
						/org/freedesktop/NetworkManager        \
						org.freedesktop.NetworkManager.Sleep   \
						boolean:false
}

# See how we were called.
case "$1" in
	start)
		is_yes "$NM_DOWN_CONTROLLED" && interface_down
		start
		;;
	stop)
		stop
		;;
	status)
		nm_status
		;;
	reload|restart)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condreload|condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	sleep)
		nm_sleep
		;;
	wake)
		nm_wake
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status|sleep|wake}"
		RETVAL=1
esac

exit $RETVAL
