#! /sbin/sh
##  $Revision: 1.18 $
##  News boot script.

##  =()<. @<_PATH_SHELLVARS>@>()=
. /var/spool/news/news.lib/innshellvars

##  Pick ${INND} or ${INNDSTART}
WHAT=${INNDSTART}
##  Set to true or false
DOINNWATCH=true
MAIL="${MAILCMD} -s 'Boot-time Usenet warning on `hostname`' ${NEWSMASTER}"

##  RFLAG is set below; set FLAGS as appropriate.
RFLAG=""
FLAGS="-i0"

case $1 in
'start')
	##  Clean shutdown?
	if [ -f ${SERVERPID} ] ; then
	    echo 'INND:  PID file exists -- unclean shutdown!'
	    RFLAG="-r"
	fi
	
	if [ ! -f ${NEWSBIN}/news.daily ] ; then
	    echo 'No .news.daily file; need to run news.daily?' | eval ${MAIL}
	else
	    case `find ${NEWSLIB} -name .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"
	fi
	
	##  Remove temporary batchfiles and lock files.
	( cd ${BATCH} && rm -f bch* )
	( cd ${LOCKS} && rm -f LOCK* )
	rm -f ${NEWSCONTROL} ${NNTPCONNECT} ${SERVERPID}
	
	##  Start the show.
	echo 'Starting innd\c'
	eval ${WHAT} ${RFLAG} ${FLAGS}
	
	# Gee, looks like lisp, doesn't it?
	${DOINNWATCH} && {
		echo "( ( sleep 60 ; ${INNWATCH} ) & )" | su ${NEWSUSER}
	}
	
	RMFILE=${MOST_LOGS}/expire.rm
	if [ -s ${MOST_LOGS}/expire.rm ] ; then
	    echo "Removing articles from pre-downtime expire run."
	    (
			echo 'System shut down during expire.  Unlinking articles listed in'
			echo ${RMFILE}
	    ) | eval ${MAIL}
		echo "${NEWSBIN}/expirerm ${RMFILE}" | su ${NEWSUSER} &
	fi
	# start nntplink, actual transfer articles.
#	if [ -f /var/spool/news/news.bin/newsboot ]; then
#       		 /var/spool/news/news.bin/newsboot & echo ' newsboot'
#	fi
	if [ -x /usr/lib/newsbin/links ]
	then
		/usr/lib/newsbin/links check > /dev/null 2>&1
		echo ' nntplink\c'
	fi
	# start nnmaster
	if [ -f /usr/local/lib/nn/nnmaster -a -f /usr/lib/news/active ]; then
			 rm -f /usr/local/lib/nn/MPID
   		     /usr/local/lib/nn/nnmaster -E 1 -M 0 -l -C -r & echo ' nnmaster\c'
	fi
	echo "."
	;;
'stop')
	HOME=/usr/lib/news; export HOME
	/usr/lib/newsbin/ctlinnd throttle "Throttle before the shutdown" >> ${LOG}.crit
	${DOINNWATCH} && [ -f ${WATCHPID} ] && {
		kill `cat ${WATCHPID}`
	}
	/usr/lib/newsbin/ctlinnd mode >> ${LOG}.crit
	/opt/bin/nnadmin =mky
	/usr/lib/newsbin/links -s INT
	/usr/lib/newsbin/ctlinnd shutdown "Clean shutdown after the throttle" >> ${LOG}.crit
	;;
esac
