#! /bin/sh
# doexpire - overall administration for expire

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

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

lock="$NEWSCTL/LOCKexpire"
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
	echo "$0: expire apparently already running" | mail "$NEWSMASTER"
	exit 1
fi

cd $NEWSCTL

firstctl=
firstar=
while true
do
	size="`sizeof history history.pag history.dir`"
	if test " `spacefor $size control`" -le 0
	then
		if test " $firstctl" = " "
		then
			echo "$0: trouble finding space for work files" |
							mail "$NEWSMASTER"
			firstctl=n
		fi
	elif test " `spacefor 1 archive`" -le 0
	then
		if test " $firstar" = " "
		then
			echo "$0: trouble finding space for archiving" |
							mail "$NEWSMASTER"
			firstar=n
		fi
	else			# enough space both places
		break
	fi
	sleep 600		# and hope it will improve
done

expire $* $NEWSCTL/explist 2>/tmp/doex$$
if test -s /tmp/doex$$
then
	(echo 'expire problems:' ; cat /tmp/doex$$ ) | mail "$NEWSMASTER"
	rm -f /tmp/doex$$
	exit 1
fi
rm -f /tmp/doex$$
exit 0
