#!/sbin/sh
#
#ident	"@(#)sysidtool-net.sh 1.22     05/04/28 SMI"
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# /lib/svc/method/sysidtool-net
#
# Script to invoke sysidnet, which completes configuration of basic
# network parameters.
#

. /lib/svc/share/smf_include.sh

config_interfaces() {
		
	#
	# Configure all network interfaces
	#

	#
	# GLXXX this is an ugly hack.  See 5032543
	# This line should be removed after this bug is fixed.
	#
	/usr/sbin/devfsadm > /dev/msglog 2>&1

	/sbin/ifconfig -a plumb > /dev/null 2>&1

	#
	# Get the complete list of network devices
	# so that we can revarp them individually
	# since the -ad option seems to stop after
	# the first failure (unconnected net device)
	# that it encounters
	#
	for i in `ifconfig -a |grep "^[a-z0-9]*:"`
	do
		echo $i |grep "^[a-z0-9]*:" >/dev/null 2>&1
		if [ $? -eq 1 ]; then
			continue
		fi
		# Remove colon after interface name and any 
		# logical interface portion that follows it.
		m=`echo ${i} | sed 's/:[0-9]*:*//'`
		net_device_list="${m}\n${net_device_list}"
	done

	#
	# net_device_list contains a new line delimited list
	# of network devices. The new line is used for the (unique) sort.
	# do an auto-revarp on each of unique interface with the 
	# exception of the loopback device
	#

	net_device_list=`echo $net_device_list | sort -u`

	echo `gettext "Configuring network interface addresses:"`"\\c" > /dev/sysmsg
	set -- $net_device_list
	for i 
	do
		#
		# skip the auto-revarp for the loopback device
		#
		if [ "$i" = "lo0" ]; then
			continue
		fi
		echo " ${i}\c" > /dev/sysmsg
		/sbin/ifconfig $i auto-revarp netmask + broadcast + \
						>/dev/null 2>&1
		ipaddr=`/sbin/ifconfig $i |grep inet |awk '{print $2;}'`
		if [ "X$ipaddr" != "X0.0.0.0" ] ; then
			# The interface configured itself correctly
			/sbin/ifconfig $i up
		fi
	done
	echo `gettext "."` > /dev/sysmsg
	/sbin/hostconfig -p bootparams > /dev/null 2>&1
}	

if [ -f /etc/.UNCONFIGURED -a -x /usr/sbin/sysidnet ] ; then

	#
	# set the _INIT_NET_IF and _INIT_NET_STRATEGY variables.
	#
	smf_netstrategy

	/usr/sbin/sysidnet -l < /dev/console > /dev/console 2>&1
	if [ -x /usr/sbin/sysidconfig ] ; then
		/usr/sbin/sysidconfig < /dev/console > /dev/console 2>&1
	fi

	if [ "${_INIT_ZONENAME:=`/sbin/zonename`}" = "global" -a \
		"X$_INIT_NET_STRATEGY" != "Xdhcp" ] ; then
		config_interfaces
	fi

	/usr/sbin/sysidnet < /dev/console > /dev/console 2>&1
	if [ -x /usr/sbin/sysidkrb5 ]
	then
		/usr/sbin/sysidkrb5 < /dev/console > /dev/console 2>&1
	fi

	#
	# Another ugly hack for greenline: the sysidnet utility can wind up
	# starting rpcbind, which (seriously) confuses greenline.
	#
	pkill -9 rpcbind
fi
exit 0
