#! /bin/sh
# $Id: radctl.m4,v 1.9 2003/05/29 15:50:28 gray Exp $
# This file is part of GNU Radius.
# Copyright (C) 2000,2003 Sergey Poznyakoff
#
# GNU Radius 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.
#
# GNU Radius 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 GNU Radius; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

PATH=/bin:/usr/bin:/usr/ucb:$PATH; export PATH
PS="/usr/bin/ps"
PIDFILE=/var/run/radiusd.pid
PROGNAME=/usr/sbin/radiusd

usage() {
	cat - <<EOF
usage: $0 {start|stop|restart|reload|which}
EOF
	exit 0
}

start() {
	$PROGNAME ${1} && {
		sleep 5
		if  [ -r $PIDFILE ]  ; then
			echo "RADIUS server started"
		else
			echo "can't start RADIUS server"
		fi
	}
}

stop() {
	 [ $RUNNING -eq 1 ]  && {
		echo "sending TERM to RADIUS server ($PID)"
		kill -TERM $PID && sleep 5
		 [ -r $PIDFILE ]  && {
			echo "radiusd ($PID) is still running. Sending KILL"
			kill -9 $PID && sleep 5
		}
	}
	rm -f $PIDFILE
}

chan_signal() {
	case $1 in
		reload) 
                         [ $RUNNING -eq 0 ]  && {
                                echo $PROCESS
                                exit 1
                        }
			kill -HUP  $PID && echo "Reloading configs";;
		start)
			 [ $RUNNING -eq 1 ]  && {
				echo "$0: start: radiusd (pid $PID) already running"
				exit 1
			}
			rm -f $PIDFILE
			shift
			start $*;;
		stop)   stop;;

		which)  echo $PROCESS;;

		restart)
			stop
			shift
			start $*;;

		*)	usage;;
	esac

	exit 0
}

if  [ -f $PIDFILE ] ; then
	PID=`cat $PIDFILE`
	PROCESS=`$PS -p $PID | sed -n '2p'`
	RUNNING=1
	 [ `echo $PROCESS | wc -w` -ne 0 ]  || {
		PROCESS="radiusd (pid $PID?) not running"
		RUNNING=0
	}
else
	PROCESS="radiusd not running"
	RUNNING=0
fi

if  [ x"$1" = x"--debug" ] ; then
	DEBUG=$1
	shift
fi	    

if  [ x"$1" = x"-s" -o x"$1" = x"--signal" ] ; then
	shift 
fi
chan_signal $*

