#!/bin/ksh
#
# Copyright 2005 Sun Microsystems, Inc. ALL RIGHTS RESERVED
# Use of this software is authorized pursuant to the
# terms of the license found at
# http://developers.sun.com/berkeley_license.html
#
# 2004-01-20 Tom Kranz (tom@siliconbunny.com)
#
# This script is distributed under the GNU Public License (GPL) with the
# following extra conditions:
# - attritbution must be maintained
# - CD-ROM or similar media for commercial distribution without the prior 
#   approval of the author
#
# This script will build a ce.conf file based on the type of ce interface
# (GigE or 100mb). We will pull apart path_to_inst to work this out
#
# The end result is to force 100fdx for 100mb ce interfaces, and to leave it all
# up to auto negotiation for the GigE interfaces
#
# See Sun Info Doc 72033 for more information
# See Sun Alert Notification 55360 for details of the evil that happens if this
# is not setup correctly
#
# NOTE WELL: seperate device paths for midframes and starfires!

# Nasty IFS hacking ahoy, so we need to save what we have
SAVEIFS=${IFS}

# Variables to source our files
# CHANGE THESE TO SUIT
PATH_TO_INST=path_to_inst
OUTPUT=ce.conf

# We need to save the existing ce.conf, then blow it away
if [[ -a ${OUTPUT} ]]
then
	mv ${OUTPUT} ${OUTPUT}.sav
fi

# Our non-changing settings
# Note we can set 1000fdx and 100fdx to 1, because adv_autoneg_cap will always
# take priority over specific speed/duplex settings
OTHER_PARMS="adv_1000fdx_cap=0 adv_1000hdx_cap=0 adv_100fdx_cap=1 adv_100hdx_cap=0 adv_10fdx_cap=0 adv_10hdx_cap=0;"

# The auto negotiation setting - we can have a choice here
YES_AUTONEG="adv_autoneg_cap=1"
NO_AUTONEG="adv_autoneg_cap=0"

# Now we pull apart path_to_inst to work out what ce nodes we have
CARDS=`cat ${PATH_TO_INST} | grep \"ce\"`

# We'll take each line at a time, splitting things out until we get the
# data we need
IFS='
'
for INST in ${CARDS}
do
	# We need to split on spaces again
	IFS=" "
	set -A DETAILS ${INST}

	# First field in path_to_inst is the device path
	INSTPATH=${DETAILS[0]}

	# Now we need to chop up the device path to work out what sort of 
	# ce device we have
	IFS="/"
	set -A SNIPPET ${INSTPATH}

	# Naturally the midframes have to handle things differently
	if [ `uname -i` = "SUNW,Sun-Fire" ]
	then
		CE_TYPE=${SNIPPET[3]}
	else	
		CE_TYPE=${SNIPPET[2]}
	fi
	
	# What do we do with what we've matched
	case "${CE_TYPE}" in
		pci*)
			# If the device path has an extra pci* field in
			# it, we know it's a 100mb interface
			IFS=$SAVEIFS
			if [ `uname -i` = "SUNW,Sun-Fire" ]
			then
				DEVPATH="/${SNIPPET[1]}/${SNIPPET[2]}/${SNIPPET[3]}"
				UADDRESS=`echo ${INSTPATH} | cut -d@ -f5`
			else
				DEVPATH="/${SNIPPET[1]}/${SNIPPET[2]}"
				UADDRESS=`echo ${INSTPATH} | cut -d@ -f4`
			fi

			# Behold the OUTPUT!
			echo "name=\"pci108e,abba\" parent=\"${DEVPATH}\" unit-address=\"${UADDRESS} ${NO_AUTONEG} ${OTHER_PARMS}" >> ${OUTPUT}
			echo "" >> ${OUTPUT}
			;;
	
		network*)
			# Otherwise, it's a GigE interface
			IFS=$SAVEIFS
			if [ `uname -i` = "SUNW,Sun-Fire" ]
			then
				DEVPATH="/${SNIPPET[1]}/${SNIPPET[2]}"
				UADDRESS=`echo ${INSTPATH} | cut -d@ -f4`
			else
				DEVPATH="/${SNIPPET[1]}"
				UADDRESS=`echo ${INSTPATH} | cut -d@ -f3`
			fi

			# Behold the OUTPUT!
			echo "name=\"pci108e,abba\" parent=\"${DEVPATH}\" unit-address=\"${UADDRESS} ${YES_AUTONEG} ${OTHER_PARMS}" >> ${OUTPUT}
			echo "" >> ${OUTPUT}
			;;

		*)
			# Just in case something unexpected happens
			echo ""
			echo "Unable to chop up device paths - check output and input files!"
			echo ""
			exit 1
			;;

		esac

	# If you value sane output, please reset the IFS value back
	IFS=$SAVEIFS
done

exit 0
