#
#	@(#)m332xt	35.4 
#
# RC Script to download MVME332XTs
#
# (c) Motorola Inc, 1987, 1988
#
# There is a lot of messing about to determine which version of
# the "itt" program is on the system.  This is because versions
# of "itt" less than 1.9 will not allow their standard input to
# be redirected and will therefore only work for tty drivers that
# are connected to the console.  Each MVME332XT has a distinct
# device driver (its out on the board) so we have to have a version 
# of "itt" which can ioctl its standard input (like stty does).
#
# Revision History
#  08 Oct 87  Added ISP support                               DPC
#

check_itt_version( )
{
	#
	 # Check that the version of the itt program is at least 1.9
	 # Return 0 if it is, 1 otherwise
	#
	ITTVER=`what /etc/itt | sed -n -e '2s/[^0-9]*\([^ ]*\).*/\1/p'`
	ITTMAJ=`echo $ITTVER | sed -n -e 's/\([0-9]*\).*/\1/p'`
	ITTMIN=`echo $ITTVER | sed -n -e 's/[^.]*\.\([0-9]*\)/\1/p'`

	if [ $ITTMAJ -gt 1 ]
	then
		return 0
	elif [ $ITTMAJ = 1 -a $ITTMIN -ge 9 ]
	then
		return 0
	else
		return 1
	fi
}

PATH=$PATH:/etc

# Do we have the necessary program to continue?
if [ ! -x /etc/m332xctl ]
then
	exit 0
fi

if [ "$1" = "start" ]
then
	set `who -r`
	if [ $9 = S ]
	then
		dir=/stand/m68k/boots
		# If we have ISP, say so ...
		USEISP=FALSE
		if [ -d /etc/cs.term -a -x /etc/itt ]
		then
			if check_itt_version
			then
				USEISP=TRUE
				ISPMSG=' and ISP Tables'
			else
				echo 'WARNING: ISP tables not loaded on MVME332XT'
				echo "         /etc/itt is revision $ITTVER, must be at least revision 1.9"
			fi
		fi
		echo "Downloading MVME332XT Line Disciplines$ISPMSG."
		for dev in /dev/m332x?8
		do
			if m332xctl -t ${dev}
			then
				# get Firmware version number
				FW_ver=`m332xctl -R ${dev}`

				# get the greatest DownLoad version number
				DL_ver=`ls /stand/m68k/boots/m332.*.o | \
						sed -n -e '$s/[^.]*\.\([^.]*\).*/\1/p'`
				# only down load if FW version is older than DL version.
				if [ ${FW_ver} -lt ${DL_ver} ]
				then
					if [ ! -f ${dir}/m332.${FW_ver}.${DL_ver} ]
					then
						m332xctl -s $dev > ${dir}/sym_${FW_ver}.ld
						ld -o ${dir}/m332.${FW_ver}.${DL_ver} \
							${dir}/sym_${FW_ver}.ld \
							${dir}/m332.${DL_ver}.o
						rm ${dir}/sym_${FW_ver}.ld
					fi
					# m332xctl -d ${dir}/m332.${FW_ver}.${DL_ver} -l ${dev}
					m332xctl -d ${dir}/m332.${FW_ver}.${DL_ver} -l -e dlinit ${dev}
				fi

				#
				 # If we have ISP translation tables then download them
				 # to the 332xt
				#
				if [ $USEISP = TRUE ]
				then
					(
					cd /etc/cs.term
					for T in *
					do
						itt t=$T < ${dev}
					done
					)
				fi
			fi
		done
	fi

else # we're shutting down

	if [ -d /etc/cs.term -a -x /etc/itt ]
	then
		if check_itt_version
		then
			echo 'Removing ISP translation tables from MVME332XT.'
			(
			cd /etc/cs.term
			for dev in /dev/m332x?8
			do
				if m332xctl -t ${dev}
				then
					for T in *
					do
						itt -r t=$T < ${dev}
					done
				fi
			done
			)
		fi
	fi

fi
