#!/bin/sh
# 
# @DEC_COPYRIGHT@
#
# HISTORY
# $Log: evmlog_explain.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.4.13  1999/03/30  21:40:18  Jem_Treadwell
# 	Final drop for BL23 from EVM shared sandbox
# 	[1999/03/29  21:31:20  Jem_Treadwell]
#
# Revision 1.1.2.14  1999/03/26  22:25:11  Jem_Treadwell
# 	Redirect dspmsg errors to /dev/null
# 	[1999/03/26  22:24:46  Jem_Treadwell]
# 
# Revision 1.1.2.13  1999/03/04  21:00:38  Jem_Treadwell
# 	Restructure to support set-ids and use functions
# 	[1999/03/04  21:00:09  Jem_Treadwell]
# 
# Revision 1.1.2.12  1998/11/05  22:59:22  Jem_Treadwell
# 	Removed debug line which tee'ed output to file ,x
# 	[1998/11/05  22:49:19  Jem_Treadwell]
# 
# Revision 1.1.2.11  1998/10/28  22:26:24  Jem_Treadwell
# 	Support the system vendor/product macro
# 	[1998/10/28  22:25:52  Jem_Treadwell]
# 
# Revision 1.1.2.10  1998/09/13  22:27:33  Jem_Treadwell
# 	Speed improvement - use sed script to handle catalog
# 	[1998/09/13  22:27:07  Jem_Treadwell]
# 
# Revision 1.1.2.9  1998/08/10  21:32:32  Jem_Treadwell
# 	Fixed problem with long explanations, & cleanup
# 	[1998/08/10  21:31:57  Jem_Treadwell]
# 
# Revision 1.1.2.8  1998/04/23  20:37:33  Jem_Treadwell
# 	Fixed spacing/indentation error
# 	[1998/04/23  20:37:00  Jem_Treadwell]
# 
# Revision 1.1.2.7  1998/04/21  18:26:53  Jem_Treadwell
# 	QAR 60531 - Remove ERROR from default messages
# 	[1998/04/21  18:25:54  Jem_Treadwell]
# 
# Revision 1.1.2.6  1998/02/13  23:53:07  Jem_Treadwell
# 	Get explanations from msg catalog
# 	[1998/02/13  23:52:38  Jem_Treadwell]
# 
# Revision 1.1.2.5  1997/12/08  14:24:31  Jem_Treadwell
# 	Fixed typo
# 	[1997/12/08  14:23:56  Jem_Treadwell]
# 
# Revision 1.1.2.4  1997/12/01  20:05:17  Jem_Treadwell
# 	Added more generic explanations
# 	[1997/12/01  20:00:00  Jem_Treadwell]
# 
# Revision 1.1.2.3  1997/09/08  19:04:47  Bruce_Gayliard
# 	Fixes for LINUX / portability.
# 	[1997/09/08  19:01:39  Bruce_Gayliard]
# 
# Revision 1.1.2.2  1997/07/28  12:14:18  Jem_Treadwell
# 	Initial submit
# 	[1997/07/28  12:13:32  Jem_Treadwell]
# 
# $EndLog$
# 
# @(#)$RCSfile: evmlog_explain.sh,v $ $Revision: 1.1.1.1 $ (DEC) $Date: 2003/12/11 15:40:53 $
# 

. /usr/sbin/evmcommandset

# FUTURE: When we provide localized catalogs this must be changed so
# that this default locale is used only if the catalog cannot be
# found in the user's own locale, or if no locale is specified.
LANG=en_US.ISO8859-1

EVNAME=$1

REF=$2
if [ "$REF" = "-" ]
then
    REF=""
fi

FORMAT="$FMT -72"

DEFMSG="Cannot find explanation for event \"$EVNAME\".
The supplied explanation reference is \"$REF\"."

MACRO_VP="@SYS_VP@"
VP=`evminfo -vp`

# This function scans the explanation catalog and outputs data in
# the form:
#      event-name set-id message-id
# 
dump_catalog()
{   if [ "$CATALOG_SET" = "" ]
    then
	# Display the whole catalog, and extract from each message
	# the event name, the set number and the message id:
        ${DSPCAT} $CATALOG | sed -n -e '
	     /^[0-9]* : [0-9]*/ !d
	     s+\}.*$++
	     s+[:\{]++g
	     '"s+${MACRO_VP}+${VP}+"'
	     s+\([0-9]*\) *\([0-9]*\) *\(.*$\)+\3 \1 \2+p'
    else
	# We have a set number, so just display messages from the
	# set.  We have to add back in the set number and message
	# id, since dspcat doesn't display them if you specify
	# the set.  We have to assume that the message numbers are
	# incremented monotonically.
        ${DSPCAT} $CATALOG $CATALOG_SET 2> /dev/null | sed -n -e '
	     /^{/ !d
	     s+^{++
	     s+\}.*$+ '$CATALOG_SET+'
	     '"s+${MACRO_VP}+${VP}+p" | awk '{printf("%s %s %s\n",$1,$2,++i)}'
    fi
}

# This function searches the input stream for the best event-name
# match, and outputs the catalog reference info in a form useful
# as an argument list to dspmsg(1).

find_match()
{   awk -v catalog=$CATALOG -v evname=$EVNAME ' \

    function print_dspmsg_args(a_set,a_msgid)
    {  printf("-s %s %s %s\n",a_set,catalog,a_msgid)
    }

    BEGIN { best_match_len = 0; set = ""; msgid = "" }

	  {  if ($1 == evname)
	     {	# If we have an exact match, nothing more to do:
	        print_dspmsg_args($2,$3)
		exit
	     }

	     match(evname,"^" $1);
	     if (RLENGTH > best_match_len)
	     {  # RLENGTH contains the length of the match.
		# Best match so far if next char of event name is a dot:
		if (substr(evname,RLENGTH+1,1) == ".")
		{  best_match_len = RLENGTH;
		   set = $2
		   msgid = $3
		}
	     }
	  }

    END   { if (set != "") print_dspmsg_args(set,msgid) }'
}


if [ "$REF" != "" ]
then
    # The format of the reference should be:
    # 	    cat:<catalog-name>[:set-number]
    #
    # Turn the reference into an NLS catalog name:
    IFS_SAVE=$IFS; IFS=:
    set $REF
    KEYWORD=$1; CATALOG=$2; CATALOG_SET=$3
    IFS=$IFS_SAVE

    # Check that the keyword field is valid:
    if [ "$KEYWORD" != "cat" ]
    then
        ( echo "    Event \"$EVNAME\" contains invalid reference \"$REF\".  "
	  echo "    No explanation is available.\n" 
	) | $FMT
	exit 1
    fi

    ARGS=`dump_catalog | find_match`

    if [ "$ARGS" != "" ]
    then
	# Display the message (format with indentation and wrapping):
        $DSPMSG $ARGS "$DEFMSG" | 
	    sed -e 's+^{[^}]*}++g' -e "s+$MACRO_VP+$VP+g" -e 's+^ *+    +' |
	    $FMT
	echo
	exit
    fi
fi 

echo "    No explanation is available for event \"$EVNAME\"." | $FMT
echo
