:
#	Copyright (c) 1988, 1990 Intel Corporation
#	All Rights Rreserved
#
#	INTEL CORPORATION PROPRIETARY INFORMATION
#
#	This software is supplied to AT & T under the terms of a license 
#	agreement with Intel Corporation and may not be copied nor 
#	disclosed except in accordance with the terms of that agreement.

#ident	"@(#)mbus:cmd/initpkg/init.d/mb2_init	1.2.2.1"
 
# mb2_init:	Download 410.fw software to iSBC 186/410 Comm. board
#		Setup device nodes for 186/410 and 279
#		Initialize 279 menus
#
#		usage: mb2_init [ start | stop ]
#
#	This script is linked to the /etc/rc?.d directories allowing
#	automatic initialization of various MB2 agents as the run state
#	of UNIX changes.
#
#
##### Tab Stops = 4
# 
state=$1
set `who -r`
count=$8
previous=$9
#####################
#Support functions
#####################
CCI_D=/usr/lib/cci						# location of cci utilities
MDEV=/etc/conf/cf.d/mdevice				# location of kernel config file
CONFIG=/etc/default/bootserver/config	# location of system config file
serial_channel=0						# start with tty000
#
# function to make the serial device nodes
#	ARGS:
#	1->	Major number
#
# Also rename the nodes with minor number 4 & 5(if exists)  to /dev/lp*
#
Do_mknod() {
	major=$1
	rm -f /dev/tty???
	/sbin/iasyports | awk '{ print "mknod /dev/" $1, "c '$major'", $2; }' | sh

	rm -f /dev/lp
	nodename=`/sbin/iasyports | nawk '$2 == 5 { print $1 }'`
	if [ x"$nodename" != x ] ; then
		mv /dev/$nodename /dev/lp
		echo "mb2_init: /dev/$nodename mapped to /dev/lp" 
		nodename=`/sbin/iasyports | nawk '$2 == 4 { print $1 }'`
		if [ x"$nodename" != x ] ; then
			mv /dev/$nodename /dev/lp1
			echo "mb2_init: /dev/$nodename mapped to /dev/lp1" 
		fi
	else
		nodename=`/sbin/iasyports | nawk '$2 == 4 { print $1 }'`
		if [ x"$nodename" != x ] ; then
			mv /dev/$nodename /dev/lp
			echo "mb2_init: /dev/$nodename mapped to /dev/lp" 
		fi
	fi
}
#
# function to make the graphics  device node for the 279, and 
# set things up
#	ARGS:
#	1-> MB2 Slot Id
#
Do_i279() {
	slot_id=$1
	minor_num=`cpunum -t`
	minor_num=`expr $slot_id \* 6 + 6 + $minor_num `
	old_name=`ls -l /dev/tty??? | awk '/, *'$minor_num'/ { print $NF; }'`
	rm -f $old_name
	echo "mb2_init: $old_name mapped to /dev/console" 
	graphic_id=5
	graphics_setup=/usr/lib/i279/menu
	rm -f /dev/graphics  		 # remove the old node
	minor_num=`expr $slot_id \* 6 + 6 + $graphic_id `
	old_name=`ls -l /dev/tty??? | awk '/, *'$minor_num'/ { print $NF; }'`
	mv $old_name /dev/graphics
	chown root /dev/graphics
	chmod 600 /dev/graphics
	echo "mb2_init: $old_name mapped to /dev/graphics" 
	[ -f $graphics_setup ] && {
		(stty raw; cat $graphics_setup) >/dev/graphics 0<&1
	}
}
#
# Function to get major number for a given Module name
#	ARGS:
#	1-> module name
#
Do_get_c_major () {
	mod_name=$1
	mod_mdev=`egrep "^$mod_name[ 	]" /etc/conf/cf.d/mdevice`
	mod_sdev=`egrep "^$mod_name[ 	]" /etc/conf/cf.d/sdevice`
	if [ -n "$mod_sdev" ];then
		set $mod_sdev
		if [ $2 = Y ];then
			if [ -n "$mod_mdev" ];then
				set $mod_mdev
				echo $6
			else
				echo "-1"	
			fi
		else
			echo "-1"	
		fi
	else
		echo "-1"	
	fi
}
#
# Function to get slot to serial board mapping.
# note that the current implimentation is rather brain-dead, it only
# recoginized the default MDP and SYP520 assignments. This will
# eventually move into /etc/default/bootserver/config.
#	ARGS:
#	1-> slot id 
#
Do_get_serial_map(){
	slot_id=$1
	case $slot_id in
		0)	echo "-1" ;;
		1)	echo "0" ;; 	# MDP board 0
		2)	echo "1" ;; 	# MDP board 1
		3)	echo "3" ;;	# SYP520 board 3
		4)	echo "2" ;;	# SYP520 board 2
		5)	echo "1" ;; 	# SYP520 board 1
		6)	echo "0" ;; 	# SYP520 board 0
		7)	echo "-1" ;;
	esac
}
#####################
case $state in

'start')
		if [ "$previous" = "S" -a "$count" = "0" ];then
			Do_mknod `Do_get_c_major iasy`
			for slot in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
			do
				ID1=`/sbin/icsrd -d $slot 0 1`
				ID2=`/sbin/icsrd -d $slot 1 1`
				[ $ID1 -eq 0  -a $ID2 -eq 0 ] && continue
				BOARD_NAME=`/sbin/icsrd -s $slot 2 10`
				case $BOARD_NAME in
					 "186/410")
							/etc/conf/bin/idcheck -p mb2stai
							if [ $? -gt 0 -a $? -lt 100 ];then
								/sbin/lckld $CCI_D/410.fw $slot
							fi
							;;
					 "186/450")
							/etc/conf/bin/idcheck -p mb2stai
							if [ $? -gt 0 -a $? -lt 100 ];then
								/sbin/lckld $CCI_D/450.fw $slot
							fi
							;;
					 "386/258")
							# test to see if the 279 is present (???)
							ICS=`/sbin/icsrd $slot 70 1`
							[ -n "$ICS" ] && set $ICS
							#check interconnect space for atcs server
							if [ $4 -eq 129 ];then
								# bail out if rci not configured in
								major_rci=`Do_get_c_major rci`
								[ $major_rci = -1 ] && continue
								Do_i279 $slot
								# termtype -t i279 console
							else
								: termtype -t ansi console
							fi
							;;
				esac
			done
	else
    	exit
	fi
	;;
'stop')
	:"Do Nothing" 
	;;
esac
