#! /bin/sh
##  $Revision: 1.18 $
##  Daily news maintenance.
##  Optional arguments:
##	expdir=xxx	Directory in which to build new history file
##	flags=xxx	Pass xxx flags to expire
##	noexpire	Do not expire
##	noexplog	Do not log expire output
##	nologs		Do not scan logfiles
##	nomail		Do not capture and mail output
##	nonn		Do not perform NN admin functions
##	norenumber	Do not renumber the active file
##	norm		Do not remove certain old files
##	norotate	Do not rotate logfiles
##	nostat		Do not run innstat
##	delayrm		Delay unlink files, then do it quicker (expire -z)

# =()<NEWSBIN=@<_PATH_NEWSBIN>@>()=
NEWSBIN=/usr/local/news/bin
PATH=${NEWSBIN}:${PATH}:/bin:/usr/bin:/usr/ucb:/etc ; export PATH

# =()<ACTIVE=@<_PATH_ACTIVE>@>()=
ACTIVE=/usr/local/news/active
# =()<HISTORY=@<_PATH_HISTORY>@>()=
HISTORY=/usr/local/news/history
# =()<NEWSMASTER=@<NEWSMASTER>@>()=
NEWSMASTER=usenet
# =()<LIB=@<_PATH_NEWSLIB>@>()=
LIB=/usr/local/news
# =()<SPOOL=@<_PATH_SPOOL>@>()=
SPOOL=/var/spool/news
# =()<INNDDIR=@<_PATH_INNDDIR>@>()=
INNDDIR=/usr/local/news/innd
# =()<TEMPSOCK=`basename @<_PATH_TEMPSOCK>@ | sed -e 's/XXXXXX$/*/'`>()=
TEMPSOCK=`basename /usr/local/news/innd/ctlinndXXXXXX | sed -e 's/XXXXXX$/*/'`
## =()<TMPDIR=@<_PATH_TMP>@>()=
TMPDIR=/tmp
# =()<MAILCMD=@<_PATH_MAILCMD>@>()=
MAILCMD=/usr/ucb/Mail
# =()<LOCK=@<_PATH_LOCKS>@/LOCK.news.daily>()=
LOCK=/usr/local/news/LOCK.news.daily
# =()<MOST_LOGS=@<_PATH_MOST_LOGS>@>()=
MOST_LOGS=/var/log/news
EXPLOG=${MOST_LOGS}/expire.log
INNSTAT=${NEWSBIN}/innstat

##  Mailer command line.
MAIL="${MAILCMD} -s 'Daily Usenet report' ${NEWSMASTER}"

##  If your expire does not pause or throttle innd, enable this next line:
#MESSAGE="Expiration script $$"
##  Renumber all at once, or in steps?  Set to the delay if steps (that
##  may take a long time!).
RENUMBER=

##  Set defaults.
DOEXPIRE=true
DOEXPLOG=true
DOLOGS=true
DOMAIL=true
DONN=true
DORENUMBER=true
DORM=true
DOSTAT=true
EXPDIR=
EXPIREFLAGS="-v1"
REASON=
RMFILE=
SCANARG=

##  Parse JCL.
for I
do
    case "X$I" in
    Xdelayrm)
	RMFILE=${TMPDIR}/explist$$
	;;
    Xexpdir=*)
	EXPDIR=`expr "${I}" : 'expdir=\(.*\)'`
	REASON="Expiring $$ on ${EXPDIR}"
	;;
    Xflags=*)
	EXPIREFLAGS=`expr "${I}" : 'flags=\(.*\)'`
	;;
    Xnoexpire)
	DOEXPIRE=false
	;;
    Xnoexplog)
	DOEXPLOG=false
	;;
    Xnologs)
	DOLOGS=false
	;;
    Xnomail)
	DOMAIL=false
	MAIL=cat
	;;
    Xnonn)
	DONN=false
	SCANARG="${SCANARG} nonn"
	;;
    Xnorenumber)
	DORENUMBER=false
	;;
    Xnorm)
	DORM=false
	;;
    Xnorotate)
	SCANARG="${SCANARG} norotate"
	;;
    Xnostat)
	DOSTAT=false
	;;
    *)
	echo "Unknown flag ${I}" 1>&2
	exit 1
	;;
    esac
done
test -n "${EXPDIR}" \
    && EXPIREFLAGS="${EXPIREFLAGS} '-D${EXPDIR}' '-r${REASON}'"
test -n "${RMFILE}" \
    && EXPIREFLAGS="${EXPIREFLAGS} '-z${RMFILE}'"

if ${DOMAIL} ; then
    ##  Try to get a temporary file.
    TEMP=${TMPDIR}/doex$$
    test -f ${TEMP} && {
	echo "Temporary file ${TEMP} exists" | eval ${MAIL}
	exit 1
    }
    touch ${TEMP}
    chmod 0660 ${TEMP}
    exec 3>&1 >${TEMP} 2>&1
