#! /bin/sh
# newsdaily - daily housekeeping chores

# =()<. ${NEWSCONFIG-@<NEWSCONFIG>@}>()=
. ${NEWSCONFIG-/usr/lib/news/bin/config}

PATH=$NEWSCTL/bin:$NEWSBIN/maint:$NEWSBIN:$NEWSPATH ; export PATH
umask $NEWSUMASK

gripes="/tmp/ngrip$$"
tmp="/tmp/ndai$$"
trap "rm -f $gripes $tmp ; exit 0" 0 1 2 15
>$gripes

if test $# -gt 0
then
	gurus="$*"
else
	gurus="$NEWSMASTER"
fi

cd $NEWSCTL

# keep several generations of errlog for problem tracking
rm -f errlog.ooo
mv errlog.oo errlog.ooo
mv errlog.o errlog.oo
mv errlog errlog.o && >errlog

# keep one generation of log -- it's big
rm -f log.o
mv log log.o && >log

# keep several generations of batchlog for statistics
rm -f batchlog.ooo
mv batchlog.oo batchlog.ooo
mv batchlog.o batchlog.oo
mv batchlog batchlog.o && >batchlog

# report any errors
sleep 500			# hope that errlog.o is quiescent after this
if test -s errlog.o
then
	(
		echo "errlog.o"
		echo ---------
		cat errlog.o
		echo ---------
		echo
	) >>$gripes
fi

# look for input anomalies
cd $NEWSARTS/in.coming
find . -type f -mtime +1 -print | sed 's;^\./;;' | egrep -v '^bad/' >$tmp
if test -s $tmp		# old non-bad files lying about
then
	(
		echo 'old input files:'
		ls -ldtr `cat $tmp`
		echo
	) >>$gripes
fi
find bad -type f -name '[0-9]*' -mtime -2 -print >$tmp	# recent bad batches
if test -s $tmp
then
	(
		echo 'recent bad input batches (perhaps worth investigation):'
		ls -ldtr `cat $tmp`
		echo
	) >>$gripes
fi
find bad -type f -name '[0-9]*' -mtime +7 -exec rm -f '{}' ';'

# look for output anomalies
cd $NEWSARTS/out.going
find . -type f -name 'togo*' -size +0 -mtime +1 -print >$tmp
if test -s $tmp
then
	(
		echo 'batching possibly stalled for sites:'
		sed 's;^\./\([^/]*\)/.*;\1;' $tmp | sort -u
		echo
	) >>$gripes
fi
cd $NEWSCTL
no=`egrep 'disk too full' batchlog.o | wc -l`
if test " $no" -gt 0
then
	(
		echo "space shortage(s) limited batching $no times"
		echo
	) >>$gripes
fi

# look for problem newsgroups on input (can miss cross-posted articles)
cd $NEWSCTL
egrep '`' log.o | egrep junked | sed 's/.*`\(.*\)'"'"'.*/\1/' | sort |
	uniq -c | sort -nr | sed 5q >$tmp
if test -s $tmp
then
	(
		echo 'leading five unknown newsgroups by number of articles:'
		cat $tmp
		echo
	) >>$gripes
fi
egrep '`' log.o | egrep unapproved | sed 's/.*`\(.*\)'"'"'.*/\1/' | sort |
	uniq -c | sort -nr | sed 5q >$tmp
if test -s $tmp
then
	(
		echo 'top five supposedly-moderated groups with unmoderated postings:'
		cat $tmp
		echo
	) >>$gripes
fi
egrep '`' log.o | egrep 'no subscribed' | sed 's/.*`\(.*\)'"'"'.*/\1/' | sort |
	uniq -c | sort -nr | sed 5q >$tmp
if test -s $tmp
then
	(
		echo 'leading five unsubscribed newsgroups:'
		cat $tmp
		echo
	) >>$gripes
fi

# and send it
if test -s $gripes
then
	mail $gurus <$gripes
fi
