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

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

# Get service config - may override defaults
[ -f /etc/sysconfig/tenshi ] && . /etc/sysconfig/tenshi

# 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

# configtest itself
# must return non-zero if check failed
# output is discarded if checkconfig is ran without details
configtest() {
	/usr/sbin/tenshi -C -c /etc/tenshi/tenshi.conf
	return $?
}

# wrapper for configtest
checkconfig() {
	local details=${1:-0}

	if [ $details = 1 ]; then
		# run config test and display report (status action)
		show "Checking %s configuration" "tenshi"; busy
		local out
		out=$(configtest 2>&1)
		RETVAL=$?
		if [ $RETVAL = 0 ]; then
			ok
		else
			fail
		fi
		[ "$out" ] && echo >&2 "$out"
	else
		# run config test and abort with nice message if failed
		# (for actions checking status before action).
		configtest >/dev/null 2>&1
		RETVAL=$?
		if [ $RETVAL != 0 ]; then
			show "Checking %s configuration" "tenshi"; fail
			nls 'Configuration test failed. See details with %s "checkconfig"' $0
			exit $RETVAL
		fi
	fi
}

start() {
	# Check if the service is already running?
	if [ -f /var/lock/subsys/tenshi ]; then
		msg_already_running "tenshi"
		return
	fi

	checkconfig
	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
}

stop() {
	if [ ! -f /var/lock/subsys/tenshi ]; then
		msg_not_running "tenshi"
		return
	fi

	# Stop daemons.
	msg_stopping "tenshi"
	killproc tenshi
	rm -f /var/lock/subsys/tenshi /var/run/tenshi/tenshi.pid
}

reload() {
	if [ ! -f /var/lock/subsys/tenshi ]; then
		msg_not_running "tenshi"
		RETVAL=7
		return
	fi

	checkconfig
	msg_reloading "tenshi"
	killproc --pidfile /var/run/tenshi/tenshi.pid tenshi -HUP
	RETVAL=$?
}

flush() {
	if [ ! -f /var/lock/subsys/tenshi ]; then
		msg_not_running "tenshi"
		RETVAL=7
		return
	fi

	checkconfig
	echo "Flushing all queues"
	killproc --pidfile /var/run/tenshi/tenshi.pid tenshi -USR2
	RETVAL=$?
}

condrestart() {
	if [ ! -f /var/lock/subsys/tenshi ]; then
		msg_not_running "tenshi"
		RETVAL=$1
		return
	fi

	checkconfig
	stop
	start
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	checkconfig
	stop
	start
	;;
  try-restart)
	condrestart 0
	;;
  reload|force-reload)
	reload
	;;
  flush)
	flush
	;;
  checkconfig|configtest)
	checkconfig 1
	;;
  status)
	status tenshi
	RETVAL=$?
	;;
  *)
	msg_usage "$0 {start|stop|restart|try-restart|reload|force-reload|checkconfig|status}"
	exit 3
esac

exit $RETVAL
