#!/sbin/sh 
#
# Copyright (c) 1996-1998 by Sun Microsystems, Inc.
# All rights reserved.
#
#ident	"@(#)ncakmod	1.28	99/03/28 SMI"
#

PATH=/sbin:/usr/bin:/usr/sbin; export PATH

NCA=/kernel/drv/nca
NCAKMODCONF=/etc/nca/ncakmod.conf
NCALOGDCONF=/etc/nca/ncalogd.conf
NCAIFCONF=/etc/nca/nca.if
TEMPDIR=/tmp
DEFAULT_MISS_DOOR=/tmp/nca_httpd_1.door
DEFAULT_LOGD_DOOR=/tmp/nca_logd_1.door

CHECKFORVIRT=TRUE

readifconf() {
	while read i
	do
		case "$i" in
		'#'* | '')	# Ignore comments, empty lines
				continue ;;
		'*')		INTERFACES="`echo /etc/hostname.*[0-9] \
				   2>/dev/null`"
				CHECKFORVIRT=FALSE
				break ;;
		esac
		INTERFACES="$INTERFACES $i"
	done
}

readconf() {
	while read varname varvalue; do
		case "$varname" in
		'#'* | '')	# Ignore comments, empty lines
				continue ;;
		esac
		[ "x$varname" = "x$1" ] && break
	done
}

#
# Simulates cat in sh so it doesn't need to be on the root filesystem.
#

shcat() {
	while [ $# -ge 1 ]; do
		while read i; do
			echo "$i"
		done < $1
		shift
	done
}

case "$1" in
'start')

	exec < $NCAKMODCONF; readconf "status"

	case "$varvalue" in
	'enabled')
		;; # Fall through
	*)
		# Default is "disabled"
		exit 0
		;;
	esac

	if [ "`/usr/bin/uname -r`" != "5.7" ]; then
		echo "NCA is only supported on Solaris 7"
		exit 0
	fi

	if [ -f "$NCAIFCONF" ]; then
		readifconf < $NCAIFCONF
		INTERFACES="`echo $INTERFACES | sed 's/.etc.hostname.//g'`"
		if [ "$INTERFACES" = "" ]; then
			exit 0
		fi
		if [ "$CHECKFORVIRT" = "TRUE" ]; then
		  for i in $INTERFACES
		  do
		    FINDVINTERFACES="`echo /etc/hostname.$i*[0-9] \
		      2>/dev/null | sed 's/.etc.hostname.//g'`"
		    VINTERFACES="$VINTERFACES $FINDVINTERFACES"
		  done
		  VINTERFACES="`echo $VINTERFACES | tr ' ' '\012' | grep :`"
		else
		  VINTERFACES="`echo $INTERFACES | tr ' ' '\012' | grep :`"
		fi
		INTERFACES="`echo $INTERFACES | tr ' ' '\012' | grep -v :`"
		PHYSINTERFACES="`echo $INTERFACES | tr ' ' '\012' | \
			sed 's/[0-9]*$//' | sort | uniq`"
		CHECK="`modinfo | grep nca | awk '{print $6}' | uniq`"
		if [ "$CHECK" != "nca" ]; then
			/usr/sbin/modload $NCA
		fi
		exec < $NCAKMODCONF; readconf "httpd_door_path"
		if [ "$varvalue" != "$DEFAULT_MISS_DOOR" ]; then
			ndd -set /dev/nca httpd_door_path $varvalue
		fi
		exec < $NCALOGDCONF; readconf "logd_door_path"
		if [ "$varvalue" != "$DEFAULT_LOGD_DOOR" ]; then
			ndd -set /dev/nca logd_door_path $varvalue
		fi
		exec < $NCAKMODCONF; readconf "nca_logging_on"
		if [ "$varvalue" = "enabled" ]; then
			ndd -set /dev/nca nca_logging_on 1
		fi
		echo 'configuring NCA interfaces:\c'
		for i in $VINTERFACES
		do
			/usr/sbin/ifconfig $i down
		done
		set -- $INTERFACES
		while test $# -ge 1; do
			if [ "$1" != "xx0" ]; then
				/sbin/ifconfig $1 down
				/sbin/ifconfig $1 unplumb
			fi
	  	shift
		done
		for i in $PHYSINTERFACES; do
		  MAJOR="`/usr/sbin/getmajor $i`"
		  AP="`autopush -g -M $MAJOR -m 0 2>/dev/null`" 
		  if [ "$AP" = "" ]; then
		    echo "$i  -1  0  nca" >> $TEMPDIR/nca.ap
		  else
		    autopush -g -M $MAJOR -m 0  > $TEMPDIR/nca-tmp.ap
		    MODULES="`tail +2 $TEMPDIR/nca-tmp.ap |  sed 's/.*-//'`"
		    MODULES="$MODULES nca"
		    /usr/bin/rm $TEMPDIR/nca-tmp.ap
		    /usr/sbin/autopush -r -M $MAJOR -m 0
		    echo "$i  -1  0  $MODULES" >> $TEMPDIR/nca.ap
		  fi
		done
		/usr/sbin/autopush -f $TEMPDIR/nca.ap
		rm -f $TEMPDIR/nca.ap
		INTERFACES="`echo $INTERFACES` `echo $VINTERFACES`" 
		set -- $INTERFACES
		while test $# -ge 1; do
			if [ "$1" != "xx0" ]; then
				addr="`shcat /etc/hostname.$1`"
				/sbin/ifconfig $1 plumb
				if [ -n "$addr" ]; then
					/sbin/ifconfig $1 inet "$addr" \
				    		netmask + broadcast + \
				    		-trailers up 2>&1 > /dev/null
				fi
				echo " $1\c"
			fi
	  		shift
		done
		echo '.'
	fi
	;;

'stop')

	;;

*)

	echo "Usage: $0 { start | stop }"
	exit 1
	;;
esac
exit 0
