#!/bin/sh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2000,2019 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# sccsid = "@(#)62   1.4   src/rsct/ffdc/bin/fcslogrpt.sh, ffdc, rsct_rady, rady2035a 11/12/15 16:42:55"
# =============================================================================
# Module Name:	fcslogrpt.sh
#
# Component:	ffdc
#
# Description:	Scans a normal text file, looking for entries within that file
#		that follow the First Failure Data Capture entry format.  The
#		file can be scanned to locate all FFDC format entries, or an
#		entry for a specific First Failure Data Capture Failure
#		Identifier.
#
#		The text file is assumed to be created and appended by the
#		syslogd daemon.  All entries will be contained on a single
#		line of the file, with a maximum line length of 1024 ASCII
#		characters.  FFDC entries will represent imbedded newlines
#		with two ASCII characters (^M), as will tabstops (^I).  This
#		script will expand these characters before writing the entry
#		to standard output.
#
# Usage:	fcslogrpt [ <FFDCID> ] <logfile1> [ <logfile2> ... ]
#
#		where:	FFDCID		First Failure Data Capture Failure
#					Identifier for entry being sought
#			logfile1	First syslogd log file to examine for
#					the entry (required)
#			logfile2	Subsequent syslogd log file to examine
#					for the entry (optional)
#
# Input:	ASCII text files, created by the syslogd daemon
#
# Output:	If no entries matching the invokation criteria are detected, no
#		output is generated.  If matching entries are found, formatted
#		versions are displayed to standard output.
#
# Exit Status:	(int)	0	Entry/entries found and displayed
#			1	No matching entries found
#			2	Incorrect usage
# =============================================================================

# Set path to known value
PATH=/opt/rsct/bin:/usr/xpg4/bin:/usr/bin:/usr/sbin:/bin
export PATH

# ------------------------------------------- #
# Check usage and store FFDC ID to search for #
# ------------------------------------------- #
if test $# -eq 0
then
	echo "Usage: fcslogrpt  [ FFDC_ID ]  filename [ filename ... ]"
	exit 2
fi
if test $# -gt 1
then
	FFDCID=$1
	shift
else
	FFDCID=""
fi
# ------------------------------- #
# Scan all files for FFDC entries #
# ------------------------------- #
FOUND=0
for FILE in $*
do
	# ---------------------------- #
	# Search for specific FFDC IDs #
	# ---------------------------- #
	if test -n "$FFDCID"
	then
		ENTRY=`sed -n '
\?Recorded using libct_ffdc.a?! {
d
}
\?Recorded using libct_ffdc.a? {
\?Error ID: '"$FFDCID"'?!{
d
}
\?Error ID: '"$FFDCID"'?{
p
}
}' $FILE`
		if test -n "$ENTRY"
		then
			FOUND=1
			OFS=$IFS
			IFS=""
			for LINE in $ENTRY
			do
				echo $LINE | sed '
{
s/:::/\
	/g
}'
			done
			IFS=$OFS
			exit 0
		fi
	# ------------------------------------- #
	# Search for any FFDC formatted entries #
	# ------------------------------------- #
	else
		ENTRY=`sed -n '
\?Recorded using libct_ffdc.a?! {
d
}
\?Recorded using libct_ffdc.a? {
p
}' $FILE`
		if test -n "$ENTRY"
		then
			FOUND=1
			OFS=$IFS
			IFS=""
			for LINE in $ENTRY
			do
				echo $LINE | sed '
{
s/:::/\
	/g
}'
			done
			IFS=$OFS
		fi
	fi
done
# ---------------------------------------------- #
# Make sure at least one FFDC ID entry was found #
# ---------------------------------------------- #
if test $FOUND -eq 0
then
	exit 1
fi
# -------------------------------------------- #
# At least one FFDC ID entry found & displayed #
# -------------------------------------------- #
exit 0