#!/bin/sh
#
# Copyright (C) 2001-2005 Francesco P. Lovergine <frankie@debian.org>
#

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/radiusd
NAME=radiusd
DESC=yardradius

#test -x $DAEMON || exit 0

log_daemon_msg() {
	echo -n "$1: $2"
}

log_end_msg() {
	if [ $1 -ne 0 ]; then
		echo " failed!"
	else
		echo "."
	fi
}

[ -f /lib/lsb/init-functions ] && source /lib/lsb/init-functions

set -e

case "$1" in
  start)
	if [ -f /etc/yardradius/users ]; then
		if [ ! -f /etc/yardradius/users.db ]; then
			/usr/sbin/builddbm 
		fi
		log_daemon_msg "Starting $DESC" "$NAME"
		start-stop-daemon --start --quiet --oknodo \
			--exec $DAEMON -- -l /var/log/yardradius.log -b -c
		log_end_msg $?
	else
		echo "$DESC not configured"
		exit 1;
	fi
	;;
  stop)
	log_daemon_msg "Stopping $DESC" "$NAME"
	/usr/bin/pkill -f -u root $DAEMON || true;
	log_end_msg $?
	;;
  restart|force-reload)
	if [ -f /etc/yardradius/users ]; then
		log_daemon_msg "Restarting $DESC" "$NAME"
		/usr/bin/pkill -f -u root $DAEMON || true;
		sleep 1
		start-stop-daemon --start --quiet --oknodo \
			--exec $DAEMON -- -l /var/log/yardradius.log -b -c
		log_end_msg $?
	else
		echo "$DESC not configured"
		exit 1;
	fi
	;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0

