#!/bin/sh
#
###########################################################################
#  Copyright 2006 Hewlett-Packard Development Company, L.P.               #
###########################################################################
#
# Call the pClass firmware updater after doing some checking of the local 
# environment to make sure we're in a correct environment and a firmware 
# update is appropriate.  We also will start the cpqriisd in order to access 
# the rack infrastructure correctly and we'll leave it running if this is in 
# fact a pClass rack and it starts correctly.
#
# RESULT CODES returned to the menu system
#
# 0 - The operation failed.  This is either a failure in your utility or in 
#     the flash engine or operation.
# 2 - The flash operation was successful
# 3 - Either the local server is not in a p-class Enclosure or the version of
#     the firmware is already up-to-date or newer than the version provided 
#     in the cpqrmmxxx.bin file.

SCRIPT_HOME=`dirname $0`
SCRIPT_NAME=`basename $0`

FIRMWARE_UPDATER=$SCRIPT_HOME/cpqblru
CPQRIISD=cpqriisd

OP_FAILED=0
OP_OK=2
OP_CONFIG_ERROR=3

#
# Wait for the user to press enter before continuing.  This is to allow 
# them time to read the screen for messages as the window will close if
# this script is called from the firmware update menu when this script 
# is exited.
#
function pause() {
	echo -n "Press "\"Enter\"" to continue : "
	read
}

#
# Check to see if the rack infrastructuer daemon is running.  If not we'll
# try and start it.  It won't run if this isn't a pClass blade we're executing on.
#

ps -e | grep "$CPQRIISD" >/dev/null 2>&1
if [ $? != 0 ]; then

	$CPQRIISD &

	ps -e | grep "$CPQRIISD" >/dev/null 2>&1
	if [ $? != 0 ]; then
		echo 
        	echo "Unable to start the Rack Infrastructure Daemon."
		echo
		pause
		exit $OP_CONFIG_ERROR
	fi
fi

#
# Figure out firmware version on the CD
#
# Look in the SCRIPT_HOME directory for a firmware image file that matches
# the pattern 'cpqrmm???.bin' where the '???' is a 3 digit numeric version
# number. 
#

FIRMWARE_IMAGE=`basename $SCRIPT_HOME/cpqrmm???.bin`

if [ ! -e $FIRMWARE_IMAGE ]; then
    echo "No pClass firmware image found.  Cannot proceed."
    echo 
    pause
    exit $OP_CONFIG
else
    echo Using pClass Enclosure firmware image : $FIRMWARE_IMAGE
fi
 
# 
# Check to make sure we're executing on a pClass enclosure and that the firmware
# versions in place are at older than the version we have available.
# Run CPQBLRU in query mode.  If this fails there's a config problem
# and we can't get the rack infrastructure information.  Else we save
# the query output to fetch the current firmware version for comparison
#

CPQBLRU_QUERY=/tmp/cpqblru.$$
$FIRMWARE_UPDATER -q >$CPQBLRU_QUERY

echo
echo Querying for pClass hardware configuration
echo
cat $CPQBLRU_QUERY

grep "Server Enclosure" $CPQBLRU_QUERY >/dev/null 2>&1
if [ $? != 0 ]; then
	echo 
	echo "Configuration error.  Invalid pClass enclosure configuration"
	echo
	pause
	exit $OP_CONFIG_ERROR
else
	echo
	echo Valid pClass enclosure configuration found
	echo
fi

#
# Configuration is correct.  Go ahead and do the flash operation
# The update tool will prompt if there is no change to the revision
# or if this will attempt to down-rev the image
#

$FIRMWARE_UPDATER -a $SCRIPT_HOME/$FIRMWARE_IMAGE

if [ $? != 0 ]; then
    RESULT_CODE=$OP_FAILED
else
    RESULT_CODE=$OP_OK
fi

pause

exit $RESULT_CODE
