#!/bin/sh

#AMCQUERY MC5000 UTILITY
#(c) Meru Networks 2008

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

#Platform sanity tests

if [ ! -f /etc/platform ]; then
     echo "Error: Could not detect platform type."
     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

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

#Globals

IPMITOOL=/opt/meru/bin/ipmitool
IPMIQ=/opt/meru/bin/ipmiq

MC5KETH="eth6"

# Default device (none detected)
DEVICE="NONE"
DEVDESC="NONE"

# Device array for AMC detection.  First element = FRU name, second element = Meru name.
DEVICES=( "AMC203"   "MC5K-BLK1C2F2"  "Dual Port SFP/Copper GbE Interface" 
          "AMC 9479" "MC5K-BLK1F2"    "Dual Port SFP GbE Interface"  
          "AMC-7211" "MC5K-BLK1A4"    "Quad Port SFP GbE Interface with Hardware Acceleration" 
          "AM4301"   "MC5K-BLK1C4"    "Quad Port Copper GbE Interface" )

NUMDEVICES=4
RECORDSZ=3

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

# Display simple banner for the utility
banner() {
     echo "Querying AMC FRU data, please be patient..."
}

# Perform actual device detection.  Store result in DEVICE
detect_device() {
     # Sanity check: make sure IPMI tools are installed
     $IPMIQ install 2>&1

     # Grab the FRU data
     FRUDATA=`$IPMITOOL fru 2>&1 | grep "Device Description" 2>&1`

     for ((dev = 0; dev < NUMDEVICES; dev++))
     do   
          devind=`expr $dev \* $RECORDSZ`
          dev_to_check=${DEVICES[${devind}]}

          if `echo "$FRUDATA" | grep "$dev_to_check" 2>&1 > /dev/null`; then
               devind=`expr $devind + 1`               
               DEVICE=${DEVICES[${devind}]}
               devind=`expr $devind + 1`               
               DEVDESC=${DEVICES[${devind}]}
               break
          fi

     done
}

amc203_get_interface() {
     AMC203INTERFACE="Copper"
     EEPFIND=`/usr/sbin/ethtool -e $MC5KETH 2>&1 | grep '0x0010' | grep -o '60 10'`
     if [ "$EEPFIND" = "60 10" ]; then
          AMC203INTERFACE="SFP"
     fi
}

# "banner" argument: display banner
if [ "$1" = "banner" ]; then
     banner
     exit 0
fi

# "quick" argument: output device
if [ "$1" = "quick" ]; then
     detect_device
     echo $DEVICE
     exit 0
fi

# "interface" argument: get amc203 interface
if [ "$1" = "interface" ]; then
     amc203_get_interface
     echo $AMC203INTERFACE
     exit 0
fi


# Otherwise print it all
banner
detect_device

if [ "$DEVICE" = "NONE" ]; then
     echo ""
     echo "No AMC card recognized!"
     echo ""
     exit 0
fi

CONFINT=""
if [ "$DEVICE" = "MC5K-BLK1C2F2" ]; then
     amc203_get_interface
     CONFINT="[EEPROM configured to use $AMC203INTERFACE ports]"
fi


echo ""
echo "AMC card recognized."
echo ""
echo "Name:       $DEVICE"
echo "Descrption: $DEVDESC $CONFINT"
echo ""

exit 0




