#!/bin/sh
# innexpireiflow -- run inndoexpire if free space is short
# Author: tale@isc.org (David Lawrence)

# Loosely based on C News' expireiflow.
# Overall dexpire (or something similar) is a much better approach,
# and dexpire will soon be in an INN production release.

# CONFIGURE: this sets the limit on the amount of free space at 10k
# blocks higher than the point at which innwatch would throttle things,
# so expire has a chance to run and start cleaning things about before
# the server throttles.  You may want to play with this number some.
# The number is in blocks as returned by df.
ART_MARGIN=10240

# CONFIGURE: this is the same as ARTLIMIT, but for the history file
# directory.  history size grows much more slowly than spool size,
# so this buffer is an order of magnitude smaller.
LIB_MARGIN=1024

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

case "$#" in
0)	;;
*)      echo "Usage: $0" >&2; exit 2 ;;
esac

lock=$LOCKS/LOCK.expire

if [ -f $lock ]; then
  if shlock -c -p $$ -f $lock; then
     :
  else
     exit 0;
  fi
fi

## =()<ARTLIMIT=`expr $@<INNWATCH_SPOOLSPACE>@ + $ART_MARGIN`>()=
ARTLIMIT=`expr 8000 + $ART_MARGIN`
## =()<ARTFREE=`$INNDF $SPOOL | $AWK 'NR == 2 { print $@<INNWATCH_BLOCKS>@ }`>()=
ARTFREE=`$INNDF $SPOOL | $AWK 'NR == 2 { print $4 }`

## =()<HISTLIMIT=`expr $@<INNWATCH_LIBSPACE>@ + $HIST_MARGIN`>()=
HISTLIMIT=`expr 25000 + $HIST_MARGIN`
## =()<HISTFREE=`$INNDF $NEWSLIB | $AWK 'NR == 2 { print $@<INNWATCH_BLOCKS>@ }`>()=
HISTFREE=`$INNDF $NEWSLIB | $AWK 'NR == 2 { print $4 }`

if [ $ARTFREE -le $ARTLIMIT -o $HISTFREE -le $HISTLIMIT ]; then
  inndoexpire
fi

exit 0