#! /bin/sh
# Incoming-news spooling.
# We ignore arguments -- it looks tempting to put "$*" after cat and
# newsspool, but there are security problems.

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

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

# check space, assuming a pretty large batch (no cheap way to find real size)
counter=1
while test " `spacefor 250000 incoming`" -le 0
do
	# Warn sysadmin -- he just might be listening and able to act -- and
	# stall a little bit in hopes that it's transient.
	if test " $counter" -eq 1
	then
		(
			echo 'Incoming news will be discarded due to space'
			echo 'shortage unless more room becomes available'
			echo 'within 15 min.'
		) | mail "$NEWSMASTER"
	fi
	sleep 300
	if test " $counter" -gt 1111		# four tries is plenty
	then
		# oh no! -- nothing we can do, really...
		cat >/dev/null
		echo incoming news discarded due to space shortage >&2
		exit 1
	fi
	if test ! -f $NEWSCTL/rnews.stall
	then
		counter="1$counter"
	else			# avoid repeating the sysadmin warning
		counter=2
	fi
done

# spool it
if test -f $NEWSCTL/rnews.immed
then
	opt=-i
else
	opt=
fi
if newsspool $opt >/tmp/ngripe.$$ 2>&1
then
	rm -f /tmp/ngripe.$$
	exit 0
else
	# there really isn't any way to save the data if newsspool fails,
	# not without causing other problems
	(
		echo newsspool failed!!!
		cat /tmp/ngripe.$$
	) | mail "$NEWSMASTER"
	cat /tmp/ngripe.$$ >&2
	rm -f /tmp/ngripe.$$
	exit 1
fi
