#!/bin/sh
#
#ident	"@(#)net_info.sh	1.8	04/12/08 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Get network devices configuration
#
# By Svetlana Kostyuchenko

master=/platform/i86pc/boot/solaris/devicedb/master
export master

ifconfig -a > /tmp/ifc.out1

# All
egrep '(UP|BROAD)' /tmp/ifc.out1 | grep -v lo0 > /tmp/ifc.out2
# UP
egrep '(UP|BROAD)' /tmp/ifc.out1 | grep -v lo0 |grep POINTOPOINT > /tmp/ifc.out2_1
egrep '(UP|BROAD)' /tmp/ifc.out1 | grep -v lo0 | grep -v POINTOPOINT > /tmp/ifc.out2_2

grep UP /tmp/ifc.out2 | awk -F: ' { print $1 }' > /tmp/ifc.out3
grep UP /tmp/ifc.out2_2 | awk -F: ' { print $1 }' > /tmp/ifc.out3_tmp
lineno=`wc -l /tmp/ifc.out3_tmp | cut -f8 -d ' '`

if [ -f "/tmp/ifc.out3_act" ]
then
	rm -f /tmp/ifc.out3_act
fi

while [ $lineno != 0 ]
do
	i=`head -1 /tmp/ifc.out3_tmp`
	if [ -n "`grep IPv4 /tmp/ifc.out2_2 | grep $i`" ]
	then
		echo "$i.inet" >> /tmp/ifc.out3_act
	fi
	if [ -n "`grep IPv6 /tmp/ifc.out2_2 | grep $i`" ]
	then
		echo "$i.inet6" >> /tmp/ifc.out3_act
	fi
	cat /tmp/ifc.out3_tmp | grep -v $i > /tmp/ifc.out3_tmp
	lineno=`wc -l /tmp/ifc.out3_tmp | cut -f8 -d ' '`
done

grep UP /tmp/ifc.out2_1 | awk -F: ' { print $1 }' >> /tmp/ifc.out3_act
echo
echo "Active Network Interface(s): "`cat /tmp/ifc.out3_act`
echo
# NONACTIVE
grep -v UP /tmp/ifc.out2 | awk -F: ' { print $1 }' > /tmp/ifc.out4
if [ -s /tmp/ifc.out4 ]
then
	echo "Non-Active Network Interface(s): "`cat /tmp/ifc.out4`
	echo
fi
echo > /tmp/ifc.out5 2>/dev/null

echo "		Network Controller(s):                     "
  
for netdrv in `cat /tmp/ifc.out3 /tmp/ifc.out4`
do
	# Remove instance number
	netdrv=`expr $netdrv : '^\([0-9a-zA-Z]*[a-zA-Z]\)[0-9]*'`
	prtconf -D | grep $netdrv | awk -F" " ' { print $1 } ' | awk -F, ' { print $1 "," $2 } ' | sort | uniq> /tmp/net.out3 2>/dev/null
	if [ ! -s /tmp/net.out3 ]
	then
		continue
	fi

	for netdev in `cat /tmp/net.out3`
	do
		grep $netdev $master >> /tmp/ifc.out5 2>/dev/null
		if [ $? -ne 0 ]
		then 	
			sun_netdev=`prtconf -vp | nawk ' ($1 == "Node") { found = 0 } 
			 ($1 == "model:") { DEVID = $3} 
			 ($1 == "name:") && ($0 ~ /'$netdev'/) { found = 1} 
			 ($1 == "vendor-id:") && ( found == 1 ) {  
			 split(DEVID,array,".")
			 ven_dev = array[1]
			 printf "pci%s ",ven_dev}'`
			 O_IFS=$IFS
			 IFS=' '

		         for i in `echo $sun_netdev` 
		         do    
				grep $i $master >> /tmp/ifc.out5 2>/dev/null
		                if [ $? -eq 0 ]
				then
					foundit=0
					break;
				else
					foundit=1
				fi
		        done
			IFS=$O_IFS

			if [ $foundit -eq 1 ]
			then
				sun_netdev=`prtconf -vp | nawk ' ($1 == "Node") { found = 0 } 
				($1 == "device-id:") { DEVID = $2} 
				($1 == "name:") && ($0 ~ /'$netdev'/) { found = 1} 
				($1 == "vendor-id:") && ( found == 1 ) {  
					match($2, /[1-9a-f]/) 
					vendor = substr($2, RSTART, length($2)) 
					match(DEVID, /[1-9a-f]/) 
					device = substr(DEVID, RSTART, length($2)) 
					printf "pci%s,%s ",vendor, device}'`
					O_IFS=$IFS
					IFS=' '
				for i in `echo $sun_netdev` 
				do
					grep $i $master >> /tmp/ifc.out5 2>/dev/null
					if [ $? -eq 0 ]
					then
						foundit=0
						break;
					else
						foundit=1
					fi
				done
				IFS=$O_IFS
			fi
		else
			foundit=0
		fi
	done

	if [ $foundit -eq 1 ]
	then
		echo
		echo
		echo "Driver $netdrv with path $netdev was not found in master DB"
        fi
done

cat /tmp/ifc.out5 | sort | uniq 2>/dev/null
echo
rm /tmp/ifc.out* /tmp/net.out3
