#!/bin/bash 
#
#  getSedcLogValues
#
#  Go through SEDC log files and extract data for a certain scanId and/or scanId and component name.
# 
#
# $Header$
#
#####################################################
# Copyright (c) 2008 Cray Inc. All Rights Reserved. #
#####################################################


#
# Function: usage
# Description:
#     Write usage message to std out
# Arguments:
#     None
# Returns:
#     Nothing - exits process with status of 255
# Notes:
#

usage()
{
	echo "Usage: $scriptName [-h] [-s] [-c <componentName>]  <scanId> <fileName> ... <fileName>" 
	echo "       where -h means show this message."
	echo "       where -s <fileName> means show scanIds in file."
	echo "       where -c <componentName> <scanId> <fileName> means show only records for the indicated componentName."
	echo "       where <scanId> <fileName> will show all records for indicated scanId."
	exit 255
}

#
# Function: getscanIds
# Description:
#     Get the scan ids out of a file. 
#     The first line of a SEDC log file represents a comma separated list describing the contents
#     of the lines that follow. The format is "service id,time,scanid,scanid,...,scanid
#     This function parses the first line and extracts the list of scanids, placing them in the
#     global variable "scanIdList".
# Arguments:
#     Name of file from which to obtain the list of scanids.
# Returns:
#     Nothing.
# Notes:
#     The functions fills the global variable "scanIdList".
#
getscanIds()
{
	((count = 0))
	header=`head -1 $1|sed -e s/,/\ /g`
	for i in $header
	do
		if((++count > 3))
		then
			scanIdList="$scanIdList $i"
		fi
	done
}

#
# Function: showscanIds
# Description:
#     Write the scanIdList to std. out.
# Arguments:
#     None.
# Returns:
#     Nothing.
# Notes:
#
showscanIds()
{
	for i in $scanIdList
	do
		echo $i
	done
}

#
# Function: getidFieldNum
# Description:
#     Find the field number (position within a scan record) for the
#     named scanid.
# Arguments:
#     The scanid name to search for.
# Returns:
#     None.
# Notes:
#     Sets the global variable fieldNum to equal the field number of the 
#     indicated scanid.
#
getidFieldNum()
{
	
	((count = 0))
	for i in $scanIdList
	do
		((++count))
		if test $i = $1;
		then
			((fieldNum = count))
			break;
		fi
	done
	((fieldNum = fieldNum + 2))
}

#
# Function: checkFile
# Description:
#     Determine if a file exists.
# Arguments:
#     The name of the file to check.
# Returns:
#     None.  If the indicated file is not found, this function displays an error message
#            and exits process with status of 1.
# Notes:
#
checkFile()
{
	if ! test -f $1; then
		echo "File $1 not found."
		exit 1
	fi
}

#
# Function: checkScanId
# Description:
#     Determine if a file contains records for a named scanid.
# Arguments:
#     The name of the file to check, and the name of the scanid to look for.
# Returns:
#     None.  If the indicated scanid is not found in the indicated file, this function 
#            displays an error message and exits process with status of 1.
# Notes:
#
checkScanId()
{
	grep $2 $1 > /dev/null
	if(($? > 0))
	then
		echo "scanId $2 not found in file $1."
		exit 1
	fi
}

#
# Function: printMatchingRecords
# Description:
#     Go through the list of file name arguments and print all records that match the indicated scanid.
# Arguments:
#     Optional component name. If given, this indicates that only the matching records for this component 
#     will be printed.
# Returns:
#     None.  
# Notes:
#
printMatchingRecords()
{
	for i in $fnames
	do
		if test -z $1
		then
			tail +1 $i| sed -e s/::// |\
			awk -v fieldNum=$fieldNum '{FS=","} {if (length($fieldNum) > 1) print $1 "   "   $2  "   "  $fieldNum }' |\
			grep -v NA
		else
			tail +1 $i| sed -e s/::// |\
			awk -v fieldNum=$fieldNum '{FS=","} {if (length($fieldNum) > 1) print $1 "   "   $2  "   "  $fieldNum }' |\
			grep $1 | grep -v NA
		fi
	done
}

#
# Function: setLocale
# Description:
#     Sets the locale value for LC_CTYPE used in this script, as a workaround
#     to avoid a awk bug in certain versions of linux.
# Arguments:
#     None.
# Returns:
#     None. 
# Notes:
#
setLocale()
{
	prevLC_CTYPE=`locale|grep LC_CTYPE`
echo LC_CTYPE = $prevLC_CTYPE
	export	LC_CTYPE="C"
}

#
# Function: restoreLocale
# Description:
#     Restores the value of LC_CTYPE.
# Arguments:
#     None.
# Returns:
#     None.
# Notes:
#     See setLocale
#
restoreLocale()
{
        export  LC_CTYPE=$prevLC_CTYPE
}

#
# Function: cdToLogDir
# Description:
#     Look in sedc ini file for the name of the SEDC log directory. If found, cd to this directory.
# Arguments:
#     None.
# Returns:
#     None. If SEDC log directory is not found, print error message and exit process with status of 1. 
# Notes:
#
cdToLogDir()
{
	line=`grep STR:file_data_dir $SEDC_INI_FILE`
	if(($? > 0))
	then
		echo "SEDC ini file not found: $SEDC_INI_FILE "
		exit 1
	fi
        logDir=`echo $line|awk '{print $3}'`
	if(($? > 0))
	then
		echo "SEDC log directory not found".
		exit 1
	fi
	cd $logDir
}

#
# Run Script 
#


SEDC_INI_FILE=/opt/cray/etc/sedc_srv.ini

scriptName=`basename $0`

if (( $# < 2 ))
then
	usage
fi

setLocale
cdToLogDir
component=
onlyShowScanIds=
while getopts 'shc:' OPTION
	do
		case $OPTION in
                s)      checkFile $2
                        getscanIds $2
                        showscanIds
                        exit 0
			;;
		c)	component=$2
			if(( $# < 4))
			then
				usage
			fi
			;;
		?)	usage
			;;
		esac
	done
shift $(($OPTIND - 1))

scanId=$1
firstFile=$2
shift
fnames=$*
checkFile $firstFile
checkScanId $firstFile $scanId
getscanIds $firstFile
getidFieldNum $scanId
printMatchingRecords $component
restoreLocale
exit 0





