#!/bin/sh
# 
# @DEC_COPYRIGHT@
#
# HISTORY
# $Log: evmlog_cleanup.sh,v $
# Revision 1.1.1.1  2003/12/11 15:40:53  ajay
# Importing Evm sources.
#
# Revision 1.1.1.1  2002/09/12 15:43:58  lsn
# EVM source - Linux initial version
#
# Revision 1.1.18.1  2001/09/26  18:58:37  Anthony_Hoffman
# 	redo of wildcatos submits
#
# Revision 1.1.10.2  2001/07/30  18:08:35  Anthony_Hoffman
# 	wc.evm.002.portability
#
# Revision 1.1.10.1  2001/04/12  21:04:34  Jem_Treadwell
# 	QAR 84471: Fixed the logfile find algorithm so that it succeeds even if
# 	there are a huge number of events in the directory.  Previously, the
# 	find failed because the command exceeded the maximum command line
# 	length.
#
# Revision 1.1.4.5  1999/03/19  21:53:40  Bruce_Gayliard
# 	Drop from EVM shared sandbox to steel BL 23.
# 	[1999/03/19  13:30:56  Bruce_Gayliard]
#
# Revision 1.1.2.5  1999/02/22  16:18:23  Jem_Treadwell
# 	Support retrieval and cleanup in alternate directory.
# 	[1999/02/22  15:24:13  Jem_Treadwell]
# 
# Revision 1.1.2.4  1998/11/09  16:41:36  Jem_Treadwell
# 	Added support for new /var/evm directory structure
# 	[1998/11/09  16:40:48  Jem_Treadwell]
# 
# Revision 1.1.2.3  1998/05/21  15:05:15  Jem_Treadwell
# 	Changed default expiry from 30 to 31 days
# 	[1998/05/21  15:04:44  Jem_Treadwell]
# 
# Revision 1.1.2.2  1998/03/25  23:22:18  Jem_Treadwell
# 	Initial submit
# 	[1998/03/25  23:21:40  Jem_Treadwell]
# 
# $EndLog$
# 
# @(#)$RCSfile: evmlog_cleanup.sh,v $ $Revision: 1.1.1.1 $ (DEC) $Date: 2003/12/11 15:40:53 $
# 

# This script handles the EVM log daily cleanup.  Depending
# on the arguments passed, the script will archive aged logs
# by compressing them, and delete expired logs.

. /usr/sbin/evmcommandset

#
# Function: do_find()
#
# This function writes to stdout a list of all files in the current
# directory that are older than the number of days specified by $1.
#
# NOTE: The function uses "ls" rather than "find" to produce the initial
# file list, to avoid recursing sub-directories, because "find . -prune"
# produces no useful output.  Use of "find" is still required for
# filtering files according to their attributes.
# 
# Preventing recursion allows users to preserve interesting logfiles by
# storing them in a subdirectory.  This approach requires the filenames
# to be passed to "find" as discrete pathname arguments, and since the
# number of logfiles can be very large, "xargs" is used to prevent the
# command line from exceeding limits.  Since the filename arguments to
# "find" have to be inserted before its options, the "insert" (-i) form 
# of "xargs" must be used, rather than the usual form, which appends
# arguments to the command.  This introduces a limited buffer size, which
# is why xargs is invoked twice:
# - The first time to produce a file list with each line limited to a
#   size of 220, since the -i option used in the next command limits 
#   the total size of the inserted argument to 255 characters.
# - Then to produce the "find" commands by substituting filenames
#   output by the previous command in place of the "xx".
# See the xargs(1) reference page for more information.

do_find()
{   DAYS=$1
    ${LS} | ${XARGS} -s 220 echo |
	${XARGS} -ixx echo ${FIND} xx -prune -type f -mtime +$DAYS | /bin/sh -s
}

ARCHIVE_DAYS=7
EXPIRE_DAYS=31

LOGNAME=evmlog
LOG_INFO_FILE=/var/run/evmlogger.info
DEFAULT_LOGPATH="/var/evm/${LOGNAME}/${LOGNAME}.dated"

# The dated egrep pattern matches logfile names of the form
# evmlog.yyyymmdd[_nnn], where nnn is the generation number.
DATED_PATTERN='.[12][90][0-9][0-9][01][0-9][0-3][0-9]_{0,1}[0-9]{0,3}'
UNDATED_PATTERN='_{0,1}[0-9]{0,3}'

# This pattern matches the archive filename:
ARCHIVE_SUFFIX='\.gz'

if [ "$1" != "" ]
then
    ARCHIVE_DAYS=$1
fi

if [ "$2" != "" ]
then
    EXPIRE_DAYS=$2
fi

# Get the names of the primary and alternate log directories from
# the logger's output config file:
LOGPATHS=
if [ -r $LOG_INFO_FILE ]
then
    LOGPATHS=`${GREP} "^eventlog	${LOGNAME}" < $LOG_INFO_FILE |
		${AWK} '{print $3 " " $4}'`
fi

if [ "$LOGPATHS" = "" ]
then
    LOGPATHS=$DEFAULT_LOGPATH
fi

HERE=`pwd`
for path in $LOGPATHS
do
    if [ $path = "-" ]
    then
	continue
    fi

    dir=`${DIRNAME} $path`
    filename=`${BASENAME} $path`

    # Strip any ".dated" suffix from the filename:
    file_prefix=`${BASENAME} $filename .dated`

    # The pattern we use to find the files depends on whether there was
    # a ".dated" suffix:
    if [ $filename != $file_prefix ]
    then
	LOG_PATTERN=${file_prefix}${DATED_PATTERN}
    else
	LOG_PATTERN=${filename}${UNDATED_PATTERN}
    fi
    ARCHIVE_PATTERN=${LOG_PATTERN}${ARCHIVE_SUFFIX}

    cd $dir
    # If the archive period is non-zero we'll archive any aged logfiles.
    # If the archive period is zero and the delete period is non-zero,
    # we'll delete any logfiles which are older than the delete period.
    if [ $ARCHIVE_DAYS != 0 ]
    then
	# Compress all logfiles which are older than the archive period:
	do_find $ARCHIVE_DAYS |
	    ${EGREP} "$LOG_PATTERN"'$' | ${XARGS} ${GZIP} -N
    else
	if [ $EXPIRE_DAYS != 0 ]
	then
	    # Delete all logfiles which are older than the delete period:
	    do_find $EXPIRE_DAYS |
		${EGREP} "$LOG_PATTERN"'$' | ${XARGS} $RM -f
	fi
    fi

    # If the delete period is non-zero, we'll delete any archived
    # files which are older than the delete period.
    if [ $EXPIRE_DAYS != 0 ]
    then
	# Delete all archived files which are older than the delete period:
	do_find $EXPIRE_DAYS |
	    ${EGREP} "$ARCHIVE_PATTERN"'$' | ${XARGS} $RM -f
    fi
    cd $HERE
done
