#!/bin/sh
# $Copyright$
# Copyright (C) Fujitsu Technology Solutions 2009
# All rights reserved
#
# $Copyright$
# Copyright (C) Fujitsu Siemens Computers GmbH 2008, 2009
# All rights reserved
#
# $Id: irmcview.sh,v 1.2 2008-04-23 15:21:50+02 richter Exp richter $
#
# NAME
#   irmcview - View integrated Remote Management Controller (iRMC)
#
# SYNOPSIS
#   irmcview [options]
#
# DESCRIPTION
#   irmcview is a utility for displaying information available across
#   the IPMI from the PRIMERGY iRMC (integrated Remote Management Con-
#   troller). It concentrates specifically on the OEM specific IPMI
#   functions implemented in the iRMC.
#
# SEE ALSO
#	IMPI (Intelligent Platform Management Interface) Specification
#
# EXIT STATUS
#	0 OK
#	1 Runtime Error
#	2 Argument Error
#
# CHANGE HISTORY
#	April 2008, -rit
#		Initial implementation.

# ----------------------------------------------------------------------
# Initialization
# ----------------------------------------------------------------------
# Check if ipmiview_cmd is in the current directory. Usually if
# running fron a shar archive

ipmiView="/etc/srvmagt/ipmiview_cmd"
[ -e ipmiview_cmd ] && ipmiView=ipmiview_cmd

MYSELF="${0##*/}"
MYVERS="V4.70"

status=0
language=

# Internationalized Messages

BANNER="
iRMC IPMI Information Viewer
(c) Fujitsu Technology Solutions 2009

$(LC_ALL=C date +'%F %T %Z') $HOSTNAME $MYVERS $MYSELF $* "

USAGE="#### Usage
$MYSELF [-ACDEFPS] [-cdehilorstx] [-f value] [-r variable]
	-h	prints this output
	Mutually exclusive options:
	-A 	combines CDEFS
	-C	displays Chassis Status
	-D	displays Device-Id
	-E	displays System Event Log
	-F	displays FRUs
	-P	displays processor and memory information
	-S	displays SDRs
	-e	displays EEPROM information
	-f selector	set FW selector
	-l	list all supported IPMI netfunc + cmd combinations
	-r variable	display configuration space variable
	-s  	execute a single IPMI command

	Modifier options for mutually exclusive options:
	-c	set class of debug : 1 = stderr, 2 = file
	-d	set level of debug : 1 = basic, 2 = internal errors, 3 = details, 4 = full
	-i	display IPMI command classes and commands during processing
	-o	show full OEM details on SDRs
	-t	write trace into a trace file - in conjunction with -c 2 
	-x	additional hexadecimal output in FRUs and SDRs
"

MustBeBash="You must use bash to execute this script. Exit!\n"
MustBeRoot="%s: You must be root to execute this program. Exit!\n"
InvalidOpt="Invalid additional option '%s'!\n"
UndefArgument="Undefined argument '%s'!\n"

# ----------------------------------------------------------------------
# Main
# ----------------------------------------------------------------------

PATH="/bin:/sbin:/usr/bin:/usr/sbin"
echo "$BANNER"

[ -n "${BASH}" ] || {
	printf "$MustBeBash"
	exit 1
}

[ $(id -un) = "root" ] || {
	printf "$MustBeRoot" $MYSELF
	exit 1
}

# Need at least ONE option

[ $# -eq 0 ] && {
	printf "$USAGE"
	exit 0
}

# Save arguments to pass them to processor

argc=$#
argv=$@
numopts=0

while getopts ACDEFPSbc:d:ef:hiln:or:st:x optChar; do
	case "$optChar" in
	A|C|D|E|F|P|S|e|l|r|s)
		# mutual exclusive options
		let numopts++
		[ $numopts -gt 1 ] && {
			printf "$InvalidOpt" $optChar
			printf "$USAGE"
			exit 2
		} ;;
	c|d|f|i|o|t|x)
		# Modifying options
		;;
	h|[?])
		printf "$USAGE"
		[ "$1" = "-h" ] && exit 0
		exit 2 ;;
	esac
done

# No additional args defined

shift $(($OPTIND - 1))
[ -z "$1" ] || {
	printf "$UndefArgument" "$1"
	printf "$USAGE"
	exit 2
}

# Call processing utility

$ipmiView $argv
status=$?

# ----------------------------------------------------------------------
# Done
# ----------------------------------------------------------------------

exit $status

# EOF
