#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 1999,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 
#-----------------------------------------------------------------------------#
# Module Name:	fcfilter
#
# Component:	ffdc
#
# Description:	This script scans the input, searching for FFDC Failure
#		Identifiers (which are labelled with the FFDC Failure Identifier
#		message number, 2615-000).  Any FFDC Failure Identifiers that
#		are found are displayed to standard output, minus the message
#		number.
#
# Usage:	fcfilter [filename] [...]
#
# Arguments:	filename	Optional file that contains the input to be
#				scanned.  More than one file name may be
#				provided.  If a file name is not provided, the
#				command reads input from standard input.
#
# Exit Status:	0	Normal completion.  Note that this does not mean that
#			any FFDC Failure Identifiers were detected, only that
#			the script completed without interruption or fatal
#			error.
#		>0	Number of the signal that interrupted the completion
#			of this script.
#
# NLS Concerns:	This script assumes that message numbers from AIX applications
#		using message catalogs are always presented using their Arabic
#		(ASCII) form, regardless of the local set by the system (for
#		example, when LC_ALL is set to jp_JP, numbers appear as Arabic
#		numbers instead of the Japanese equivilent).  If this is not
#		the case, not only will this script not work, it will be
#		impossible to provide a generic script to locate FFDC Failure
#		Identifiers that work in all locales.  Specific scripts will
#		need to be written for each locale (ugh).
#
# >>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<  This script depends on FFDC Failure
# >>> NOTES TO CODE MAINTAINERS <<<  Identifiers being preceeded by the message
# >>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<  number used by the fc_display_fid() API
#		                     routine to present FFDC IDs.  This message
#		number is 2615-000.  If any data is interleaved between the
#		message number and the FFDC Failure Identifier, this script
#		will present incorrect information.  If the message number
#		used to present the FFDC Failure Identifier changes, this
#		module MUST BE CHANGED to use the new number.  If FFDC Failure
#		Identifiers exist in the input but are not preceeded by the
#		FFDC ID message number, this script will not locate these
#		identifiers.
#
#		Also, this module assumes that the length of the FFDC Failure
#		Identifier is 42 characters.  This definition is made in the
#		<rsct/ct_ffdc.h> header file.  If this length is altered, this
#		module MUST BE CHANGED to use the new length and support the
#		old length as well.
#-----------------------------------------------------------------------------#
# sccsid = "@(#)08   1.3   src/rsct/ffdc/bin/fcfilter.sh, ffdc, rsct_rady, rady2035a 11/12/15 16:42:53"
#-----------------------------------------------------------------------------#
#
#-----------------------------------------------------------------------------#
# Function:	do_awk
# Description:	This "awk" script reads input from a file, searching for the
#		specific message used to report an FFDC Failure Identifier
#		(message number 2615-000).  If the message number is found,
#		the next item of data is expected to be the FFDC Failure
#		Identifier - this value is obtained and displayed.  The "awk"
#		script logic is:
#			for (every field in the input record) {
#			  if (the field value is "2615-000") {
#			    if ("2615-000" is last field in the record) {
#			      read next record
#		              print first field of record (should be FFDC ID)
#			      perform main loop logic on this new record
#			    }
#			    else {
#			      skip to next field in record
#			      display contents of field
#			      continue main loop logic on the current record
#			    }
#			  }
#			}
# Parameters:	$1 - Name of the file containing the input
# Notes:	If more than one "2615-000" message is contained in the input
#		file, more than one FFDC Failure Identifier will be displayed
#		by this function.  Each FFDC Failure Identifier will be on its
#		own line.
# NOTE TO CODE MAINTAINERS: Note the dependency on the message number and the
#		length of the FFDC Failure Identifier in the "awk" script.
# Return Codes:	None.
#-----------------------------------------------------------------------------#
function do_awk
{
	awk ' \
		BEGIN { PFR = 0 } \
		{ \
			if ( PFR == 1 ) { \
				if ( length($1) == 42 ) { \
					print $1 \
				} \
				PFR = 0 \
			} \
			for ( I = 1; I <= NF; I++ ) { \
				if ( $I == "2615-000" ) { \
					if ( I < NF ) { \
						I++ ; \
						if ( length($I) == 42 ) { \
							print $I \
						} \
					} \
				 	else { \
						PFR = 1 \
					} \
				} \
			} \
		} ' \
	$1
}
#-----------------------------------------------------------------------------#
#   -- MAINLINE CODE -- MAINLINE CODE -- MAINLINE CODE -- MAINLINE CODE --    #
#-----------------------------------------------------------------------------#
# Set path to known value
PATH=/opt/rsct/bin:/usr/xpg4/bin:/usr/bin:/usr/sbin:/bin
export PATH
#
# Variables used
#
typeset LINE
integer LOOPCTR
#
# Signal handlers to clean up any temporary files on premature termination
#
trap 'rm -f /tmp/fcawk.$$; exit 2' INT
trap 'rm -f /tmp/fcawk.$$; exit 3' QUIT
trap 'rm -f /tmp/fcawk.$$; exit 6' ABRT
trap 'rm -f /tmp/fcawk.$$; exit 15' TERM
trap 'rm -f /tmp/fcawk.$$; exit 17' STOP
trap 'rm -f /tmp/fcawk.$$; exit 33' DANGER
#
# Locate FFDC Failure Identifiers in the input
#
if (($# == 0))
then
	rm -f /tmp/fcfilter.$$
	read LINE
	while [[ -n $LINE ]]
	do
		print $LINE >> /tmp/fcfilter.$$
		read LINE
	done
	do_awk /tmp/fcfilter.$$
	rm -f /tmp/fcfilter.$$
else
	LOOPCTR=0
	while ((LOOPCTR < $#))
	do
		if [[ -f $1 ]]
		then
			do_awk $1
		fi
		shift
	done
fi
#
# Processing complete
#
exit 0