#!/bin/sh

# getHaStat function
# we return 100 for active
#           101 for standby
#           102 for unknown state
getHaStat() {
	hastat=102
	retCode=`hashow | /bin/grep Local |  /bin/grep -c Active`
	[ $retCode -eq 1 ] && hastat=100
	retCode=`hashow | /bin/grep Local |  /bin/grep -c Standby`
	[ $retCode -eq 1 ] && hastat=101
}

# main function script starts here
getHaStat

case $hastat in
100)
#  We are Active
	this_active_standby="Active"
	;;
101)
#  We are Standby
	this_active_standby="Standby"
	;;

*)
#  We are Unknown
	this_active_standby="N/A"
	;;

esac

# let us obtain the tty session
this_tty=`/usr/bin/tty`

# let us obtain the Active/Standby status

case $this_tty in
/dev/pts/*)
	ppp_id=`/fabos/bin/expr $PPID - 1`
	this_ip=`/bin/netstat -np | /bin/grep $ppp_id | /usr/bin/tr -s " " : | /usr/bin/cut -d: -f4`
	this_cp_sw=`/bin/grep $this_ip /etc/hosts | /usr/bin/tr -s " \t" : | /usr/bin/cut -d: -f2`
	message1="${this_cp_sw} (${this_ip})"
	;;

/dev/ttyS1)
	message1="Modem Line (/dev/ttyS1)"
	;;

/dev/ttyS0|/dev/console|/dev/con)
	message1="Console Port (/dev/ttyS0)"
	;;

*)
	message1="Unknown (tty returned '$this_tty')"
	;;

esac

#next let us see their switch information
	current_switch=`/fabos/bin/switchshow | /bin/grep switchName | /usr/bin/cut -d: -f2 | /usr/bin/tr -s "[ \t]" " " `

#next let us see if the system is redundant or non redundant
	retcode=`hashow | /bin/grep -ic non-redundant`
	if [ $retcode -eq 1 ]
	then
		this_redundant_non_redundant="Non-Redundant"
	else
		retcode=`hashow | /bin/grep -ic standby`
		if [ $retcode -eq 1 ]
		then
			this_redundant_non_redundant="Redundant"
		else
			this_redundant_non_redundant="HA-Status-N/A"
		fi
	fi


	printf "\
	Current Switch: ${current_switch}
	Session Detail:  ${message1} ${this_active_standby} ${this_redundant} ${this_redundant_non_redundant}
"
