#
#    Copyright (c) 1996-2000 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    System-wide environment profile for all Bourne- and Korn-
#    compatible login shells.
#

#
# Shell prompt (VxWorks-compatible)
#
#
#
# Ignore control-C when env is initializing for restricted shells
# trap will set back to default in individual user profiles.
# Following three lines should not be moved from this position.
if [ "$LOGNAME" != "root" ]; then
	trap "" INT
fi

max_telnet_sessions=4
if [ "$LOGNAME" = "admin" ]; then
        max_telnet_sessions=2
fi

#Here console port login is excluded from no_sessions
no_sessions=`/usr/bin/who | /bin/grep -v "ttyS"| /bin/grep\
		 "$LOGNAME" | /usr/bin/wc -l` 2>/dev/null
if [ $no_sessions -gt $max_telnet_sessions ]; then
   /bin/echo "Sorry! Max telnet sessions for login:$LOGNAME is $max_telnet_sessions"
   exit
fi


# Standard search path for commands
PATH="/fabos/sbin"
standby=0

haShow > /dev/null
if [ $? -eq 1 ]; then
	standby=1
fi

setup_standby_profile=0

# "root" and "factory" will be allowed all commands in /fabos/bin on standby
# See Defect 9952. Reversing fix for 9952. See Defect 10338.
if [ "$LOGNAME" != "root" -a "$LOGNAME" != "factory" -a  $standby -eq 1 ]; then
	setup_standby_profile=1
fi

if [ -x /etc/fabos/profile ] && [ -r /etc/fabos/profile ]; then
	. /etc/fabos/profile
	if [ $standby -eq 1 ]; then
		echo "*****************************************************************"
		echo "Logging into STANDBY CP, not all commands are fully supported !!"
		echo "*****************************************************************"
	fi
fi

#
# Shell history (VxWorks-compatible)
#
unset HISTFILE
HISTSIZE=20

#
# Telnet IDLE Login Timeout
# Default Timeout is zero(meaning it is disabled)
# The timeout value(in seconds) is stored in /etc/idle.conf
#
TMOUT=`/fabos/sbin/config get system.login.timeout 2`
# if the timeout value is not defined we will make it zero
[ -z $TMOUT ] && TMOUT=0
#
# Other miscellaneous shell aliases
#
alias h='history'		# VxWorks compatibility

alias help='faboshelp'	# VxWorks compatibility

if [ "$LOGNAME" != "root" ]; then
	enable -n kill   #disable access to shell builtin 'kill' for restricted shells
fi

#
# Export the switch instance to the shell
#
export_switch()
{
	case "$1" in
	    0)
		eval `diagenv -b writed SWITCH_ENV 0 0`
		;;
	    1)
		eval `diagenv -b writed SWITCH_ENV 0 1`
		;;
	    *)
		printf "Invalid Input. Logging to Switch 0...\n"
		eval `diagenv -b writed SWITCH_ENV 0 0`
		;;
	esac
}

#
# Set consloe switch number, based on platform
#
set_console_switch()
{
    #
    # For non-ulysses platforms, enter into switch 0.
    # Please make sure it is consistent with
    # /etc/rc.d/init.d/fabos
    #
    PL=`/sbin/sin 2>/dev/null | /bin/grep SWBD | \
	/usr/bin/cut -d ' ' -f 2 | /usr/bin/cut -c 5-6`
    if [ "$PL" == "10" ]; then
	printf "Enter Switch Number to Login <0 or 1>: "
	read choice
	export_switch $choice
    fi
}

#
# Set switch number based on the user input
#
set_switch_inst()
{
    case "$1" in
	# If this is console
	'/dev/console' | '/dev/ttyS0' | '/dev/ttyS1')

	    set_console_switch
	    ;;
	*)
	    eval `/fabos/etc/swInst $PPID`
	    ;;
    esac
}

if [ $setup_standby_profile -eq 0 ]; then
	PATH="/fabos/bin"

	# Do NOT use full path or commands not available to admin
	eval `diagenv ALIAS`

	# Set the switch number
	set_switch_inst `/usr/bin/tty`

else
	PATH="/fabos/standby_bin"
fi

export PATH PS1 HISTSIZE TERM setup_standby_profile

SWNAME=`/fabos/bin/.sname`
PS1="$SWNAME:$LOGNAME> "
alias switchname='source .sn'
alias switchName='source .sn'

# The following two lines ensure all login clients has the same TZ.
source /etc/default/init
export TZ

LESS="-isd"
PAGER="/usr/bin/less"
MANPAGER=$PAGER

export LESS
export PAGER
export MANPAGER

# Make sure the man pages are displayed correctly
export MANWIDTH=80

# Bill Lewis Suppress the set builtin due to defect 8805 12-19-01
enable -n set
