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

### BEGIN INIT INFO
# Provides:          freeradius
# Required-Start:    $remote_fs $network $syslog
# Should-Start:      $time mysql slapd postgresql samba krb5-kdc
# Required-Stop:     $remote_fs $syslog
# 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"

set -e

. /lib/lsb/init-functions

test_freeradius_config() {
  log_action_begin_msg "Checking $DESCR configuration"

  out=`$PROGRAM -Cxl stdout $FREERADIUS_OPTIONS`; ret=$?
  out=`echo "${out}" | tail -n 1 | sed 's/^\s*ERROR:\s*\(.*\)\s*$/\1/'`
  log_action_end_msg $ret "$out"
  return $ret
}

if [ -r /etc/default/$PROG ]; then
  . /etc/default/$PROG
fi

test -f $PROGRAM || exit 0

# /var/run may be a tmpfs
if [ ! -d /var/run/freeradius ]; then
  mkdir -p /var/run/freeradius
  chown freerad:freerad /var/run/freeradius
fi

export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"

ret=0

case "$1" in
        start)
                log_daemon_msg "Starting $DESCR" "$PROG"

                start_daemon -p "$PIDFILE" "$PROGRAM" $FREERADIUS_OPTIONS || ret=$?
                log_end_msg $ret
                ;;

        stop)
                log_daemon_msg "Stopping $DESCR" "$PROG"

                killproc -p "$PIDFILE" || ret=$?
                log_end_msg $ret
                ;;

        restart|force-reload)
                test_freeradius_config || exit $?

                $0 stop
                $0 start
                ;;

        reload)
                test_freeradius_config || exit $?

                if status_of_proc -p "$PIDFILE" "$PROG" "$DESCR"; then
                  log_daemon_msg "Reloading $DESCR" "$PROG"

                  start-stop-daemon --stop --signal HUP --quiet --pidfile $PIDFILE || ret=$?
                  log_end_msg $ret
                fi
                ;;

        configtest|testconfig)
                test_freeradius_config || exit $?
                ;;

        debug)
                $PROGRAM -X $FREERADIUS_OPTIONS
                ;;

        status)
                status_of_proc -p "$PIDFILE" "$PROGRAM" "$PROG" && exit 0 || exit $?
                ;;

        *)
                echo "Usage: $0 start|stop|restart|force-reload|reload|configtest|debug|status"
                exit 1
                ;;
esac

exit 0
