#!/bin/sh
#
#
#    Copyright (c) 1996-2006 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#
# NAME
#      setmfgmode - set or display diagnostic MFG mode
# 
# SYNOPSIS
#      setmfgmode [ mode | -show ] -slot 
# 
# AVAILABILITY
#      admin
# 
# DESCRIPTION
#      This  command enables MFG mode if mode value is non-zero and
#      disables the MFG mode if mode value is 0. The mode is  saved
#      in flash memory and stays in that mode until the next execu-
#      tion of setmfgmode. The mode becomes active as soon as  this
#      command  is  executed.  It does not require a reboot to take
#      effect.
# 
#      MFG mode when enabled modifies the behavior of the  diagnos-
#      tic  test  methods and POST(Power-On Self-Test) scripts. The
#      exact behavior varies but most commonly consists of enabling
#      extra manufacturing specific tests and data patterns.
# 
# OPTIONS
#      mode      Specify the MFG mode value. 0 means to disable MFG
#                mode, any other value will enable MFG mode.
# 
#      -show     If specified or no mode is given, the current  MFG
#                mode will be displayed.
# 
# EXAMPLES
#      > setmfgmode -show
#      Mfg Mode is 0 (Disabled).
# 

#
#
# Load library -- must be first.
#


home="/fabos/share"
util="diagcommon.sh"
ok=0
file="/tmp/DIAGS"

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
  err "Could not locate $util"
  exit 3
fi

_mslot=0
if [ "`supportsQuiescent`" = TRUE ]
then
    if [ $# -eq 0 ]
    then
        _val1=1
        _val2=TRUE
        _mslot=0
    else
        if [ "$2" = "-slot" ]
        then
            _mslot=$3
        fi
        if [ $1 -eq 0 ]
        then
            _val1=0
            _val2=FALSE
        else
            _val1=1
            _val2=TRUE
        fi
    fi
    /fabos/link_sbin/burninlevel $_val1
    for _slot in `getAvailSlots -chassisnofault`
    do
        if [ $_mslot -ne 0 ]
        then
            if [ $_mslot -ne $_slot ]
            then
                continue
            fi
        fi
        if [ "$_val2" = "TRUE" ]
        then
            if [ ! -e $file.$_slot ]
            then
                do_sw_quiescent $_slot $_val2
                /bin/touch $file.$_slot
            else
                /bin/echo "DIAG_START is still active on slot $_slot"
            fi
        else
            if [ -e $file.$_slot ]
            then
                do_sw_quiescent $_slot $_val2
                /bin/rm $file.$_slot
            else 
                /bin/echo "DIAG_DONE is still active on slot $_slot"
            fi
        fi
    done
    exit 0
else
    echo "Command not applicable to this platform."
    exit 3
fi

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.mode.mfg"	# config string to update
config_name="Mfg Mode"		# User name of config.
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

#
# setmfgmode()
#
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
