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

# SSH Banner display related
if  [ -f /etc/fabos/issue.$FABOS_SWITCHNO ] && [ -r /etc/fabos/issue.$FABOS_SWITCHNO ]; then
	/bin/cat /etc/fabos/issue.$FABOS_SWITCHNO
	echo ""
	echo "-----------------------------------------------------------------"
fi

# $SWLOGNAME is the login account name $LOGNAME stripped of switch
# instance number. It replaces the usage of LOGNAME in the old
# script to keep it compatible with account name per switch instance.

# The following condition takes care of login without PAM module.
if [ "$SWLOGNAME" = "" ]; then
	SWLOGNAME="$LOGNAME"
fi

#
# 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.
#
# Disable interrupts for root profile also (defect 25763).
#if [ "$SWLOGNAME" != "root" ]; then
	trap "" INT
#fi

#
# Check number of successive logins in same session
#
check_login_count()
{
	login_count=0
	parent=""
	P=$PPID
	while true ; do
		item_count=0
		for item in `</proc/$P/stat` ; do
			item_count=`expr $item_count + 1`
			if [ $item_count = 2 ] ; then
        			cmd=$item
        		fi
			if [ $item_count = 4 ] ; then
        			parent=$item
        			break
        		fi
		done
		P=$parent
		if [ "$cmd" = "(login)" ] ; then
        		login_count=`expr $login_count + 1`
       		fi
		if [ $parent = 1 -o $parent = 0 ] ; then
        		break
        	fi
	done
	/bin/echo $login_count
}

max_telnet_sessions=4
max_total_sessions=32
max_same_session_logins=5

num_logins=`check_login_count`
if [ $num_logins -gt `expr $max_same_session_logins` ] ; then
        /bin/echo "Sorry! Same session logins exceeded. Autologout"
        /bin/sleep 2
        exit 2 >/dev/null
        fi
if [ $num_logins -gt `expr $max_same_session_logins - 1` ] ; then
        echo "Same session login limit reached. Logout before next login"
        fi

rolename=`/usr/bin/id -gn`

if [ "$rolename" = "nonfcsadmin" ]; then
	/fabos/libexec/secRoleCheck $FABOS_SWITCHNO $rolename
	if [ $? -ne 0 ]; then
		exit 2>/dev/null
	fi
fi

if [ "$rolename" = "admin" ] || [ "$rolename" = "nonfcsadmin" ]; then
        max_telnet_sessions=2
fi

#Here accounts logged in using switch authentication type are considered
if [ "$AUTH_TYPE" = "0" ]; then
	#Here console port login is excluded from no_sessions
	no_sessions=`/usr/bin/who | /bin/grep -v "ttyS"| /bin/grep -w\
			 "$LOGNAME" | /usr/bin/wc -l` 2>/dev/null
	if [ $no_sessions -gt $max_telnet_sessions ]; then
		/bin/echo "Sorry! Max remote sessions for login:$SWLOGNAME is $max_telnet_sessions"
		exit 2>/dev/null
	fi
fi

total_sessions=`/usr/bin/who | /bin/grep -v "ttyS"| /usr/bin/wc -l` 2>/dev/null
if [ $total_sessions -gt $max_total_sessions ]; then
	/bin/echo "Sorry! Max remote sessions for all accounts is $max_total_sessions"
   exit 2>/dev/null
fi

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

if [ -x /fabos/cliexec/hashow ]; then
	/fabos/cliexec/hashow | grep Local | grep Standby > /dev/null 2>&1
	if [ $? -eq 0 ]; then
		standby=1
	fi
fi

# if passwd is aged and if we are on active CP, ask for a new passwd.
if [ $passwd_aged ]; then 
	if [ $passwd_aged -eq 1 -o $passwd_aged -eq 2 ]; then
		trap "" INT
		if [ $standby -eq 0 ]; then
			/bin/echo "Your password has expired. Please change your password now."
			/fabos/bin/passwd
			if [ $? -ne 0 ]; then
				exit 2>/dev/null
			else
				unset passwd_aged
			fi
		else
			/bin/echo "Your password has expired. Please login to the ACTIVE CP and change your password now."
			exit 2>/dev/null
		fi
	fi
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 [ "$SWLOGNAME" != "root" -a "$SWLOGNAME" != "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

# get rid of duplicate history, subject to bash version
export HISTIGNORE='&'

#
# Telnet IDLE Login Timeout
# Default Timeout is 10 minutes
# The timeout value(in seconds) is stored in system.login.timeout
# in config database in switch 0
# Must get timeout value from sw0 --- Defect 25800
#
switchno=$FABOS_SWITCHNO
FABOS_SWITCHNO=0

if [ -x /fabos/cliexec/config ]; then
	TMOUT=`/fabos/cliexec/config get system.login.timeout 2`
fi
# if the timeout value is not defined we will make it to 10 minutes
[ -z "$TMOUT" ] && TMOUT=600

FABOS_SWITCHNO=$switchno; export FABOS_SWITCHNO

#
# Other miscellaneous shell aliases
#
alias h='history'		# VxWorks compatibility

alias help='faboshelp'	# VxWorks compatibility

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

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

#
# 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
    #
	if [ -x /bin/getchassisconfig ]; then
    	CHASINFO=`/bin/getchassisconfig`
    	ISCHASSIS=`echo $CHASINFO | \
		/bin/sed -n -e 's/.*Chassis based system: //gp'	| \
		/bin/sed -n -e 's/ .*//gp'`
    	NUM_SWITCHES=`echo $CHASINFO | \
		/bin/sed -n -e 's/Number of switches: //gp' | \
		/bin/sed -n -e 's/ .*//gp'`
    	if [ "$NUM_SWITCHES" == "2" ]; then
			printf "Enter Switch Number to Login <0 or 1>: "
			read choice
			export_switch $choice
    	fi
	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
	    ;;
	*)
		[ -x /fabos/etc/swInst ] && eval `/fabos/etc/swInst $PPID`
	    ;;
    esac
}

