#!/bin/sh
#
#
#    Copyright (c) 1996-2006 Brocade Communications Systems, Inc.
#    All rights reserved.
#
# NAME
#      diagretry - set or display diagnostic retry mode
# 
# SYNOPSIS
#      diagretry [ mode | -show ]
# 
# AVAILABILITY
#      admin
# 
# DESCRIPTION
#      This  command  enables  retry mode if mode value is non-zero
#      and disables the retry mode if mode value is 0. The mode  is
#      saved  in flash memory and stays in that mode until the next
#      execution of diagretry. The mode becomes active as  soon  as
#      this  command  is  executed. It does not require a reboot to
#      take effect.
# 
#      Retry mode when enabled modifies the behavior of  the  diag-
#      nostic  test  methods,  POST(Power-On Self-Test), and burnin
#      scripts.  The  exact  behavior  depends  on  the  tests  and
#      scripts that are run.
# 
# OPTIONS
#      mode      Specify  diagnostic retry mode value. 0 means dis-
#                able retry mode, any other value will enable retry
#                mode.
# 
#      -show     If  specified  or  no  mode  is given, the current
#                retry mode will be displayed.
# 
# EXAMPLES
#      > diagretry -show
#      Diagnostic Retry Mode is currently disabled.
# 

#
# Load library -- must be first.
#
home="/fabos/share"
util="diagcommon.sh"
ok=0

if [ $# -eq 0 ]; then
	# Check RBAC permission on command
	/fabos/libexec/rbac_check `/bin/basename $0`
else
	# Check RBAC permission on identified options
	/fabos/libexec/rbac_check `/bin/basename $0` $1
fi

if [ $? -ne 0 ]; then
exit 127
fi

for f in "./$util" "$FABOSHOME/share/$util" "$home/$util" ; do
	if [ -r $f ] ; then
		. $f
		ok=1
		break;
	fi
done
if [ $ok -ne 1 ] ; then
	echo "Error -- could not locate $util"
	exit 3
fi

#
# Program customization
#

config_string="diag.retryDisable"  # config string to update
config_default=0		   # default value
config_mode=$INTEGER		   # config mode

syntax="`/bin/basename $0` [ mode \| -show ]" ; export syntax

show_mode=`/usr/bin/expr "$1" : "[-]*show"`
if { [ -z "$1" ] || [ "0" -ne "$show_mode" ] ; } ; then

	#
	# without option or -show option
	#
	current_value=`getConfig $config_string $config_mode $config_default`
	if [ $? != 0 ] ; then exit 3 ; fi

	if [ "$current_value" -eq "0" ] ; then
		echo "Diagnostic Retry Mode is currently enabled."
	else
		echo "Diagnostic Retry Mode is currently disabled."
	fi
else

	#
	# specified with any value except "-show"
	#
	value=`getValue $1`

	if [ $? != 0 ] ; then exit 3 ; fi

	#
	# (Caution)
	# use "set_value" in comparison
	# if the value is not 0, it means "Enable Retry Mode", in other words
	#     "Set diag.retryDisable to 0" : set_value = 0
	# if the value is 0, it means "Disable Retry Mode", in other words
	#     "Set diag.retryDisable to 1" : set_value = 1
	#
	if [ "$value" -eq  "0" ] ; then
		set_value=1
	else
		set_value=0
	fi

	current_value=`getConfig $config_string $config_mode $config_default`
	if [ "$current_value" != "$set_value" ]
	then
		setConfig $config_string $config_mode $set_value
		updateConfig
		if [ $? != 0 ] ; then exit 3 ; fi

		if [ "$set_value" -eq "0" ] ; then
			echo "Diagnostic Retry Mode is now enabled."
		else
			echo "Diagnostic Retry Mode is now disabled."
		fi
	else
		if [ "$set_value" -eq "0" ] ; then
			echo "Diagnostic Retry Mode is enabled. (Unchanged)"
		else
			echo "Diagnostic Retry Mode is disabled.(Unchanged)"
		fi
	fi
fi

exit 0
