#! /bin/sh
# inndoexpire -- overall administration for expire.
# Author: tale@isc.org (David Lawrence)

# Loosely based on C News's doexpire, to encapsulate expire's
# functionality outside of news.daily.  Trying to run _just_
# expire with news.daily can be a pain because of the need to
# remember what options to pass.

# =()<. ${NEWSCONFIG-@<NEWSCONFIG>@}>()=
. ${NEWSCONFIG-/var/news/innshellvars}

LOCK=$LOCKS/LOCK.expire
RMFILE=$MOST_LOGS/expire.rm
EXPIREFLAGS="-q -z $RMFILE"
# In EXPIREFLAGS:
#   add -v1 for statistics
#   add -l if articles are on more than one (unstriped) partition

if shlock -p $$ -f $LOCK; then
  trap 'rm -f $LOCK ; exit 1' 1 2 3 15
else
  (echo "$0: Locked by `cat $LOCK`"; echo; ${INNSTAT}) >&2
  exit 1
fi

cd $MOST_LOGS

# If the file expire exists, it is left over from a previous run, so
# just use it for removing articles and then rolling.  Normally this
# is encountered by the news boot procedure, making "inndoexpire"
# suitable to put in rc.news in place of "expirerm".  Using inndoexpire
# instead causes all the list ageing to happen normally.
if [ ! -s $RMFILE ]; then
  expire $EXPIREFLAGS
fi

# if the problem was a full disc while rebuilding, these are useless
# and continue to hog the disc.  leave history.n.dir, which is very small,
# as evidence that the rename failed.
rm -f $NEWSLIB/history.n $NEWSLIB/history.n.pag

sort -o $RMFILE $RMFILE

expirerm $RMFILE

test -f expire.list || exit 1   # an error with expirerm occurred

test -f expire.list.6$Z && mv -f expire.list.6$Z expire.list.7$Z
test -f expire.list.5$Z && mv -f expire.list.5$Z expire.list.6$Z
test -f expire.list.4$Z && mv -f expire.list.4$Z expire.list.5$Z
test -f expire.list.3$Z && mv -f expire.list.3$Z expire.list.4$Z
test -f expire.list.2$Z && mv -f expire.list.2$Z expire.list.3$Z
test -f expire.list.1$Z && mv -f expire.list.1$Z expire.list.2$Z
test -f expire.list.0$Z && mv -f expire.list.0$Z expire.list.1$Z
if [ -f expire.list.0 ]; then
  echo "$0: will not clobber `pwd`/expire.list.0 with `pwd`/expire.list" >&2
  exit 1
fi
mv expire.list expire.list.0
$COMPRESS -f expire.list.0

rm -f $LOCK
exit 0
