#! /bin/sh
#
# xsupplicant	IEEE 802.1x supplicant (client)
# 
#		Written by Eric Evans <eevans@sym-link.com>

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/xsupplicant
NAME=xsupplicant
DESC="802.1x supplicant"
PIDFILE=/var/run/$NAME.pid

test -x $DAEMON || exit 0

# Include xsupplicant defaults if available
if [ -f /etc/default/xsupplicant ] ; then
	. /etc/default/xsupplicant
fi

set -e

stillrunning()
{
    if [ ! -f $PIDFILE ] ; then
	return 1
    fi
    
    pid=`cat $PIDFILE`

    if [ -z "$pid" ] ; then
        rm -f $PIDFILE
	return 1
    fi

    cmd=`cat /proc/$pid/cmdline 2>/dev/null | tr "\000" "\n"| head -n 1`

    if expr "$cmd" : "$DAEMON" >/dev/null 2>&1 ; then
        return 0
    else
        rm -f $PIDFILE
	return 1
    fi
}

case "$1" in
  start)
	echo -n "Starting $DESC: "
	if ! stillrunning ; then
	    start-stop-daemon --start --quiet --pidfile $PIDFILE \
	        --exec $DAEMON -- $DAEMON_OPTS >/dev/null
	fi
	echo "$NAME."
	;;
  stop)
	echo -n "Stopping $DESC: "
	start-stop-daemon -o --stop --quiet --pidfile $PIDFILE --exec $DAEMON
	if stillrunning ; then
	   echo "failed to shutdown" 2>&1
	else
	    echo "$NAME."
	fi
	;;
  restart|force-reload)
	echo -n "Restarting $DESC: "
	start-stop-daemon --stop --quiet --pidfile $PIDFILE --exec $DAEMON
	sleep 1
	if ! stillrunning  ; then
	    start-stop-daemon --start --quiet --pidfile $PIDFILE \
	        --exec $DAEMON -- $DAEMON_OPTS >/dev/null
	    echo "$NAME."
	else
            echo "failed to restart" 2>&1
	fi
	;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
