#!/bin/sh
LibDir=/usr/notes/lib
TmpDir=/usr/notes/tmp
TmpFile=$TmpDir/xnews$$
AWK=/usr/local/bin/nawk
Uncompress=/usr/ucb/uncompress
NNTPfeed=kaon.uchicago.edu
#NNTPfeed=128.135.12.103

Mailer=/bin/mail
Rm="/bin/rm -f"

cat > $TmpFile

$AWK '
/^#![ \t]*cunbatch/ {exit 1}

/^IHAVE[ \t]+/ {exit 2}

{exit 3}
' < $TmpFile
status=$?

case $status in
  1)	# A compressed batch
	($LibDir/uncunbatch < $TmpFile | $Uncompress | \
		$LibDir/newsinput $*) >> $LibDir/rnews.log 2>&1
	keep=1
  ;;
  2)	# A stray IHAVE list
	($AWK '$1=="IHAVE" {print $2}' < $TmpFile | \
		$LibDir/nntpgimme -v $NNTPfeed) >> $LibDir/rnews.nntp.log 2>&1
	keep=0
  ;;
  3)	# A regular batch or a single article
	$LibDir/newsinput $* < $TmpFile >> $LibDir/rnews.log 2>&1
	keep=0
  ;;
esac
status=$?

if [ $status -eq 0 -a keep -eq 0 ]
then
	$Rm $TmpFile
elif [ -s $TmpFile ]
then
	$Mailer notes -s "newsinput: failure"<<xx
	A bad news header was received with command line:
	$0 $*
	and was saved in $TmpFile
	exit status was $status
xx
else
	$Rm $TmpFile
	$Mailer notes -s "newsinput: zero-length batch" <<xx
	A zero-sized file was received and discarded.
	This may be caused by a full file system.
xx
fi
exit $status
