#!/bin/sh
#
# chkconfig:	345 29 69
# description:	tenshi

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

# do some sanity check
if grep -q sample /etc/tenshi/tenshi.conf; then
	echo >&2 "Please configure /etc/tenshi/tenshi.conf before starting. Remove word 'sample' when done."
	exit 1
fi

checkconfig() {
	/usr/sbin/tenshi -C -c /etc/tenshi/tenshi.conf
	return $?
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
	# Check if the service is already running?
	if [ ! -f /var/lock/subsys/tenshi ]; then
		msg_starting tenshi
		daemon /usr/sbin/tenshi -c /etc/tenshi/tenshi.conf -P /var/run/tenshi/tenshi.pid
		RETVAL=$?
		[ $RETVAL -eq 0 ] && touch /var/lock/subsys/tenshi
	else
		msg_already_running tenshi
	fi
	;;
  stop)
        if [ -f /var/lock/subsys/tenshi ]; then
		msg_stopping tenshi
		killproc tenshi
		rm -f /var/run/tenshi/tenshi.pid /var/lock/subsys/tenshi >/dev/null 2>&1
	else
		msg_not_running tenshi
	fi
	;;
  restart)
	$0 stop
	$0 start
	exit $?
	;;
  status)
	status tenshi
	exit $?
	;;
  reload|force-reload)
	if [ -f /var/lock/subsys/tenshi ]; then
		if checkconfig; then
			echo "Reloading configuration and flushing all queues"
			kill -HUP `cat /var/run/tenshi/tenshi.pid`
			RETVAL=$?
		else
			echo "tenshi config file has syntax error, not restarting"
			RETVAL=7
		fi
	else
		msg_not_running tenshi
		RETVAL=7
	fi
	;;
  flush)
	if [ -f /var/lock/subsys/tenshi ]; then
		echo "Flushing all queues"
		kill -USR2 `cat /var/run/tenshi/tenshi.pid`
		RETVAL=$?
	else
		msg_not_running tenshi
		RETVAL=7
	fi
	;;
  *)
	msg_usage "$0 {start|stop|restart|reload|force-reload|status|flush}"
	exit 3
esac

exit $RETVAL
