#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/lib/nim/methods/c_showlog.sh 1.3.1.4 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1993,2014 
# 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 
# @(#)93        1.3.1.4  src/bos/usr/lib/nim/methods/c_showlog.sh, cmdnim, bos720 5/19/14 09:47:45
#
#   COMPONENT_NAME: CMDNIM
#
#   FUNCTIONS: ./usr/lib/nim/methods/c_showlog.sh
#
#   ORIGINS: 27
#
#
#   (C) COPYRIGHT International Business Machines Corp. 1993, 1995
#   All Rights Reserved
#   Licensed Materials - Property of IBM
#   US Government Users Restricted Rights - Use, duplication or
#   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#

# include common NIM shell defines/functions
NIMPATH=${0%/*}
NIMPATH=${NIMPATH%/*}
[[ ${NIMPATH} = ${0} ]] && NIMPATH=/usr/lpp/bos.sysmgt/nim
NIM_METHODS="${NIMPATH}/methods"
. ${NIM_METHODS}/c_sh_lib

#---------------------------- local defines     --------------------------------

#---------------------------- module globals    --------------------------------
REQUIRED_ATTRS="location"
OPTIONAL_ATTRS="full_log show_usr_spot_root alog"
PROGRAM_NAME="c_showlog"
location=""
name=""
verbose=""
usr_spot_root_loc="/usr/lpp/bos/inst_root/var/adm/ras/nim.installp"

#---------------------------- show_last_entry    -------------------------------
#
# NAME: show_last_entry
#
# FUNCTION:
#		Displays the last entry in the log file
#
# EXECUTION ENVIRONMENT:
#
# NOTES:
#	Looks for last BEGIN time.  If none found, cats entire log.
#	If found, prints log until "END" time or end of file is reached.
#
# RECOVERY OPERATION:
#
# DATA STRUCTURES:
#		parameters:
#			location = path name to log file
#		global:
#
# RETURNS: 
#
# OUTPUT:
#		text from log file specified at location
#-------------------------------------------------------------------------------
function show_last_entry {

	typeset location=${1}

        begin_time=`/usr/bin/awk '{FS=":"}  {\
           if (/^BEGIN/) { \
                 if (match($5,/[0-9]+/)) \
                         year=substr($5,RSTART+RLENGTH-4,4);\
                 else year="0000";\
                 date_str=$5; \
           }
        } END {print (date_str);}' ${location}`

	if [ -z "${begin_time}" ]
	then
		${CAT} ${location} 2>${ERR} || err_from_cmd ${CAT}
	else
		${AWK} -v begin_time=${begin_time}  \
				'BEGIN {  \
					FS=":"; \
					print_line = 0; \
				} \
				($1 == "BEGIN" && $5 == begin_time) { \
					print_line = 1; \
				} \
				(print_line == 1) { \
					print; \
					if ($1 == "END") \
						print_line = 0;\
					
				}' ${location} || err_from_cmd ${AWK}
	fi
}

#------------------------------ show_liveupdate --------------------------------
#
# NAME: show_liveupdate
#
# FUNCTION:
#               displays all the logs at the location specified for liveupdate
#
# EXECUTION ENVIRONMENT:
#
# NOTES:
#
# RECOVERY OPERATION:
#
# DATA STRUCTURES:
#               parameters:
#                       see required and optional args
#               global:
#
# RETURNS: (int)
#
# OUTPUT:
#-------------------------------------------------------------------------------
function show_liveupdate {
	logs=`$LS $location`
	if [[ -z $logs ]]; then
		#
		# 0042-218 c_showlog: The log file is either empty or does not exist at
		# 	<var/adm/ras/liveupdate/logs>
		#
		${DSPMSG} -s ${ERR_SET} cmdnim.cat ${ERR_LOG_FILE_NOT_FND} \
			'0042-218 %s: The log file is either empty or does not exist at\n\t%s' \
			${PROGRAM_NAME} $location
	else
		for logfile in $logs; do
			echo "############## $logfile ##############"
			echo "$CAT $location/$logfile"
			$CAT $location/$logfile
		done
	fi
}

#------------------------------ show_last_entry_liveupdate --------------------------------
#
# NAME: show_last_entry_liveupdate
#
# FUNCTION:
#               displays the last log entry at the location specified for liveupdate
#
# EXECUTION ENVIRONMENT:
#
# NOTES:
#
# RECOVERY OPERATION:
#
# DATA STRUCTURES:
#               parameters:
#                       see required and optional args
#               global:
#
# RETURNS: (int)
#
# OUTPUT:
#-------------------------------------------------------------------------------
function show_last_entry_liveupdate {
	logs=`$LS $location`
	if [[ -z $logs ]]; then
		#
		# 0042-218 c_showlog: The log file is either empty or does not exist at
		#      <var/adm/ras/liveupdate/logs>
		#
		${DSPMSG} -s ${ERR_SET} cmdnim.cat ${ERR_LOG_FILE_NOT_FND} \
			'0042-218 %s: The log file is either empty or does not exist at\n\t%s' \
			${PROGRAM_NAME} $location
        else
		for logfile in $logs; do
			echo "############## $logfile ##############"
			echo "$TAIL $location/$logfile"
			$TAIL $location/$logfile
		done
	fi
}

#---------------------------- c_showlog         --------------------------------
#
# NAME: c_showlog
#
# FUNCTION:
#		displays the log at the location specified
#
# EXECUTION ENVIRONMENT:
#
# NOTES:
#
# RECOVERY OPERATION:
#
# DATA STRUCTURES:
#		parameters:
#			see required and optional args
#		global:
#
# RETURNS: (int)
#
# OUTPUT:
#-------------------------------------------------------------------------------

# signal processing
trap cleanup 0
trap err_signal 1 2 11 15

# NIM initialization
nim_init

# initialize local variables
typeset c=""
typeset rc=0

# set parameters from command line
while getopts :a:qv c
do
	case ${c} in

		a) 	# validate the attr ass
			parse_attr_ass "${OPTARG}"

			# include the assignment for use in this environment
			eval ${variable}=\"${value}\"
		;;

		q)	# show attr info
			cmd_what
			exit 0
		;;

		v)	# verbose mode (for debugging)
			verbose=TRUE
			set -x
			for i in $(typeset +f)
			do
				typeset -ft $i
			done
		;;

		\?)	# unknown option
			error ${ERR_BAD_OPT} ${OPTARG}
		;;
	esac
done

# check for missing attrs
ck_attrs

# Make sure a file exists at location
if [ ! -s "${location}" ]
then
	error ${ERR_LOG_FILE_NOT_FND} ${location}
fi

# liveupdate will be set by m_showlog
if [ `basename $location` == "liveupdate"  ]; then
	liveupdate=1
	location=${location}/logs
fi

# if log type is "alog", display contents of file with alog command
if [ "${alog}" = "yes" ]
then
	${ALOG} -of ${location} 2>${ERR} || err_from_cmd ${ALOG} 
	exit 0
fi

if [ "${full_log}" = "yes" ]
then
	if [ $liveupdate ]; then
		show_liveupdate
	else
		# show entire log
		${CAT} ${location} 2>${ERR} || err_from_cmd ${CAT}
	fi
else
	if [ $liveupdate ]; then
		show_last_entry_liveupdate
	else
		# only show most recent entry
		show_last_entry ${location}
	fi
fi	

# Is this a /usr SPOT which has a log of root_syncs in the root part 
# that needs to be displayed?
if [ "${show_usr_spot_root}" = "yes" ]
then

	${C_ERRMSG} ${MSG_SHOW_LOG_ROOT_PART} ${C_ERRMSG_MSG} "" "" "" "" \
		> /tmp/msg$$ 2>&1

	${CAT} /tmp/msg$$

	${RM} /tmp/msg$$

	if [ "${full_log}" = "yes" ]
	then
		${CAT} ${usr_spot_root_loc} 2>${ERR} || err_from_cmd ${CAT}
	else
		show_last_entry ${usr_spot_root_loc}
	fi	
fi

# all done
exit ${rc}
