#!/bin/sh
export PATH=/fabos/sbin:/fabos/bin:/bin:/usr/bin:/sbin:/usr/sbin:/fabos/cliexec:/fabos/libexec:
OPTION_SHOW="-show"
export syntax="Usage: appLoginHistory <option>\n options:\n\t--show\n\t--help"

# a small syntax check before RBAC
if [ "$1" != "--show" ]; then
	/bin/echo -e $syntax
	exit 1
fi

# perform RBAC check
if [ $# -eq 0 ]; then
# Check RBAC permission on command
    /fabos/libexec/rbac_check `/bin/basename $0`
elif [ "$1" = "--show" ]; then
    # Check RBAC permission for actual option
    /fabos/libexec/rbac_check -opt `/bin/basename $0` $OPTION_SHOW
else
    # otherwise, just check for OPERAND_PRESENT
    /fabos/libexec/rbac_check `/bin/basename $0` $1
fi 

if [ $? -ne 0 ]; then
    exit 127
fi

PATH=/bin:/usr/bin:/usr/sbin:/fabos/bin:/fabos/link_bin:

#set ROLE to root pass remaining RBAC checks. Don't do it before rbac_check
export ROLE_ID=root
isVFEnabled=0

#
# copied from /fabos/src/setup/profile
#
setcontext()
{
	if [ "$1" != "chassis" ]
    then
		# 261476: in case set_switch_env() return code is -1
        SW_CONTEXT_RC=`lscfg_util --switch $1`
		eval $(lscfg_util --switch $1)
		export FABOS_SWITCHNO
		export CURRENT_VF
	fi
    export ROLE_ID=root
}

if [ -e /fabos/link_sbin/lscfg_test ]
then
    /bin/rm /fabos/link_sbin/lscfg_test
fi

/bin/ln -s /fabos/cliexec/lscfg_util /fabos/link_sbin/lscfg_test 2> tmpfile
export PATH=/fabos/link_bin:/bin:/usr/bin:/sbin:/usr/sbin:/fabos/link_abin:/fabos/link_sbin:/fabos/link_rbin:/fabos/factory:/fabos/xtool

if [ "$(lscfg_test --vf_enabled 2> tmpfile)" = 'VF is enabled.' ]; then
	HOME_VF=128
    setcontext chassis
	export HOME_VF
	isVFEnabled=1
else
    HOME_VF=
    export HOME_VF
fi
rm tmpfile


HAS_LINK=0
if [ -x "/fabos/sbin/webdhelper" ]; then
    HAS_LINK=1
else
    ln -s /fabos/libexec/webdconfigure /fabos/sbin/webdhelper
    HAS_LINK=1
fi

/fabos/sbin/webdhelper webdstore loginHistory 1 > tmpfile

LOGINHISTORY=`cat tmpfile | grep -wE "(SessionIp|User|AgentName|LoginTimeStamp|SessionId|VFId)" |  cut -d " " -f 2`

rm tmpfile

echo ""
echo "The following are history of logins to the switch from external applications"
echo "(Login Timestamp, IP Address, User Account, Application, SessionId, Current VF):"
echo ""

HELLO=1
for var in $LOGINHISTORY; do
if [ $HELLO -eq 6 ]; then
    SIX=$var
	echo $ONE, $FOUR, $FIVE, $SIX, $TWO, $THREE
    HELLO=1
elif [ $HELLO -eq 5 ]; then
    FIVE=$var
    HELLO=6
elif [ $HELLO -eq 4 ]; then
    FOUR=$var
    HELLO=5
elif [ $HELLO -eq 3 ]; then
    THREE=$var
    HELLO=4
elif [ $HELLO -eq 2 ]; then
    TWO=$var
    HELLO=3
else
    ONE=$var
    HELLO=2
fi;
done

/fabos/sbin/webdhelper webdstore printAll 1 > tmpfile
if [ 1 -eq $isVFEnabled ]; then
USER=`cat tmpfile | grep -wE "(SessionIp|User|AgentName|LoginTimeStamp|SessionId|Current_VF)" | cut -d " " -f 2`
else
USER=`cat tmpfile | grep -wE "(SessionIp|User|AgentName|LoginTimeStamp|SessionId|Current_AD)" | cut -d " " -f 2`
fi
rm tmpfile

echo ""
echo "The following are the sessions from the external applications that are"
echo "active currently:"
echo ""

HELLO=1
for var in $USER; do
if [ $HELLO -eq 6 ]; then
	SIX=$var
	echo  $SIX, $THREE, $TWO, $FIVE, $ONE, $FOUR 
	HELLO=1
elif [ $HELLO -eq 5 ]; then
    FIVE=$var
    HELLO=6
elif [ $HELLO -eq 4 ]; then
    FOUR=$var
    HELLO=5
elif [ $HELLO -eq 3 ]; then
	THREE=$var
	HELLO=4
elif [ $HELLO -eq 2 ]; then 
	TWO=$var
	HELLO=3
else 
	ONE=$var
	HELLO=2
fi;
done

echo ""

if [ 1  -eq $HAS_LINK ]; then
	rm /fabos/sbin/webdhelper
fi
