#
#  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 service 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

#TODO : Fix double history issue
#Add serviceShell --activate to command history
if [ $SHLVL -eq 2 ]; then
	export CLI_HISTORY_ADD="SET"
	/bin/echo "serviceshell --activate" | clihistory --add
	export CLI_HISTORY_ADD=""
fi

# 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//\\/\\\\}-service> "
else
        PS1="$SWNAME:${LOGIN_ID//\\/\\\\}-service> "
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

expiry=`/usr/bin/printenv | /bin/grep SERVICESHELL_EXPIRY | /bin/awk -F'=' '{print $2}'`

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

if [ $SHLVL -eq 2 ]; then
	if [ -f /fabos/share/maint_service_disclaimer ]; then
		/bin/cat /fabos/share/maint_service_disclaimer  2> /dev/null
	fi
        export MANSECT="1:1r:1d:1m:8:2:3:4:5:6:7:9:tcl:n:l:p:o"

	/bin/echo ""
	/bin/echo "Service 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
readonly SERVICESHELL_EXPIRY
readonly SERVICESHELL_FLAG

if [ $SHLVL -eq 2 ]; then
	/fabos/libexec/login_notify `/usr/bin/tty` "service"
fi
