#!/bin/bash
#
# Emulate the ifModeSet command on the 4.0 Linux-ba/bin/sed switches.
# Requires mii-tool.
#

THIS_CP=$(/sbin/sin |/bin/grep "^Control Processor" |/bin/sed 's/^.*.: //g')
FILE=/etc/fabos/mii.$THIS_CP.cfg
ADMIN=$(/usr/bin/id |/bin/sed 's/ .*//;s/^uid=//' |/bin/grep -E 'admin|root' |/bin/grep -E 'root|admin|factory')
CONSOLE=$(/usr/bin/tty |/bin/grep -E 'ttyS0|ttyS1')

function set_phy() {
	MII_MODES=${MII_MODES%,} 	# remove trailing comma
	echo -n "Committing configuration..."
	echo "/sbin/mii-tool $MII_CMD $MII_MODES $IFACE" > $FILE
	$(/bin/cat $FILE) 1>/dev/null
	/bin/sync
	echo "done."
}

IFACE=$1
if [ "$IFACE" = "" ]; then
        echo "Usage:     ifModeSet <interface>"
        echo "Example:   ifModeSet \"eth0\""
        exit 1
fi
if [ "$IFACE" != "eth0" ]; then
        echo "ifModeSet can only be used with eth0."
        exit 1
fi
if [ "$ADMIN" = "" ]; then
        echo "ifModeSet: Permission denied"
        exit 1
fi

MII_MODES=""

if [ "$CONSOLE" = "" ]; then
	echo
	echo "Exercise care when using this command.  Forcing the link to"
	echo "an operating mode not supported by the network equipment to"
	echo "which it is attached may result in an inability to "
	echo "communicate with the system through its ethernet interface."
	echo 
	echo "It is recommended that you only use this command from the"
	echo "serial console port."
	echo 

	echo -n "Are you sure you really want to do this? (yes, y, no, n): [no] "
	read ans
	if [ "$ans" = "y" -o "$ans" = "yes" ]; then
		echo "Proceed with caution.";
	else
		echo "The link is not altered."
		exit 0;
	fi
fi

echo -n "Auto-negotiate (yes, y, no, n): [no] "
read ans
if [ "$ans" = "y" -o "$ans" = "yes" ]; then
	MII_CMD="-A"	# auto-negotiate
	ADVERTISE="Advertise "
	DEFMODE="yes"
	auto=1
else
	MII_CMD="-F"	# force
	ADVERTISE="Force "
	DEFMODE="no"
	auto=0
fi

echo -n "$ADVERTISE"
echo -n "100 Mbps / Full Duplex (yes, y, no, n): [$DEFMODE] "
read ans
if [ "$ans" = "" ]; then ans="$DEFMODE"; fi
if [ "$ans" = "y" -o "$ans" = "yes" ]; then
	MII_MODES=$MII_MODES"100baseTx-FD,"
	# When forcing, we can only set one mode 
	if [ "$auto" = "0" ]; then set_phy; exit 0; fi
fi
echo -n "$ADVERTISE"
echo -n "100 Mbps / Half Duplex (yes, y, no, n): [$DEFMODE] "
read ans
if [ "$ans" = "" ]; then ans="$DEFMODE"; fi
if [ "$ans" = "y" -o "$ans" = "yes" ]; then
	MII_MODES=$MII_MODES"100baseTx-HD,"
	# When forcing, we can only set one mode 
	if [ "$auto" = "0" ]; then set_phy; exit 0; fi
fi
echo -n "$ADVERTISE"
echo -n "10 Mbps / Full Duplex (yes, y, no, n): [$DEFMODE] "
read ans
if [ "$ans" = "" ]; then ans="$DEFMODE"; fi
if [ "$ans" = "y" -o "$ans" = "yes" ]; then
	MII_MODES=$MII_MODES"10baseT-FD,"
	# When forcing, we can only set one mode 
	if [ "$auto" = "0" ]; then set_phy; exit 0; fi
fi
echo -n "$ADVERTISE"
echo -n "10 Mbps / Half Duplex (yes, y, no, n): [$DEFMODE] "
read ans
if [ "$ans" = "" ]; then ans="$DEFMODE"; fi
if [ "$ans" = "y" -o "$ans" = "yes" ]; then
	MII_MODES=$MII_MODES"10baseT-HD,"
	# When forcing, we can only set one mode 
	if [ "$auto" = "0" ]; then set_phy; exit 0; fi
fi
if [ "$MII_MODES" = "" ]; then
	echo "You must select at least one link operating mode."
	exit 1
fi

set_phy		# do it
exit 0