#
# Init switch number based on environment variable $FABOS_SWITCHNO
#
init_switch_inst()
{
	case "$1" in
	#if this is console
	'/dev/consol' | '/dev/ttyS0' | '/dev/ttyS1')

		if [ -x /bin/getchassisconfig ]; then
			CHASINFO=`/bin/getchassisconfig`
			ISCHASSIS=`echo $CHASINFO | \
				/bin/sed -n -e 's/.*Chassis based system: //gp' | \
				/bin/sed -n -e 's/ .*//gp'`
			NUM_SWITCHES=`echo $CHASINFO | \
				/bin/sed -n -e 's/Number of switches: //gp' | \
				/bin/sed -n -e 's/ .*//gp'`
			if [ "$NUM_SWITCHES" == "2" ]; then
				export_switch $FABOS_SWITCHNO
			fi
		fi
		;;
	*)
		[ -x /fabos/etc/swInst ] && eval `/fabos/etc/swInst -s $FABOS_SWITCHNO`
		;;
	esac
}

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

	# Do NOT use full path or commands not available to admin
	[ -x /fabos/cliexec/diagenv ] && eval `/fabos/cliexec/diagenv ALIAS`

	# only do this if PAM module failed to set it
	if [ "$FABOS_SWITCHNO" = "" ]; then
		# Set the switch number, this is used for non-PAM login.
		set_switch_inst `/usr/bin/tty`
	else
		init_switch_inst `/usr/bin/tty`
	fi

else
	PATH="/fabos/standby_bin"
fi

# Set terminal type to "vt100" (supported by all platforms)
TERMINFO=/usr/share/terminfo
TERM="vt100"

export PATH PS1 HISTSIZE TERM setup_standby_profile TERMINFO

SWNAME=`/fabos/cliexec/.sname 2>/dev/null`
#PS1="$HOSTNAME:LC:${LOGIN_ID//\\\\/\\\\}> "
PS1="$HOSTNAME:` /fabos/link_sbin/hashow | /usr/bin/cut  -c 1-5 ` >"
# replace \ with \\ in $LOGIN_ID to prevent side-effects of \ in $LOGIN_ID
#if [ "$VF_ENABLED" == 1 ]
#then 
#	PS1="$SWNAME:FID$CURRENT_VF:${LOGIN_ID//\\\\/\\\\}> "
#else
#	if [ "$CURRENT_AD" != 0 ]
#	then
#		PS1="$SWNAME:AD$CURRENT_AD:${LOGIN_ID//\\\\/\\\\}> "
#	else
#		PS1="$SWNAME:${LOGIN_ID//\\\\/\\\\}> "
#	fi
#fi

# Skip the login_notify for ad --select 
argv0=$0
var=`echo ${argv0:0:1}`
if [ "$var" == "-" ]
then
	[ -x /fabos/libexec/login_notify ] && /fabos/libexec/login_notify `/usr/bin/tty`
fi

[ -r /fabos/sbin/tsSetEnvTZ ] && source /fabos/sbin/tsSetEnvTZ

# tsTimeZone command sources tsTZ script to set TZ variable in the
# current session. Note that these aliases override commands of the
# same name, so that we can still use "ssh -n switch tsTimeZone Europe/London"
# to set the timezone on the switch.
tsTimeZone() {
	if [ -x /fabos/link_bin/tsTimeZone ]
	then
		command tsTimeZone "$@"
		eval $(tsTZ)
	else
		echo "$0: command not found"
		return 1
	fi
}

alias tstimezone=tsTimeZone



LESS="-isdePType <CR> or <SPACE BAR> to continue, <q> to stop"
PAGER="/usr/bin/less"
MANPAGER=$PAGER

export LESS
export PAGER
export MANPAGER
export LINES 

# 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

# Make sure execlogin is available as builtin for all the users 
[ -x /fabos/lib/execlogin ] && enable -f /fabos/lib/execlogin execlogin 

# for noN-root users, make RBAC/AD env variables readonly. def. 74490
if [ "$SWLOGNAME" != "root" ]; then
	readonly LOGIN_ID
	readonly CURRENT_AD
	readonly HOME_AD
	readonly ADMAP
	readonly HOME_VF
	readonly VFROLEMAP
	enable -f /fabos/lib/cmdcomp cmdcomp
	cmdcomp
fi

setcontext() 
{ 
	if result=$(lscfg_util --switch $1) ; then
		eval "$result"
		[ -x /fabos/lib/execlogin ] && execlogin
	fi
}
