#!/bin/sh
#
# chkconfig: - 88 10
# description: Start/Stop the RADIUS server daemon
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#    Copyright (C) 2001 The FreeRADIUS Project   http://www.freeradius.org
#

# Source function library.
. /etc/rc.d/init.d/functions

### syslog setting ###
SYSLOG_FLAG=1       # 0=off , 1=on
FACILITY="local5"   # smx shellscript's log use facility 'local5'
PRI_DEBUG="debug"   # 7
PRI_INFO="info"     # 6
PRI_WARN="warning"  # 4
PRI_ERR="err"       # 3
PRI_CRIT="crit"     # 2
#
SYSLOG_TAG="radiusd"
######################

# ----- define ----- #
logger=/usr/bin/logger  # for syslog
# ------------------ #

writeLog(){   # args : priority(for syslog) , log_msg
  PRIORITY="$1"
  LOG_MSG="$2"
  writeSyslog "$PRIORITY" "$LOG_MSG"
}

writeSyslog(){  # args : priority(for syslog) , log_msg
  if [ ${SYSLOG_FLAG} -eq 1 ] ; then
    $logger -p ${FACILITY}.${1} -t "${SYSLOG_TAG}[$$]" "${2}"
  fi
}

RADIUSD=/usr/local/sbin/radiusd
RADWATCH=/usr/local/sbin/radwatch
LOCKF=/var/lock/subsys/radiusd
CONFIG=/usr/local/etc/raddb/radiusd.conf

[ -f $RADIUSD ] || exit 0
[ -f $RADWATCH ] || exit 0
[ -f $CONFIG ] || exit 0

RETVAL=0

case "$1" in
  start)
	echo -n $"Starting RADIUS server: "
	#daemon $RADIUSD -y
        daemon $RADWATCH	
        RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch $LOCKF &&
	    ln -s /var/run/radiusd/radiusd.pid /var/run/radiusd.pid 2>/dev/null
	;;
  stop)
       echo -n $"Stopping RADIUS server: "
        killproc $RADWATCH
#       killproc $RADIUSD
        RETVAL=$?
        echo
        writeLog "${PRI_INFO}" "Stopping RADIUS server."

        [ $RETVAL -eq 0 ] && rm -f $LOCKF

        rm /var/run/radiusd.pid 2>/dev/null
        ;;
  status)
	status radiusd
	RETVAL=$?
        ;;
  reload)
      echo -n $"Reloading RADIUS server: "
        killproc $RADIUSD -HUP
        RETVAL=$?
        echo
        writeLog "${PRI_INFO}" "Reloading RADIUS server."
        ;;
  restart)
	$0 stop
	sleep 3
	$0 start
	RETVAL=$?
	;;
  condrestart)
	if [ -f $LOCKF ]; then
		$0 stop
		sleep 3
		$0 start
		RETVAL=$?
	fi
	;;
  *)
	echo $"Usage: $0 {start|stop|status|restart|reload|condrestart}"
	exit 1
esac

exit $RETVAL
