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

# Source function library.
. /etc/rc.d/init.d/functions
. /usr/lib/inn/innshellvars

RETVAL=0
LOCKFILE=/var/lock/subsys/innd

start()
{
    AZ=ABCDEFGHIJKLMNOPQRSTUVWXYZ
    az=abcdefghijklmnopqrstuvwxyz
    ##  Pick ${INND} or ${INNDSTART}
    WHAT=${INNDSTART}
    ##  Set to true or false
    : ${DOINNWATCH:=true}
    DOINNWATCH=`echo ${DOINNWATCH} | tr ${AZ} ${az}`
    if [ -z "${DOINNWATCH}" \
	 -o "${DOINNWATCH}" = "on" \
         -o "${DOINNWATCH}" = "true" \
	 -o "${DOINNWATCH}" = "yes" ]; then
        DOINNWATCH=true
    else
        DOINNWATCH=false
    fi

    : ${DOCNFSSTAT:=false}
    DOCNFSSTAT=`echo ${DOCNFSSTAT} | tr ${AZ} ${az}`
    if [ -z "${DOCNFSSTAT}" \
         -o "${DOCNFSSTAT}" = "on" \
         -o "${DOCNFSSTAT}" = "true" \
         -o "${DOCNFSSTAT}" = "yes" ]; then
        DOCNFSSTAT=true
    else
        DOCNFSSTAT=false
    fi

    MAIL="${MAILCMD} -s 'Boot-time Usenet warning on `hostname`' ${NEWSMASTER}"

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

    echo -n "Starting innd: "
    ##  Clean shutdown or already running?
    if [ -f ${SERVERPID} ] ; then
	if [ `pidofproc innd` ] ; then
	    passed "INND is running!"
	    echo
	    RETVAL=1
	    return
        fi
	#echo 'INND:  PID file exists -- unclean shutdown!'
	RFLAG="-r"
	OVDBFLAG="-f"
    fi

    if [ ! -f ${PATHDB}/.news.daily ] ; then
	case `find ${PATHBIN}/innd -mtime +1 -print 2>/dev/null` in
        "")
	    ;;
	*)
	    echo 'No .news.daily file; need to run news.daily?' | eval ${MAIL}
	    ;;
        esac
    else
	case `find ${PATHDB}/.news.daily -mtime +1 -print 2>/dev/null` in
        "")
    	    ;;
	*)
	    echo 'Old .news.daily file; need to run news.daily?' | eval ${MAIL}
	    ;;
	esac
    fi

    ##  Active file recovery.
    if [ ! -s ${ACTIVE} ] ; then
	if [ -s ${NEWACTIVE} ] ; then
	    mv ${NEWACTIVE} ${ACTIVE}
        else
    	    if [ -s ${OLDACTIVE} ] ; then
		cp ${OLDACTIVE} ${ACTIVE}
	    else
		failure "INND: No active file!"
		echo
		RETVAL=1
		return
	    fi
	fi
	RFLAG="-r"

    fi

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

    ##  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} ${SERVERPID}

    ## In case the ovdb database was not properly shutdown, this will
    ## fix it.  Must be done before starting innd.
    if [ "$OVMETHOD" = "ovdb" -a -n "$DB_HOME" ]; then
	su news -s /bin/sh -c ovdb_recover ${OVDBFLAG} || {
	    failure "Can't recover ov database (check news.err for OVDB messages)"
	    echo
	    RETVAL=1
	    return
	}
    fi

    ##  Start the show.
    daemon ${WHAT} ${RFLAG} ${INNFLAGS}
    
    local pid=`pidofproc innd`
    if [ "$pid" ] ; then
	success "innd startup"
	echo
	touch "$LOCKFILE"
    else	
	failure "innd startup"
	echo
	RETVAL=1
	return
    fi

    # Gee, looks like lisp, doesn't it?
    ${DOINNWATCH} && {
	echo -n "Sheduled start innwatch: "
        daemon --user news "( sleep 60;${INNWATCH} ) &"
        echo
        RETVAL=$?
    }

    ${DOCNFSSTAT} && {
	echo -n "Sheduled start cnfsstat: "
        daemon --user news "( sleep 60;${PATHBIN}/cnfsstat -s -l -P ) &"
        echo
        RETVAL=$?
    }

## Stuff to support ovdb.  Must be started after innd has initialized
## overview.
    if [ "$OVMETHOD" = "ovdb" -a -s "$DB_HOME" ]; then
	echo "Scheduled start dbprocs: "
        daemon --user news "( sleep 60 ; ${PATHBIN}/dbprocs start ) &"
	echo
	RETVAL=$?
    fi

#RMFILE=${MOST_LOGS}/expire.rm
#for F in ${RMFILE} ${RMFILE}.*; do
#    if [ -f $F -a -s $F ] ; then
#	echo "Removing articles from pre-downtime expire run (${F})."
#	(
#	    echo 'System shut down during expire.' \
#		'Unlinking articles listed in'
#	    echo ${F}
#	) | eval ${MAIL}
#	${PATHBIN}/expirerm ${F}
#    fi
#done &
}


stop()
{
    #${PATHBIN}/ctlinnd shutdown 'rc.news stop'

    # Stop cnfsstat (if running)
    if [ -f ${PATHRUN}/cnfsstat.pid ]; then
	echo -n "Shutting down cnfsstat: "
	killproc cnfsstat
	RETVAL=$?
	[ $RETVAL -eq 0 ] && rm -f ${PATHRUN}/cnfsstat.pid
	echo
    fi

    # Stop innwatch (if running)
    if [ -f ${PATHRUN}/innwatch.pid ]; then
	echo -n "Shutting down innwatch: "
	killproc innwatch
	RETVAL=$?
	[ $RETVAL -eq 0 ] && rm -f ${PATHRUN}/innwatch.pid
	echo
    fi

    echo -n "Shutting down innd: "
    killproc innd
    RETVAL=$?
    [ $RETVAL -eq 0 ] && rm -f "$LOCKFILE"
    echo

    # Turn off ovdb support procs, and close the DB environment
    if [ "$OVMETHOD" = "ovdb" -a -n "$DB_HOME" ]; then
	echo -n "Shutting down dbprocs: "
	${PATHBIN}/dbprocs stop
	RETVAL=$?
	[ $RETVAL -eq 0 ] && rm -f ${PATHRUN}/dbprocs.pid
	echo
    fi
}

restart()
{
	stop
	start
}

reload()
{
	# cause the service configuration to be reread, either with kill -HUP:
	echo -n "Reloading innd: "
	daemon --user news "${NEWSBIN}/ctlinnd reload 'all' 'init request'"
	RETVAL=$?
	echo
	# or by simple restarting the daemons in a manner similar to restart above.
}

# 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 "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		# Restart the servce if it is already running, for example:
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	status)
		# report the status of the daemons in free-form format,
		# perhaps with the status function, for example:
		status innd
		RETVAL=$?
		;;
	*)
		echo "Usage: ${0##*/} {start|stop|reload|restart|condstop|condrestart|status}"
		RETVAL=1
esac

exit $RETVAL
