#!/bin/bash
#
# Emulate the ifModeShow command on the 4.0 Linux-based switches.
#

SWBD=`/sbin/sin | /bin/sed -n -e 's/^.\+\(SWBD\)\([[:digit:]]\{1,\}\).\+$/\2/gp' 2> /dev/null`

argc=$#
argv=("$@")
support=0
for ((i=0;i<$argc;i++))
do
        opt=${argv[$i]}
        if [ "$opt" == "-support" ]; then
                support=1
        fi
done

if [ $support -eq 0 ] ; then
        echo "This command is deprecated in Fabric OS v8.0.1. Please use command ethif."
        exit 1
fi

IFACE=$1
if [ "$IFACE" = "" ]; then
        echo "Usage:     ifModeShow <interface>"
        echo "Example:   ifModeShow \"eth0\""
        exit 1
fi

# bonding enable only on zentron
if [ $SWBD = 62 -o $SWBD = 77 -o $SWBD = 141 -o $SWBD = 142 -o $SWBD = 166 -o $SWBD = 165 ]; then
	if [ "$IFACE" != "eth0" ] && [ "$IFACE" != "eth3" ] \
		&& [ "$IFACE" != "bond0" ]; then
        echo "ifModeShow can only be used with eth0, eth3 or bond0."
        exit 1
	fi
else
	if [ "$IFACE" != "eth0" ]; then
		echo "ifModeShow can only be used with eth0."
		exit 1
	fi
fi

if [ "$IFACE" = "bond0" ]; then
	/bin/cat /proc/net/bonding/bond0 | /bin/grep "Currently Active Slave"
	exit 0
fi

# populate $1,$2,$3,$4,$5,$6 with output of ethmode
set -- `/fabos/libexec/ethmode "$IFACE"`

# assign some more readable variable names
AUTONEG=$2
LINK=$5
CURRENT=$6

# strings reported to user in forced mode
FORCED_HALF="half duplex"
FORCED_FULL="full duplex"
FORCED_10="10 Mbit"
FORCED_100="100 Mbit"
FORCED_1000="1000 Mbit"

if [ "$LINK" -eq 0 ]; then
	LINK_STRING="no link"
else
	LINK_STRING="link ok"
fi

if [ "$AUTONEG" -eq 0 ]; then
	if [ "$CURRENT" -eq 1 ]; then
		FORCED_DUPLEX=$FORCED_HALF
		FORCED_SPEED=$FORCED_10
	elif [ "$CURRENT" -eq 2 ]; then
		FORCED_DUPLEX=$FORCED_FULL
		FORCED_SPEED=$FORCED_10
	elif [ "$CURRENT" -eq 4 ]; then
		FORCED_DUPLEX=$FORCED_HALF
		FORCED_SPEED=$FORCED_100
	elif [ "$CURRENT" -eq 8 ]; then
		FORCED_DUPLEX=$FORCED_FULL
		FORCED_SPEED=$FORCED_100
	elif [ "$CURRENT" -eq 16 ]; then
		FORCED_DUPLEX=$FORCED_HALF
		FORCED_SPEED=$FORCED_1000
	elif [ "$CURRENT" -eq 32 ]; then
		FORCED_DUPLEX=$FORCED_FULL
		FORCED_SPEED=$FORCED_1000
	else
		FORCED_SPEED="speed undetermined"
		FORCED_DUPLEX="duplex undetermined"
	fi
	echo "Link mode:" $FORCED_SPEED"," $FORCED_DUPLEX"," $LINK_STRING
else
	if [ "$LINK" -eq 0 ]; then
		echo "Link mode:" $LINK_STRING
	else
		if [ "$CURRENT" -eq 1 ]; then
			AUTO_MODE="10baseT-HD"
		elif [ "$CURRENT" -eq 2 ]; then
			AUTO_MODE="10baseT-FD"
		elif [ "$CURRENT" -eq 4 ]; then
			AUTO_MODE="100baseTx-HD"
		elif [ "$CURRENT" -eq 8 ]; then
			AUTO_MODE="100baseTx-FD"
		elif [ "$CURRENT" -eq 16 ]; then
			AUTO_MODE="1000baseT-HD"
		elif [ "$CURRENT" -eq 32 ]; then
			AUTO_MODE="1000baseT-FD"
		else
			AUTO_MODE="speed and duplex undetermined"
		fi
		echo "Link mode: negotiated" $AUTO_MODE"," $LINK_STRING
	fi
fi

S=$(/sbin/ifconfig "$IFACE")
S=${S#*HWaddr }
S=${S%% *}
echo MAC Address: $S
exit 0
