#!/bin/bash
PATH=/bin:/usr/bin:$PATH

# getHaStat function
# we return 100 for active
#           101 for standby
#           102 for unknown state


getHaStat()
{
    hastat=102
    this_active_standby="N/A"	
    typeset -a lines
	# Break the HASHOW into lines
    IFS=$'\n' lines=($HASHOW)
    for l in "${lines[@]}"
      do
      case $l in
	  (*Local*Active*) hastat=100; this_active_standby="Active";;
	  (*Local*Standby*) hastat=101; this_active_standby="Standby";;
      esac
    done
}

getttystatus()
{
# let us obtain the tty session
    local this_tty=$(/usr/bin/tty)

# let us obtain the Active/Standby status

    case $this_tty in
	(/dev/pts/*)
	local ip=$LOCAL_IP
	case "$LOCAL_IP" in
	    (::[Ff][Ff][Ff][Ff]:*[^0-9.]*) ;; # Handle a very rare cornercase
	    (::[Ff][Ff][Ff][Ff]:**) ip=${LOCAL_IP#::[Ff][Ff][Ff][Ff]:};;
	esac
	local ss='s/#.*//;/^[\t ]*'"$ip"'[\t]*[ ]*\([^ ]*\).*/{s//\1/p;q;}'

	#/bin/echo "$ss"
	
	this_cp_sw=$(/bin/sed -ne "$ss" /etc/hosts)
	message1="${this_cp_sw} (${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
    printf "%s " "$message1"
}



getredundantstatus()
{
#next let us see if the system is redundant or non redundant
    case "${HASHOW}" in
	(*[Nn][Oo][Nn]-[Rr][Ee][Dd][Uu][Nn][Dd][Aa][Nn][Tt]*)
	this_redundant_non_redundant="Non-Redundant"
	;;
	(*[Ss][Tt][Aa][Nn][Dd][Bb][Yy]*)
	this_redundant_non_redundant="Redundant"
	;;
	(*)
	this_redundant_non_redundant="HA-Status-N/A"
	;;
    esac
}

main()
{
	printf "\n\tCurrent Switch:  "
	/fabos/cliexec/.sname
	HASHOW=$(hashow)
	printf "\tSession Detail:  "
	getttystatus
	getredundantstatus
	getHaStat
	printf "%s\n" "${this_active_standby} ${this_redundant} ${this_redundant_non_redundant}"
}

main
