#
#  Brocade Proprietary and Confidential.  Copyright © 2021 Brocade
#  Communications Systems LLC.  All Rights Reserved.
#
#  BROCADE HIGHLY CONFIDENTIAL TRADE SECRET INFORMATION
#
#  Access or possession of this material grants you no right or license,
#  express, implied, statutory or otherwise, under any Brocade patent,
#  copyright, trade secret right or other intellectual property right.
#  Any such license must be contained in a written license agreement
#  signed by an authorized officer of Brocade.  Additionally, access or
#  possession of this material is limited to Brocade employees with a
#  need to know or named employees of Brocade vendors authorized by
#  Brocade in writing to access this material.
#
#  Restricted Rights: Use, duplication, or disclosure by the U.S.
#  Government is subject to restrictions of FAR 52.227-19 and its
#  successors, or (c)(1)(ii) of the Rights in Technical Data and
#  Computer Software clause at DFAR 252.227-7013 and its successors.
#
#  Profile file for factory shell of maintenance user
#

trap "" INT

# set umask
umask 027

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

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

#Add factorycfg --enable to command history
export CLI_HISTORY_ADD="SET"
/bin/echo "factorycfg --enable" | clihistory --add
export CLI_HISTORY_ADD=""

# Other miscellaneous shell aliases
alias h='history'
alias ls='ls -CAF'
alias reboot=/fabos/link_sbin/reboot
enable -n help
enable -n set
enable -f /fabos/lib/setcontext setcontext


SWNAME=`/fabos/cliexec/.sname 2>/dev/null`
if [ "$VF_ENABLED" == 1 ]
then
        PS1="$SWNAME:FID$CURRENT_VF:${LOGIN_ID//\\/\\\\}-factory> "
else
        PS1="$SWNAME:${LOGIN_ID//\\/\\\\}-factory> "
fi


source /fabos/cliexec/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() {
    command "tsTimeZone" "$@"
    return $?
}

alias tstimezone=tsTimeZone

#invoke savehistory() everytime the user prompt returns
PROMPT_COMMAND=savehistory

# savehistory is used to pipe every CLI to clihistory so that we are saving
# a history of what the user is typing in.
savehistory() {
        local newcmd
        newcmd=$(history 1)
        local hdel
        hdel=$(CLI_HISTORY_ADD=SET PROMPT_COMMAND_EXECUTION=SET PROMPT_COMMAND_OLD="${oldcmd#*[0-9]*  }" PROMPT_COMMAND_NEW="${newcmd#*[0-9]*  }" CMD_HISTORY_1=$newcmd clihistory --add)
        $hdel
        oldcmd=$newcmd
}

savehistoryonreboot() {
                export CLI_HISTORY_ADD="SET"
                echo "$1 $2 $3" | clihistory --add
                export CLI_HISTORY_ADD=""
}
declare -x -f savehistoryonreboot

if [ ! -f /var/log/factoryauth_file ]; then
	exit 1
fi

expiry=`/bin/cat /var/log/factoryauth_file | /bin/grep FactoryShellSessionTime | /bin/awk '{print $2}'`

if [ $expiry == "" ]; then
	exit 1
fi

if [ $SHLVL -eq 2 ]; then
	if [ -f /fabos/share/maint_factory_disclaimer ]; then
		/bin/cat /fabos/share/maint_factory_disclaimer  2> /dev/null
	fi

	/bin/echo ""
	/bin/echo "Factory session will expire at `/bin/date -d @$expiry`"
	/bin/echo ""
fi

# Set the control-C handler back to default
trap - INT
/bin/stty erase '^H'

readonly EXECIGNORE
/fabos/libexec/login_notify `/usr/bin/tty` "factory"
