#!/bin/sh
#
#
#    Copyright (c) 1996-2006 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#
# NAME
#      setgbicmode - set or display GBIC mode
# 
# SYNOPSIS
#      setgbicmode [ mode | -show ]
#      setsfpmode [ mode | -show ]
#      setmediamode [ mode | -show ]
# 
# AVAILABILITY
#      admin
# 
# DESCRIPTION
#      This command enables GBIC mode if mode value is non-zero and
#      disables the GBIC mode if mode value is 0. The mode is saved
#      in flash memory and stays in that mode until the next execu-
#      tion of setgbicmode. The mode becomes active as soon as this
#      command  is  executed.  It does not require a reboot to take
#      effect.
# 
#      GBIC mode when enabled modifies the behavior of the diagnos-
#      tic  test methods so that ports without GBIC's installed are
#      not tested.  Normally tests such as crossporttest  or  spin-
#      silk  will  fail  if any port is not operating properly, but
#      with GBIC mode enabled the functional tests will be  skipped
#      on ports that do not contain GBIC's.
# 
# OPTIONS
#      mode      Specify  the  GBIC  mode value. 0 means to disable
#                GBIC mode, any other value will enable GBIC  mode.
# 
#      -show     If specified or no mode is given, the current GBIC
#                mode will be displayed.
# 
# EXAMPLES
#      > setgbicmode -show
#      Gbic Mode is 0 (Disabled).
# 
# SEE ALSO
#      crossporttest(1d), spinsilk(1d)
# 
# NOTES
#      With this mode enabled, it is not possible for the  diagnos-
#      tic  software  to  distinguish  between a missing GBIC and a
#      faulty GBIC that only appears to be missing.
# 

#
#
# 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
#

progname=`/bin/basename $0 | /usr/bin/tr "A-Z" "a-z"`
if [ "$progname" = "setgbicmode" ]; then
	config_name="Gbic Mode"		# User name of config.
elif [ "$progname" = "setsfpmode" ]; then 
	config_name="Sfp Mode"
elif [ "$progname" = "setmediamode" ]; then 
	config_name="Media Mode"
fi
config_string="diag.mode.gbic"	# config string to update
config_default=0		# default value
config_mode=$INTEGER		# config mode

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

#
# setgbicmode()
#
checkForShow "$config_name" "$config_string" "$config_mode" "$config_default" $1
val=`getValue $1`
if [ $? != 0 ] ; then exit 3 ; fi

if [ "$val" -ne "0" ]
then
	val=1
fi

if [ "$val" -ne "0" ]
then
     res=$bool_true
else
     res=$bool_false
fi

cur_val=`getConfig $config_string $config_mode $config_default`
if [ "$cur_val" != "$val" ]
then
	setConfig $config_string $config_mode $val
	updateConfig
	echo "$config_name is now $val ($res)."
else
	echo "$config_name is still $val ($res)."
fi
exit 0
