#!/bin/sh

#AMC203CONFIG MC5000 UTILITY
#(c) Meru Networks 2008

#--------------------------------------------------------

#Platform sanity tests

if [ ! -f /etc/platform ]; then
     echo "Error: Could not detect platform type."
     exit 1
fi

if [ ! -f /opt/meru/bin/amcquery ]; then
     echo "Error: Cannot detect AMC configuration."
     exit 1
fi

PLATFORM=`cat /etc/platform`

if [ "$PLATFORM" != "MC5000" ]; then
     echo "Error: This utility is intended only for the MC5000 platform."
     exit 1
fi

# Make sure we pass either the sfp or copper option
if [ "$1" != "sfp" -a "$1" != "copper" ]; then
     echo "Usage: $0 <sfp | copper>"
     exit 1
fi

ETHTOOL=/usr/sbin/ethtool

AMCCARD="MC5K-BLK1C2F2"
AMCQUERY=/opt/meru/bin/amcquery
AMCIMAGE=/opt/meru/bin/amc203ethcfg

AMCETH=eth6

# Make sure we have an AMC203

$AMCQUERY banner
DETCARD=`$AMCQUERY quick`

if [ "$DETCARD" != "$AMCCARD" ]; then
     echo "Error: This utility is indended only for MC5000 platforms with the $AMCCARD card installed."
     exit 1
fi

#--------------------------------------------------------

CURINTERFACE=`$AMCQUERY interface`
DESINTERFACE="Copper"

if [ "$1" = "sfp" ]; then
     DESINTERFACE="SFP"
fi

if [ "$CURINTERFACE" = "$DESINTERFACE" ]; then
     echo ""
     echo "Your $AMCCARD EEPROM is already configured to use the $DESINTERFACE ports."
     echo ""
     echo "If you have just reconfigured, you will need to reboot the blade before"
     echo "the changes can take effect."
     exit 0
fi


#--------------------------------------------------------

echo ""
echo "EEPROM is currently configured for:      $CURINTERFACE"
echo "This utility will now set the EEPROM to: $DESINTERFACE"

# Set src file for binary eeprom image
MAGIC=0x105E8086
EEP=/opt/meru/etc/amc203_sfp.eep
if [ "$CURINTERFACE" = "SFP" ]; then
     EEP=/opt/meru/etc/amc203_copper.eep
fi

RAW=/tmp/amc203-eeprom.bin
FIX=/tmp/amc203-fixup.txt

# Dump EEPROM contents to raw
$ETHTOOL -e $AMCETH raw on > $RAW 

# Fix the EEPROM image!
RES=`$AMCIMAGE --rawin $RAW --mode eepmerge $EEP --fixout $FIX 2>&1`

FIXCONTENTS=`cat $FIX`
IFS=$'\012'

# Now write all of the new EEPROM info

echo ""
echo -n "Writing new EEPROM information: "

iter=0

for eepslot in $FIXCONTENTS; do
     addr=`echo $eepslot | awk '{ print $1 }'`
     data=`echo $eepslot | awk '{ print $2 }'`
     $ETHTOOL -E $AMCETH magic $MAGIC offset $addr value $data 2>&1 || {
          MAGIC=0x10608086
          $ETHTOOL -E $AMCETH magic $MAGIC offset $addr value $data 2>&1 || {
               echo "Error while writing to EEPROM."
               exit 0
          }
     }
     iter=`expr $iter + 1`
     if [ $iter -eq 30 ]; then
          echo -n "."
          iter=0
     fi
done

echo " done"
/usr/bin/logger -p local2.info "<nms>: `date +%s` | nms | info | CONFIG | [MODIFY:Global Controller Parameters], AMCCARD is changed to $1" 
echo ""
echo "*** YOU MUST RESTART THE CONTROLLER FOR CHANGES TO TAKE EFFECT ***"
echo ""

IFS=$' \t\n'

rm -f $RAW
rm -f $FIX

