#! /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 log.o
mv log log.o && >log

# report any errors
sleep 500			# hope that log.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
them="`ls | egrep -v '^bad$'`"
if test " $them" != " "
then
	find $them -mtime +1 -print >$tmp	# old non-bad files lying about
	if test -s $tmp
	then
		(
			echo 'old input files:'
			cat $tmp
			echo
		) >>$gripes
	fi
fi
find bad -type f -mtime -2 -print >$tmp		# recent bad batches
if test -s $tmp
then
	(
		echo 'recent bad input batches:'
		cat $tmp
		echo
	) >>$gripes
fi

# 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;/.*;;' $tmp | sort -u
		echo
	) >>$gripes
fi

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