#!/bin/sh
########################################################################
# Begin wicd
#
# Description : Wicd bootscript
#
# Authors     : Ragnar Thomsen - rthomsen@linuxfromscratch.org
#
# Version     : LFS 7.1
#
########################################################################

### BEGIN INIT INFO
# Provides:            
# Required-Start:      
# Should-Start:
# Required-Stop:       
# Should-Stop:
# Default-Start:       2 3 4 5
# Default-Stop:        0 1 6
# Short-Description:   Starts the wicd daemon.
# Description:         Starts the wicd daemon
# X-LFS-Provided-By:   LFS
### END INIT INFO

. /lib/lsb/init-functions
PIDFILE="/var/run/wicd/wicd.pid"

case "${1}" in
   start)
      if [ -e $PIDFILE ]; then
        echo "Wicd appears to already be running."
        echo "If this is not the case, then remove"
        echo "$PIDFILE and try again..."
      else
        log_info_msg "Starting wicd daemon..."
        start_daemon /usr/sbin/wicd 1>/dev/null
        evaluate_retval
      fi
      ;;

   stop)
      log_info_msg "Stopping wicd daemon..."
      if [ -e $PIDFILE ]; then
	# Shut down wpa_supplicant and any started dhcp 
	# clients before we kill Wicd
	wicd-cli -xyz 1>/dev/null
        kill $(cat $PIDFILE)
        evaluate_retval
      else
        echo "Wicd appears not to be running..."
      fi
      ;;

   restart)
      ${0} stop
      sleep 1
      ${0} start
      ;;

   status)
      if [ -e $PIDFILE ]; then
        echo "Wicd is running with pid $(cat $PIDFILE)"
      else
        echo "Wicd appears not to be running..."
      fi
      ;;

   *)
      echo "Usage: ${0} {start|stop|restart|status}"
      exit 1
      ;;
esac

exit 0

# End wicd