#!/bin/sh
# Start/stop the FreeRADIUS daemon.

### BEGIN INIT INFO
# Provides:          radiusd
# Required-Start:    $network
# Should-Start:      $time mysql ldap postgresql samba krb5-kdc
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Radius Daemon 
# Description:       Extensible, configurable radius daemon
### END INIT INFO

PROG="freeradius"
PROGRAM="/usr/sbin/freeradius"
PIDFILE="/var/run/freeradius/freeradius.pid"
DESCR="FreeRADIUS daemon"

test -f $PROGRAM || exit 0

# /var/run may be a tmpfs
if [ ! -d /var/run/freeradius ]; then
	install -o freerad -g freerad -m 0755 -d /var/run/freeradius
	if [ $? -ne 0 ]; then
		echo "Error creating the /var/run/freeradius directory."
		exit 1
	fi
fi

case "$1" in
	start)
		echo -n "Starting $DESCR: "
		start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $PROGRAM
		if [ $? -eq 0 ]; then
			echo "$PROG."
		else
			echo "(failed!  run '$PROGRAM -x' to find out why.)"
			exit 1
		fi
		;;
	stop)
		echo -n "Stopping $DESCR: "
		start-stop-daemon --stop --retry=TERM/30/KILL/5 --oknodo --quiet --pidfile $PIDFILE
		echo "$PROG."
		;;
	restart)
		echo "Restarting $DESCR..."
		$0 stop
		$0 start
		;;
	reload | force-reload)
		echo -n "Reloading configuration files for $DESCR"
		start-stop-daemon --stop --signal 1 --oknodo --quiet --pidfile $PIDFILE
		sleep 2
		[ -r $PIDFILE ] && kill -0 $(cat $PIDFILE) > /dev/null 2>&1
		if [ $? -eq 0 ]; then
			echo "."
		else
			echo " failed! ($PROG has died)"
			exit 1
		fi
		;;
	*)
		echo "Usage: $0 start|stop|restart|reload|force-reload"
		exit 1
		;;
esac

exit 0
