#!/bin/sh
# 
# @DEC_COPYRIGHT@
#
# HISTORY
# $Log: evmlog_get.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.16.1  2001/09/17  18:28:44  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.  This fix is backported from wildcat.
#
# Revision 1.1.4.5  1999/03/19  21:53:42  Bruce_Gayliard
# 	Drop from EVM shared sandbox to steel BL 23.
# 	[1999/03/19  13:31:01  Bruce_Gayliard]
#
# Revision 1.1.2.6  1999/02/25  20:06:08  Jem_Treadwell
# 	QAR 69080 - switch off filename expansion at appropriate points, to
# 	prevent a lone "*" in a filter string from being expanded.
# 	[1999/02/25  20:05:43  Jem_Treadwell]
# 
# Revision 1.1.2.5  1999/02/22  16:18:22  Jem_Treadwell
# 	Support retrieval from alternate directory.
# 	[1999/02/22  15:24:37  Jem_Treadwell]
# 
# Revision 1.1.2.4  1998/11/09  16:41:35  Jem_Treadwell
# 	Added support for new /var/evm directory structure
# 	[1998/11/09  16:40:53  Jem_Treadwell]
# 
# Revision 1.1.2.3  1997/09/08  19:04:48  Bruce_Gayliard
# 	Fixes for LINUX / portability.
# 	[1997/09/08  19:01:41  Bruce_Gayliard]
# 
# Revision 1.1.2.2  1997/07/28  12:14:20  Jem_Treadwell
# 	Initial submit
# 	[1997/07/28  12:13:34  Jem_Treadwell]
# 
# $EndLog$
# 
# @(#)$RCSfile: evmlog_get.sh,v $ $Revision: 1.1.1.1 $ (DEC) $Date: 2003/12/11 15:40:53 $
# 

. /usr/sbin/evmcommandset

#
# Function: do_find()
#
# This function writes to stdout a list of all files in the current
# directory.
#
# 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()
{   ${LS} -t -r | ${XARGS} -s 220 echo |
	${XARGS} -ixx echo ${FIND} xx -prune -type f | /bin/sh -s
}

USAGE="Usage: $0 [-f filter-string]"
FILTER=

EXIT_OK=0
EXIT_USAGE=1

LOG_INFO_FILE=/var/run/evmlogger.info
LOGNAME=evmlog
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}'

# Inhibit filename expansion while we handle the arguments, to avoid
# wildcards in the filter arg being expanded into filenames:
set -f

while [ $# -gt 0 ]
do
	case $1 in
	-f)	if [ $# -lt 2 ]
		then
			${ECHO} $USAGE >&2
			exit $EXIT_USAGE
		fi
		FILTER="-f \"$2\""
		shift
		shift
		;;

	*)	${ECHO} $USAGE >&2
		exit $EXIT_USAGE
		;;
	esac
done

# Re-enable filename expansion:
set +f

# 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

# Scan for logfiles in the configured directories:
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
	PATTERN=${file_prefix}${DATED_PATTERN}
    else
	PATTERN=${filename}${UNDATED_PATTERN}
    fi

    cd $dir
    do_find | ${EGREP} "$PATTERN"'$' |
    while read logfile
    do 
        # Ignore the file if we can't read it:
        if [ -r $logfile ]
        then
	    # We could pipe the event stream through a single copy of
	    # evmshow, but running it individually for each file 
	    # protects against premature termination if a file is
	    # corrupt.
	    #
	    # Switch off filename expansion again, to protect the
	    # filter string:
	    set -f
	    eval /usr/bin/evmshow -r $FILTER $logfile
	    set +f
        fi
    done
    cd $HERE
done
