#! /bin/sh
. /usr/local/news/lib/innshellvars
#
# Submit path statistics based on ninpaths
# $Id: sendinpaths.in,v 1.1 2002/11/25 17:53:04 rra Exp $

# Assuming the ninpaths dump files are in ${MOST_LOGS}/path/inpaths.%d

cd ${MOST_LOGS}/path
ME=`${NEWSBIN}/innconfval pathhost`
report=30
keep=14
TMP=""
defaddr="pathsurvey@top1000.org top1000@anthologeek.net"

# Renice to give other processes priority, since this isn't too important.
renice 20 -p $$ > /dev/null

# Make report from (up to) $report days of dumps
LOGS=`find . -name 'inpaths.*' ! -size 0 -mtime -$report -print`
if [ -z "$LOGS" ] ; then
  echo "No data has been collected this month!"
  exit 1
fi

# for check dumps
for i in $LOGS
do
 ninpaths -u $i -r $ME > /dev/null 2>&1
 if test $? -eq 0; then :
  TMP="$TMP -u $i"
 fi
done

if [ "$1" = "-n" ] ; then
  ninpaths $TMP -r $ME
else
  ninpaths $TMP -r $ME |\
   $MAILCMD -s "inpaths $ME" ${1:-$defaddr}
  # remove dumps older than $keep days
  find . -name 'inpaths.*' -mtime +$keep -exec rm '{}' \;
fi

exit 0
