#!/bin/sh
# @(#) $Header: dodexpire,v 1.18 98/01/20 18:32:39 leres Exp $ (LBL)
# dodexpire - overall administration for dexpire (InterNetNews version)

##  =()<. @<_PATH_SHELLVARS>@>()=
. /usr/news/etc/innshellvars

##  =()<. @<_PATH_NEWSLIB>@/dexpire.conf>()=
. /usr/news/etc/dexpire.conf

prog=`basename $0`
errs=/tmp/dodex.$$
lock=${LOCKS}/LOCK.${prog}

# We only want one thread running at a time
shlock -p $$ -f ${lock} || {
	exit 0
}

trap 'rm -f ${lock} ; exit 1' 1 3 15

cd ${MOST_LOGS}

# Run runexp if there is an expires file to process
if [ -s expires ]; then
	rm -f runexp.log
	runexp -d -v > runexp.log 2>&1
	tstatus=$?
	if [ $tstatus != 0 ]; then
		${MAILCMD} -s "$prog problems (runexp)" \
		    "$DEXPIREMAIL" < runexp.log
	fi
fi

# topgroups
makedexplist

# Rotate logs
rm dexpire.log.13
mv dexpire.log.12 dexpire.log.13
mv dexpire.log.11 dexpire.log.12
mv dexpire.log.10 dexpire.log.11
mv dexpire.log.9 dexpire.log.10
mv dexpire.log.8 dexpire.log.9
mv dexpire.log.7 dexpire.log.8
mv dexpire.log.6 dexpire.log.7
mv dexpire.log.5 dexpire.log.6
mv dexpire.log.4 dexpire.log.5
mv dexpire.log.3 dexpire.log.4
mv dexpire.log.2 dexpire.log.3
mv dexpire.log.1 dexpire.log.2
mv dexpire.log.0 dexpire.log.1
mv dexpire.log dexpire.log.0

# Construct dexpire arguments
ARGS=""
for d in $DEXPIRE_FREE_LIST ; do
	if [ "$d" = "." ]; then
		eval onefs=\$DEXPIRE_FREE
	else
		eval onefs=${d}:\$DEXPIRE_FREE_${d}
	fi
	ARGS="$ARGS -f $onefs"
done

# Force innd to write out a current copy of the active file
ctlinnd -t120 reload active dexpire > $errs 2>&1
tstatus=$?
if [ $tstatus != 0 ]; then
	(cat $errs) | ${MAILCMD} \
	    -s "$prog problems (ctlinnd reload active)" "$DEXPIREMAIL"
fi

# Run dexpire
dexpire -lvu ${ARGS} > dexpire.log 2> $errs
tstatus=$?
if [ $tstatus != 0 ]; then
	echo dexpire exited with $tstatus >> $errs
fi

if [ -s $errs ]; then
	(cat $errs ; echo "" ; echo "----" ; echo "" ; cat dexpire.log) | \
	    ${MAILCMD} -s "$prog problems (dexpire)" "$DEXPIREMAIL"
fi
rm -f $errs

# Check to see if we were out of disk space
ctlinnd -t120 mode > $errs 2>&1
egrep "No space" $errs > /dev/null 2>&1
tstatus=$?
if [ $tstatus = 0 ]; then
	ctlinnd -s -t120 go ''
	(echo "Unthrottled innd after running dexpire:"
	 echo ""
	 cat $errs ) | ${MAILCMD} -s "$prog problems (spool was full)" \
	     "$DEXPIREMAIL"
fi

# Next check to see if we were throttled for too many open files
egrep "Too many open files" $errs > /dev/null 2>&1
tstatus=$?
if [ $tstatus = 0 ]; then
	ctlinnd -s -t120 go ''
	(echo "Unthrottled innd after running dexpire:"
	 echo ""
	 cat $errs ) | ${MAILCMD} -s "$prog problems (Too many open files)" \
	     "$DEXPIREMAIL"
fi
rm -f $errs

# Update the low marks in the active file
ctlinnd -t120 lowmark ${DLOWMARK} > $errs 2>&1
tstatus=$?
if [ $tstatus != 0 ]; then
	# That failed; figure out if it's because lowmark isn't supported
	egrep 'Usage error (unknown command)' $errs > /dev/null 2>&1
	tstatus=$?
	if [ $tstatus != 0 ]; then
		# An actual error of some kind
		${MAILCMD} -s "$prog problems (ctlinnd lowmark)" \
		    "$DEXPIREMAIL" < $errs
	else
		# Fall back on the old inefficient way
		while read GROUP lo ; do
			ctlinnd renumber ${GROUP} > $errs 2>&1
			tstatus=$?
			if [ $tstatus != 0 ]; then
				${MAILCMD} -s \
				    "$prog problems (active file renumber)" \
				    "$DEXPIREMAIL" < $errs
				break
			fi
		done < ${DLOWMARK}
	fi
fi

rm -f $errs $lock

exit 0
