#! /bin/sh
#
# ident	"%Z%%M%	%I%	%E% SMI"
#
# Copyright (c) 1998 by Sun Microsystems, Inc.
# All rights reserved.
#
# startup script for the SunHIPPI/P device drivers.
#


# get installation base for package
basedir=`/usr/bin/pkginfo -r SUNWhip 2>/dev/null`/etc/opt/SUNWconn/hippi
if [ ! -r "${basedir}" ] ; then
	echo "SUNWhip package doesn't appear to be installed."
	echo "Please install the package before continuing."
	exit 1
fi


case "$1" in
'start')
	# get number of NICs in system
	nbhip=`${basedir}/bin/hippi cards 2>/dev/null`

	if [ "${nbhip}" -eq 0 ] ; then
		exit 0
	fi

	#
	# start daemons
	#
	${basedir}/bin/hippid 2>&1 >/dev/null
	${basedir}/bin/hippidmpd -d `/usr/bin/pkginfo \
		-r SUNWhip`/var/opt/SUNWconn/hippi 2>&1 >/dev/null

	#
	# for every interface with a configuration file, bring it up
	#
	interface_names="`echo ${basedir}/hip*[0-9].conf 2>/dev/null`"
	if [ "'$interface_names'" != "'${basedir}/hip*[0-9].conf'" ] ; then
		(
		IFS="${IFS}."
		set -- ${interface_names}
		while [ $# -ge 2 ] ; do
			# set interface names
			hipname=`basename $1`
			curhip=`echo ${hipname} | sed "s/hip//g"`
			hipipname="hipip${curhip}"
			confname="${basedir}/${hipname}.conf"

			# get pci state register value according to architecture
			if [ "`uname -i`" = SUNW,Ultra-5_10 ] ; then
				pcistate=0x54
			else
				pcistate=0x90
			fi

			# set tuning parameters
			${basedir}/bin/hippitune -D ${hipname} -x 0x500000 \
				-r 0x200000 -s 20000000 -h ${pcistate} \
				-i 0x120 -w 0x80 -d 0x80

			# cycle NIC and print flags
			${basedir}/bin/hippi off ${hipname}
			${basedir}/bin/hippi on fp network switched ${hipname}
			sleep 2

			# set debug level
			${basedir}/bin/hippidb -t0 -d1  2>&1 >/dev/null

			# if BASEDIR/etc/opt/SUNWconn/hippi/r_main.obj exists
			# then use it as firmware instead of default
			if [ -r "${basedir}/r_main.obj" ] ; then
			    ${basedir}/bin/hippidb -t0 -d5 2>&1 >/dev/null
			    ${basedir}/bin/hippidnld -l \
				"${basedir}/r_main.obj" \
				${hipname} 2>&1 >/dev/null
			    ${basedir}/bin/hippi restart \
				${hipname} 2>&1 >/dev/null
			fi

			# configure network device

			# get IP configuration info
			ipaddr=`awk '{ print $1 }' "${confname}"`
			netmask=`awk '{ print $2 }' "${confname}"`
			swaddr=`awk '{ print $3 }' "${confname}"`

			# set logical address
			${basedir}/bin/hippiarp -l ${swaddr} ${hipname}

			ifconfig ${hipipname} plumb 2>&1 >/dev/null
			ifconfig ${hipipname} "${ipaddr}" \
				netmask "${netmask}" broadcast + \
				mtu 57384 up 2>&1 >/dev/null

			sleep 1

			# proceed with next device
			shift
			shift
		done
		)
	fi

	# add arp entries
	$basedir/hippiarp.conf

	;;

'stop')
	# stop daemons
	${basedir}/bin/hippid -k >/dev/null 2>&1 
	${basedir}/bin/hippidmpd -k >/dev/null 2>&1 

	#
	# for all configured interfaces, bring them down
	#
	interface_names=`ifconfig -a|egrep '^hipip'|cut -f1 -d" "|sed "s/://g"`
	if [ -n "${interface_names}" ] ; then
		(
		set -- ${interface_names}
		while [ $# -ge 1 ] ; do
			# set interface names
			curhip=`echo $1|sed "s/hipip//g"`

			# turn off NIC
			${basedir}/bin/hippi off hip${curhip} 2>&1 >/dev/null

			# bring down IP
			/usr/sbin/ifconfig $1 down unplumb 2>&1 >/dev/null

			# proceed with next device
			shift
		done
		)
	fi
	;;
*)
	echo "Usage: /etc/init.d/hippi { start | stop }"
	;;
esac
exit 0
