#!/bin/sh
#
# NetworkManager:   NetworkManager daemon
#
# description:  This is a daemon for automatically switching network 
#               connections to the best available connection.
#
# processnames: NetworkManager, wpa_supplicant
# pidfiles: /var/run/NetworkManager.pid, /var/run/wpa_supplicant.pid
#
# This file has been modified for Slackware 12 compatibility, and
# to include dbus service for wpa_supplicant for NetworkManager 0.7svn (stevek)

NETWORKMANAGER=/usr/sbin/NetworkManager
WPA_SUPPLICANT=/usr/sbin/wpa_supplicant

# Make sure NetworkManager is executable
[ -x $NETWORKMANAGER ] || exit 0

# We need /sbin/ip
[ -x /sbin/ip ] || exit 0

# We need wpa_supplicant for dbus support
[ -x $WPA_SUPPLICANT ] || exit 0

NM_PIDFILE=/var/run/NetworkManager.pid


nm_start()
{
# Check for dbus
if [ "`pgrep dbus-daemon`" = "" ]; then
  echo "D-BUS must be running to start NetworkManager"
  return ;
fi
 
# Check for HAL
if [ "`pgrep hald`" = "" ]; then
  echo "HAL must be running to start NetworkManager"
  return
fi

# Make sure we aren't already running

if ps acx | grep -q NetworkManager ; then
   echo "NetworkManager already running."
else
  echo "Starting NetworkManager daemon: $NETWORKMANAGER --pid-file $NM_PIDFILE"
  # Start up
  $NETWORKMANAGER --pid-file $NM_PIDFILE
fi ;

}

nm_status()
{
  if ps acx | grep -q NetworkManager ; then
     echo "NetworkManager running."
  fi;

}

nm_stop()
{
  echo -en "Stopping NetworkManager: "
  local pidlist=`cat $NM_PIDFILE 2>/dev/null`
  if [ ! -z "$pidlist" ]; then
    kill $pidlist &>/dev/null
    rm -f $NM_PIDFILE &>/dev/null
  fi
  ## Be sure we're dead
  killall -q -15 NetworkManager
  echo "stopped";

  echo -en "Stopping wpa_supplicant dbus services: "
  local pidlist=`cat $WPA_PIDFILE 2>/dev/null`
  if [ ! -z "$pidlist" ]; then
    kill $pidlist &>/dev/null
    rm -f $WPA_PIDFILE &>/dev/null
  fi
  ## Be sure we're dead
  killall -q -15 wpa_supplicant
  killall -q -15 nm-system-settings
  echo "stopped";
}

nm_restart()
{
  nm_stop
  nm_start
}

case "$1" in
'start')
  nm_start
  ;;
'stop')
  nm_stop
  ;;
'restart')
  nm_restart
  ;;
'status')
  nm_status
  ;;
*)
  echo "usage $0 start|stop|status|restart"
esac

