#!/bin/sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DESC="Ethernet authentication daemon"

DAEMON=/usr/libexec/ead

PIDFILE="/run/ead.pid"
SSD_OPTIONS="--oknodo --quiet --exec $DAEMON --pidfile $PIDFILE"

test -f $DAEMON || exit 0

test -f /etc/default/rcS && . /etc/default/rcS

# Avoid starting if no 802.1x configuration to avoid capturing 802.1x packets by ead.
if [ ! -f /var/lib/ead/default.8021x -a "$1" != "stop" ]; then
    echo "Not starting $DESC, no configuration"
    exit 0
fi

set -e

case $1 in
  start)
	echo -n "Starting $DESC: "
	start-stop-daemon --start --background --make-pidfile $SSD_OPTIONS -- --syslog
	echo "${DAEMON##*/}."
  ;;
  stop)
	echo -n "Stopping $DESC: "
	start-stop-daemon --stop $SSD_OPTIONS && rm -f $PIDFILE
	echo "${DAEMON##*/}."
  ;;
  restart|force-reload)
	$0 stop
	sleep 1
	$0 start
  ;;
  status)
	pidof ${DAEMON} >/dev/null
	status=$?
        if [ $status -eq 0 ]; then
                echo "$DESC is running."
        else
                echo "$DESC is not running"
        fi
        exit $status
   ;;
   *)
	N=/etc/init.d/ead
	echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
	exit 1
	;;
esac

exit 0
