#! /bin/sh
#
# /etc/init.d/innd
#
# innd		InterNet News System
#
# chkconfig: - 95 05
# description: in2n 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.
# processname: innd
# pidfile: /var/run/news/innd.pid

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

# Source config.
SourceIfNotEmpty /usr/lib/inn/innshellvars

INND_LOCKFILE=/var/lock/subsys/innd
INND_PIDFILE=/var/run/news/innd.pid

OVDB_LOCKFILE=/var/lock/subsys/ovdb_monitor
OVDB_PIDFILE=/var/run/news/ovdb_monitor.pid

RETVAL=0

##  RFLAG is set below; set INNFLAGS in inn.conf(5)
RFLAG=""

do_history_init()
{
    # You might want to rebuild the DBZ database, too:
    if [ ! -f ${PATHDB}/history.dir ]; then
        su news -s /bin/sh -c "/usr/bin/makedbz -i"
        su news -s /bin/sh -c "cd ${PATHDB} \
    	    && mv history.n.dir history.dir \
	    && mv history.n.hash history.hash \
	    && mv history.n.index history.index "
    fi
}

do_active_init()
{
    ##  Active file recovery.
    if [ ! -s ${ACTIVE} ] ; then
	if [ -s ${NEWACTIVE} ] ; then
	    su news -s /bin/sh -c "mv ${NEWACTIVE} ${ACTIVE}"
        else
    	    if [ -s ${OLDACTIVE} ] ; then
		su news -s /bin/sh -c "cp ${OLDACTIVE} ${ACTIVE}"
	    else
		msg_starting innd
		failure "INND: No active file!"
		echo
		RETVAL=1
		return
	    fi
	fi
	RFLAG="-r"
    fi
}

do_ovdb_start()
{
    ## Initialize ovdb.  Must be done before starting innd.
    if [ "$OVMETHOD" = "ovdb" ]; then
	msg_starting $"ovdb"
	start_daemon --no-announce --pidfile "$OVDB_PIDFILE" --lockfile "$OVDB_LOCKFILE" --user news --expect-user news --name ovdb_monitor /usr/bin/ovdb_init || {
	    RETVAL=$?
	    return $RETVAL
	}
    fi
}

do_ovdb_stop()
{
    # Turn off ovdb support procs, and close the DB environment
    if [ "$OVMETHOD" = "ovdb" -a -f ${PATHRUN}/ovdb_server.pid ]; then
	stop_daemon --pidfile "${PATHRUN}/ovdb_server.pid" --expect-user news ovdb_server
    fi

    if [ "$OVMETHOD" = "ovdb" -a -f $OVDB_PIDFILE ]; then
	stop_daemon --pidfile "$OVDB_PIDFILE" --lockfile "$OVDB_LOCKFILE" --expect-user news ovdb_monitor
    fi
    
}

start()
{
    if [ -f $INND_PIDFILE ] ; then
	FLAGS="--start --exec /usr/bin/innd --pidfile $INND_PIDFILE --user news"
	if start-stop-daemon $FLAGS --test > /dev/null; then
	    RFLAG="-r"
	    ##  Remove temporary batchfiles and lock files.
	    ( cd ${BATCH} && rm -f bch* )
	    ( cd ${LOCKS} && rm -f LOCK* )
	    ( cd ${TEMPSOCKDIR} && rm -f ${TEMPSOCK} )
	    rm -f ${NEWSCONTROL} ${NNTPCONNECT} $INND_PIDFILE
	fi
    fi

    do_history_init
    do_active_init
    do_ovdb_start

    msg_starting $"innd"
     start_daemon --no-announce --pidfile "$INND_PIDFILE" --lockfile "$INND_LOCKFILE" --expect-user news --name innd  ${INNDSTART} ${RFLAG} ${INNFLAGS}
    RETVAL=$?
}


stop()
{
    do_ovdb_stop
    
    stop_daemon --pidfile "$INND_PIDFILE" --lockfile "$INND_LOCKFILE" --expect-user news innd
    RETVAL=$?
    return $RETVAL

}

restart()
{
	stop
	start
}

reload()
{
	stop
	start
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload)
		reload
		;;
	restart)
		restart
		;;
	condstop)
		# Stop the servce if it is already running, for example:
		if [ -e "$INND_LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		# Restart the servce if it is already running, for example:
		if [ -e "$INND_LOCKFILE" ]; then
			restart
		fi
		;;
	condreload)
		if [ -e "$INND_LOCKFILE" ]; then
			reload
		fi
		;;
	status)
		status --pidfile "$INND_PIDFILE" --expect-user news innd
		RETVAL=$?
		;;
	*)
		echo "Usage: ${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
		RETVAL=1
esac

exit $RETVAL
