#! /bin/sh
# fixscript will replace this line with code to load innshellvars

##  $Revision: 1.14 $
##  News boot script.  Runs as "news" user.  Run from rc.whatever as:
##     su news -c /path/to/rc.news >/dev/console
##
##  Or to stop INN:
##     su news -c '/path/to/rc.news stop'

waitforpid()
{
    i=12
    while [ $i -gt 0 ];
    do
        kill -0 $1 2>/dev/null || break
	sleep 5
	printf "."
	i=`expr $i - 1`
    done
    printf "\n"
}

case X"$1" in
Xstop)
    # Stop cnfsstat (if running)
    if [ -f ${PATHRUN}/cnfsstat.pid ]; then
	kill `cat ${PATHRUN}/cnfsstat.pid`
	rm -f ${PATHRUN}/cnfsstat.pid
    fi

    # Stop innwatch (if running)
    if [ -f $WATCHPID ]; then
	kill `cat $WATCHPID`
	rm -f $WATCHPID
    fi

    printf "Stopping innd: "
    ${PATHBIN}/ctlinnd shutdown 'rc.news stop'

    # wait for innd to exit (up to 60 sec)
    pid=`cat ${SERVERPID} 2>/dev/null`
    if [ "$pid" != "" ]; then
	waitforpid $pid
    else
	printf "\n"
    fi

    # Turn off ovdb support procs, and close the DB environment
    if [ "$OVMETHOD" = "ovdb" -a -f ${PATHRUN}/ovdb_server.pid ]; then
	pid=`cat ${PATHRUN}/ovdb_server.pid 2>/dev/null`
	if [ "$pid" != "" ]; then
	    printf "Stopping ovdb_server: "
	    kill $pid
	    waitforpid $pid
	fi
    fi
    if [ "$OVMETHOD" = "ovdb" -a -f ${PATHRUN}/ovdb_monitor.pid ]; then
	pid=`cat ${PATHRUN}/ovdb_monitor.pid 2>/dev/null`
	if [ "$pid" != "" ]; then
	    printf "Stopping ovdb_monitor: "
	    kill $pid
	    waitforpid $pid
	fi
    fi

    if [ -f ${PATHBIN}/rc.news.local ]; then
        ${PATHBIN}/rc.news.local stop
    fi

    # Delete all of the PID files that we know about to avoid having them
    # stick around after a fresh start.
    rm -f ${PATHRUN}/cnfsstat.pid $WATCHPID $SERVERPID
    rm -f ${PATHRUN}/ovdb_server.pid ${PATHRUN}/ovdb_monitor.pid

    exit 0
;;
esac

##  Pick ${INND} or ${INNDSTART}
WHAT=${INND}

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

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

##  Clean shutdown or already running?
if [ -f ${SERVERPID} ] ; then
    if kill -0 `cat ${SERVERPID}` 2>/dev/null; then
	echo 'INND is running'
	exit 0
    fi
    echo 'INND:  PID file exists -- unclean shutdown!'
    RFLAG="-r"
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
	    echo 'INND:   No active file!'
	    exit 1
	fi
    fi
    RFLAG="-r"
    # You might want to rebuild the DBZ database, too:
    #cd ${PATHDB} \
    #		&& makehistory -r \
    #		&& mv history.n.dir history.dir \
    #		&& mv history.n.index history.index \
    #		&& mv history.n.hash history.hash
fi

##  Overview data was rebuilt, and also active needs to be renumbered
if [ -f ${PATHRUN}/.rebuildoverview ] ; then
    echo 'INND:  overview was rebuilt, and active needs to be renumbered!'
    RFLAG="-r"
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} ${PATHRUN}/.rebuildoverview

## Initialize ovdb.  Must be done before starting innd.
if [ "$OVMETHOD" = "ovdb" ]; then
    echo 'Starting ovdb.'
    ovdb_init || {
	echo "Can't initialize ovdb (check news.err for OVDB messages)"
	exit 1
    }
    sleep 1
fi

##  Start the show.
echo 'Starting innd.'
eval ${WHAT} ${RFLAG} ${INNFLAGS}

# Gee, looks like lisp, doesn't it?
${DOINNWATCH} && {
    echo "Scheduled start of ${INNWATCH}."
    ( sleep 60 ; ${INNWATCH} ) &
}

${DOCNFSSTAT} && {
    echo "Scheduled start of cnfsstat."
    ( sleep 60 ; ${PATHBIN}/cnfsstat -s -l -P ) &
}

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 &

# Run any additional local startup commands.
if [ -f ${PATHBIN}/rc.news.local ] ; then
    ${PATHBIN}/rc.news.local start
fi