fi

cd ${LIB}

##  Show the status of the news system.
${DOSTAT} && {
    ${INNSTAT}
    echo ''
}

##  Lock out others.
trap 'rm -f ${LOCK} ; exit 1' 1 2 3 15
shlock -p $$ -f ${LOCK} || {
    ( echo "$0: Locked by `cat ${LOCK}`"; ${INNSTAT} ) | eval ${MAIL}
    exit 1
}

##  The heart of the matter:  prologs, expire, epilogs.
if ${DOEXPIRE} ; then

    ## Wait to be fairly certain innwatch is not in the middle of a pass
    ## Since we're locked, innwatch will pause now till we're done
    sleep 30

    ##  See if we're throttled for lack of space.
    SERVERMODE=`ctlinnd mode 2>/dev/null | sed 1q`
    case "${SERVERMODE}" in
    'Server paused'*'[innwatch:'*)
	## If paused, by innwatch, then turn pause into throttle
	## as we're going to stay that way for a while now
	ctlinnd -s throttle "`expr \"${SERVERMODE}\" : 'Server paused \(.*\)'`"
    esac
    case "${SERVERMODE}" in
    *space*" -- throttling")
	echo "${SERVERMODE} -- trying to recover"
	THROTTLED=true
	EXPIREFLAGS="${EXPIREFLAGS} -n"
	MESSAGE=
	;;
    *"[innwatch:"*)
	echo "${SERVERMODE} -- pressing on"
	THROTTLED=false
	MESSAGE=
	;;
    *)
	THROTTLED=false
	;;
    esac

    ##  Throttle server if we need to.
    if [ -n "${MESSAGE}" ] ; then
	ctlinnd -s -t120 throttle "${MESSAGE}" 2>&1 || {
	    ( echo "$0: Cannot throttle news"; ${INNSTAT} ) | eval ${MAIL}
	    exit 1
	}
    fi

    ##  Actual expire the articles (finally!).
    if ${DOEXPLOG}; then
	echo "expire begin `date`: (${EXPIREFLAGS})"		>>${EXPLOG}
	eval expire "${EXPIREFLAGS}" 2>&1 | sed -e 's/^/    /'	>>${EXPLOG}
	echo "expire end `date`"				>>${EXPLOG}
    else
	eval expire "${EXPIREFLAGS}"
    fi
    test -n "${RMFILE}" -a -f "${RMFILE}" \
	&& sort ${RMFILE} | ( cd ${SPOOL}; exec xargs rm ) \
	&& rm -f ${RMFILE}


    ##  If built on another filesystem, move history files.
    if [ -n "${EXPDIR}" ] ; then
	if [ ! -f ${EXPDIR}/history.n -o ! -f ${EXPDIR}/history.n.done ] ; then
	    ( echo "$0: No new history files"; ${INNSTAT} ) | eval ${MAIL}
	    exit 1
	fi
	mv -f ${EXPDIR}/history.n ${HISTORY}
	mv -f ${EXPDIR}/history.n.dir ${HISTORY}.dir
	mv -f ${EXPDIR}/history.n.pag ${HISTORY}.pag
	rm -f ${EXPDIR}/history.n.done
	MESSAGE="${REASON}"
    fi

    ##  Restart the server if we need to.
    if ${THROTTLED} || test -n "${MESSAGE}" ; then
	ctlinnd -s go "${MESSAGE}" 2>&1 || {
	    ( echo "$0: Cannot unthrottle news"; ${INNSTAT} ) | eval ${MAIL}
	    exit 1
	}
    fi
fi

##  Renumber the active file.
if ${DORENUMBER} ; then
    echo ''
    echo 'Renumbering active file.'
    if [ -z "${RENUMBER}" ] ;then
	ctlinnd -s -t`wc -l <${ACTIVE}` renumber '' 2>&1
    else
	while read GROUP hi lo flag ; do
	    ctlinnd -s renumber ${GROUP} 2>&1
	    sleep ${RENUMBER}
	done <${ACTIVE}
    fi
fi

${DOLOGS} && {
    echo ''
    scanlogs ${SCANARG}
}

##  NN.
${DONN} && test -f ${NEWSBIN}/nnadmin && ${NEWSBIN}/nnadmin Z

##  Remove old sockets.
${DORM} && find ${INNDDIR} -name "${TEMPSOCK}" -mtime +2 -exec rm '{}' ';'

##  Mail the report.
if ${DOMAIL} ; then
    # Stop using the temp file, and mail captured output.
    exec 1>&3 2>&1 3>&-
    test -s ${TEMP} && cat ${TEMP} | eval ${MAIL}
    rm -f ${TEMP}
fi

##  All done.
rm -f ${LOCK}
date >${LIB}/.news.daily
rnews -U
exit 0
