#ident @(#)LINKBOOT	35.3
# synopsis:
#
#	linkboot
#
# names in parentheses indicates name for 88000 target
#
# if /stand/sysV68 (or /stand/sysV88) is not linked to /sysV68 (/sysV88) and
#	the kernel was booted from /stand/sysV68 (/stand/sysV88)
# then
#	if /sysV68 (sysV88) differs from /stand/sysV68 (/stand/sysV88)
#	then
#		move /sysV68 (/sysV88) to /oldsysV68 (/oldsysV88)
#		if /etc/master exists
#		then
#			move /etc/master /etc/oldmaster
#		fi
#	fi
#	link /stand/sysV68 (/stand/sysV88) to /sysV68 (sysV88)
#	if /stand/master exists
#	then
#		link /stand/master to /etc/master
#	fi
# fi
#	
#
# linkboot should be run each time the system is booted
#

PATH=/bin

# Function Definitions

inumber () {
	# synopsis:
	#
	#	inumber <filename>
	#
	# outputs the decimal inumber of <filename>
	#
	ls -i $1 | (read a b; echo $a)
}

booted () {
	# synopsis:
	#
	#	booted <filename>
	#
	# returns true if the current kernel was booted from <filename>
	# otherwise returns false
	#
	#
	# looks into the /dev/kmem and compares the i-number passed by
	# the bootloader with the i-number of <filename>
	#
	addr=`nm -x $1 | grep bootinod | (IFS='|'; read a b c; echo $b)`
	bootinum=`od -d /dev/kmem $addr | (read a b c d; echo $c)`

	if [ "$bootinum" -eq `inumber $1` ]
	then
		return 0	# yes, we were booted from <filename>
	else
		return 1
	fi
}


#
# Main Program
#

# don't run if going from run level 2 to 3
set `who -r`
if [ $9 != S ]
then
	exit 0
fi

if m88k
then
	if [ `inumber /sysV88` -ne `inumber /stand/sysV88` ] && booted /stand/sysV88
	then
		if cmp -s /sysV88 /stand/sysV88
		then
			:
		else
			if mv /sysV88 /oldsysV88
			then
				echo Moved /sysV88 to /oldsysV88
			else
				echo Could not move /sysV88 to /oldsysV88
			fi
		fi
		if ln /stand/sysV88 /sysV88
		then
			echo Linked /stand/sysV88 to /sysV88
		else
			echo Could not link /stand/sysV88 to /sysV88
		fi
	fi
else
	if [ `inumber /sysV68` -ne `inumber /stand/sysV68` ] && booted /stand/sysV68
	then
		if cmp -s /sysV68 /stand/sysV68
		then
			:
		else
			if mv /sysV68 /oldsysV68
			then
				echo Moved /sysV68 to /oldsysV68
				if [ -f /etc/master ]
				then
					mv /etc/master /etc/oldmaster
				fi
			else
				echo Could not move /sysV68 to /oldsysV68
			fi
		fi
		if ln /stand/sysV68 /sysV68
		then
			echo Linked /stand/sysV68 to /sysV68
			if [ -f /stand/master ]
			then
				ln /stand/master /etc/master
			fi
		else
			echo Could not link /stand/sysV68 to /sysV68
		fi
	fi
fi
