#!/bin/sh
# $Id: log-maint,v 1.6 1998/07/31 18:38:50 vikas Exp $
#
# daily/weekly maintenance for NOCOL log files.
#
#    - run 'logstats' on the LOGFILE
#    - move old logs and then sighup the noclogd process.
#
## Tweak these
##
##
ROOTDIR="/tmp/pkgbuild/net/nocol/work/.destdir/usr/pkg"
PIDDIR="/tmp/pkgbuild/net/nocol/work/.destdir/var/run"
OPSMAIL="root@localhost" 	        # to get email from logstats

LOGDIR="${ROOTDIR}/logs"
LOGFILE="critical"		# which file should logstats process
OLD="${LOGDIR}/old"
PIDFILE="${PIDDIR}/noclogd.pid"

LOGSTATS="${ROOTDIR}/bin/logstats"	# location of the script
MAIL="/usr/bin/mail"

KEEPOLD="6"			# number of old logfile revisions
COMPRESS="gzip"		        # compress or gzip
EXTN="gz"

OPATH=$PATH
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/etc:/usr/etc
PATH=${PATH}:/usr/ucb:/usr/bsd:/usr/local/bin:/usr/local/gnu/bin:${OPATH}

(${COMPRESS} -c $0 >/dev/null) 2>&1 | egrep -i 'not.*found' >/dev/null 2>&1
if [ $? = 0 ]; then
  COMPRESS=compress
  EXTN=Z
fi

[ -d $LOGDIR ]  || mkdir $LOGDIR
if [ ! -d $LOGDIR ]; then
  echo "$0 - Log directory $LOGDIR not found, exiting"
  exit 1
fi

[ -d $OLD ] || mkdir $OLD
if [ ! -d $OLD ]; then
  echo "$0 - Could not make directory $OLD, exiting"
  exit 1
fi

cd $LOGDIR

## Run logstats to send out the reports (run it on LOGFILE)
#
if test -x ${LOGSTATS} ; then
  if test -s ${LOGFILE} ; then
    if [ "X${MAIL}" = "X/usr/ucb/mail" ]; then
      ${LOGSTATS} -f ${LOGFILE}  | $MAIL -s "NOCOL logstats" $OPSMAIL
    else
      ${LOGSTATS} -f ${LOGFILE}  | $MAIL $OPSMAIL
    fi
  fi
fi

# since we are already in $LOGDIR, compress and move each file into OLD dir
for f in *
do
 if [ -f $f ]; then
   j=`expr $KEEPOLD - 1`
   while [ ! "$j" = "0" ]
   do
	i=`expr $j - 1`
	if [ -f $OLD/$f.$i.${EXTN} ]; then
	    mv $OLD/$f.$i.${EXTN} $OLD/$f.$j.${EXTN}
	fi
	j=$i
   done
   ${COMPRESS} -c $f > $OLD/$f.0.${EXTN}
   cp /dev/null $f
 fi
done

##
# Now send signal to noclogd so that it closes and reopens all file descriptors
sleep 5;
kill -HUP `head -1 $PIDFILE`
exit 0
##
