#!/bin/sh
#
#    Copyright (c) 2005-2006 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    File name:   timeout.sh.in 
#    Module name: fabos/src/utils/sys
#     
#    This program can be used to change the IDLE Timeout
#    value for a login session
#
setswitch () 
{ 
    eval `/fabos/cliexec/diagenv -b writed SWITCH_ENV 0 $1`
}

setcontext () 
{ 
    if [ "$1" != "chassis" ]
    then
    	eval `/fabos/cliexec/lscfg_util --switch $1`
    else
    	eval `/fabos/cliexec/lscfg_util --chassis`
    fi
}

export PATH=/fabos/sbin:/fabos/bin:/bin:/usr/bin:/sbin:/fabos/cliexec

#make sure the command only accessible on active CP
#retCode=`hashow | /bin/grep Local | /bin/grep -c Standby`

#if [ $retCode -eq 1 ]; then
#	echo WARNING: Illegal command on Standby!
#	exit
#fi

# let us set our switch to switch 0 -- timeout value always stored in sw0
setswitch 0
#USERNAME=`/usr/bin/whoami`

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

if [ $# -eq 0 ] ; then
	TIMESEC=`config get system.login.timeout 2`
	# if the timeout value is not defined we will make it to 10 minutes
	# as default value -- Defect 12101
	[ -z $TIMESEC ] && TIMESEC=600
	TIMEMIN=`expr $TIMESEC \/ 60`
	echo Current IDLE Timeout is $TIMEMIN minutes
	exit
fi

# with RBAC , we dont need this check below anymore.
# if we are here and role is other than root, admin, factory
# exit with error
#if [ "$USERNAME" != "root" -a "$USERNAME" != "admin" -a "$USERNAME" != "factory" -a "$USERNAME" !=  "switchadmin" ]
#then
#	echo Permission denied for this account
#    exit
#fi

if [ $# -ne 1 ]; then
	echo Invalid Number of arguments
	echo Please just specify the timeout in minutes
	exit
fi

choice=$1
if [ -z "$choice" ]
then
	choice=invalid
fi
nchoice=`echo $choice | /usr/bin/tr -cd "[0-9]\n"`
if [ "$nchoice" != "$choice" ]
then
	printf "Please specify a numeric timeout value in minutes\n"
	exit
fi
if [ $nchoice -gt 99999 ]
then
	printf "Please specify a timeout value in [0-99999]minutes\n"
	exit
fi
TIMESEC=`expr $nchoice \* 60`
if [ $TIMESEC -lt 0 ]
then
	printf "Please specify a timeout value in [0-99999]minutes\n"
	exit
fi
config set system.login.timeout 2 $TIMESEC

config update > /dev/null
echo IDLE Timeout Changed to $nchoice minutes
echo The modified IDLE Timeout will be in effect after NEXT login
