#! /bin/sh
# Process spooled news.

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

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

hold=
case "$1" in
-h)	hold=y	;;
esac

here="$NEWSARTS/in.coming"
cd $here

# First, is it worth trying at all?
if test -r stop
then
	exit 0
fi

# Lock against others running.
lock="$NEWSCTL/LOCKinput"
ltemp="$NEWSCTL/L.$$"
echo $$ >$ltemp
trap "rm -f $ltemp ; exit 0" 0 1 2 15
if newslock $ltemp $lock
then
	trap "rm -f $ltemp $lock ; exit 0" 0 1 2 15
else
	exit 0
fi

# Sort out where we are.
if test -r $NEWSCTL/server
then
	me="`hostname`"
	server=`cat $NEWSCTL/server`
else
	me=thishost		# don't need actual name
	server="$me"		# no server file --> we're it
fi

# Master loop.
while :				# "while true", but : is faster
do
	# Find some work.
	them=`ls | sed '/[^0-9]/d;50q'`
	if test " $them" = " "
	then
		break			# NOTE BREAK OUT
	fi

	# Check space.  It is *probably* better to stop processing
	# when things get too full.  (This test is actually a bit
	# inaccurate since the batches may be compressed, but it's
	# good enough to catch major space problems.)
	allsize=`sizeof $them`
	if test " `spacefor $allsize articles`" -gt 1	# lots of room
	then
		muchroom=y
	else
		muchroom=
	fi

	# Do it.
	tmp=nruntmp.$$
	rmlist=
	for f in $them
	do
		# Check for request to stop.
		if test -r stop
		then
			rm -f $tmp $rmlist
			exit 0
		fi

		# Space check, if we're close.
		if test " $muchroom" != " y"
		then
			rm -f $tmp $rmlist	# free up what we can
			rmlist=
			batchsize=`sizeof $f`
			if test " `spacefor $batchsize articles`" -le 0
			then
				# already removed $tmp $rmlist
				exit 0
			fi
		fi

		# Save a copy in hold if requested.
		if test " $hold" = " y" -a -d hold
		then
			ln $f hold/$f
		fi

		# Decompress if necessary.  People who get lots of
		# uncompressed batches and never use c7 encoding might
		# want to remove the c7decode attempt to speed things up.
		text=$tmp
		if compress -d <$f >$text 2>/dev/null
		then
			: okay
		elif c7decode <$f 2>/dev/null | compress -d >$text 2>/dev/null
		then
			: okay
		else
			>$text		# compress might have left garbage
			text=$f
		fi
		rmlist="$rmlist $f"

		# Check for empty.
		if test ! -s $text
		then
			continue			# NOTE CONTINUE
		fi

		# Do it.  -r redirects stdout and stderr into logs.  -n makes
		# history entries for refused articles; this is right for
		# NNTP-feed sites and doesn't hurt uucp-feed sites unless
		# they refuse a good fraction of what they get.
		if test " $server" = " $me"	# if local
		then
			relaynews -r -n <$text
		else
			# N.B.: rsh always returns exit status 0!
			rsh $server /bin/sh -c "PATH=$PATH relaynews -r -n" <$text
		fi
		st=$?
		if test $st -ne 0
		then
			# trouble
			if test ! -d bad
			then
				mkdir bad
			fi
			bad=bad/$f

			if test -s bad/limit
			then
				limit=`sed 1q bad/limit`
			else
				limit=50
			fi
			nfiles=`ls bad | wc | awk '{print $1}'`
			if test " $nfiles" -lt " $limit"
			then
				mv $f $bad	# Not $text, save the ORIGINAL!
			fi
			echo "$server relaynews \`$bad' failed, status $st (see errlog)" |
							mail "$NEWSMASTER"
		fi

	done
	rm -f $tmp $rmlist
done

exit 0
