#! /bin/sh
#
# innd		InterNet News System
#
# chkconfig:	345 95 05
# description:	inn is the most popular server for Usenet news. It allows \
#		you to setup local news servers. It can be difficult to \
#		set up properly though, so be sure to read /usr/doc/inn* \
#		before trying.

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

# Source networking configuration.
. /etc/sysconfig/network

# Daemon specific
. /usr/share/news/innshellvars

# Check that networking is up.
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
		msg_network_down "INN"
		exit 1
	fi
else
	exit 0
fi

RETVAL=0
# See how we were called.
case "$1" in
  start)
	if [ ! -f /var/lock/subsys/inn ]; then
		grep -v "#" /etc/news/inn.conf | grep ovmethod | grep ovdb 2>&1 >/dev/null
		RETVAL=$?
		if [ $RETVAL -eq 0 ]; then
			msg_starting "INN ovdb_monitor"
			su news -c ovdb_init 2>&1 > /dev/null
			ok
		fi

		msg_starting "INN"
		daemon su news -c inndstart
		if is_yes "$DOINNWATCH" ; then
			msg_starting "INN Watch"
			busy
			su news -c "umask 002
			/usr/bin/setsid /usr/bin/innwatch &" </dev/null >&0 2>&0
			RETVAL=$?
			[ $RETVAL -eq 0 ] && touch /var/lock/subsys/inn
			ps -C innwatch >/dev/null 2>&1 || usleep 500000
			if [ $RETVAL -eq 0 ] && ps -C innwatch >/dev/null 2>&1; then
				ok
				touch /var/lock/subsys/inn
			else
				RETVAL=1
				fail
			fi
		else
			touch /var/lock/subsys/inn
		fi
	else
		msg_already_running "INN"
	fi
	;;
  stop)
	if [ -f /var/lock/subsys/inn ]; then
		msg_stopping "INN"
		busy
		if ctlinnd shutdown "Stopping INN service"; then
			ok
		else
			fail
		fi

		if [ -f /var/run/news/innwatch.pid ]; then
			msg_stopping "INN Watch"
			busy
			if killall -9 innwatch; then
				ok
			else
				died
			fi
			rm -f /var/run/news/innwatch.pid \
			/var/run/news/LOCK.innwatch >/dev/null 2>&1
		fi

		if [ -f /var/run/news/infeed.pid ]; then
			msg_stopping "INN Feed"
			killproc innfeed -9
			rm -f /var/run/news/innfeed.pid >/dev/null 2>&1
		fi

		if [ -f /var/run/news/actived.pid ]; then
			msg_stopping "INN Actived"
			killproc actived -9
			rm -f /var/run/news/actived.pid >/dev/null 2>&1
		fi

		if [ -f /var/run/news/ovdb_monitor.pid ]; then
			msg_stopping "INN ovdb_monitor"
			killproc ovdb_monitor -15
		fi

		rm -f /var/lock/subsys/inn >/dev/null 2>&1

	else
		msg_not_running INN
	fi
	;;
  status)
	status innd
	exit $?
	;;
  reload|force-reload)
	if [ -f /var/lock/subsys/inn ]; then
		msg_reloading "INN"
		busy
		ctlinnd reload all "Reloading" >/dev/null 2>&1;
		[ $? -ne 0 ] && RETVAL=7
		[ $RETVAL -eq 0 ] && ok || died
	else
		msg_not_running INN >&2
		exit 7
	fi
	;;
  restart)
	$0 stop
	sleep 2
	$0 start
	;;
  *)
	msg_usage "$0 {start|stop|restart|reload|force-reload|status}"
	exit 3
esac

exit $RETVAL
