:
#ident	"@(#)idmkenv	1.5	91/02/14"
#	@(#) idmkenv 22.1 89/11/06 
#ident "@(#)idmkenv	1.3 90/04/23"
#	@(#) idmkenv 1.8 89/04/15 
#
#    Portions Copyright 1988, 1989 The Santa Cruz Operation, Inc
#		      All Rights Reserved

#
#ident	"@(#)idcmd:idmkenv	1.4"
#
# Idmkenv: This script is part of the Installable Drivers (ID) scheme.
#          It is called at boot time by an Init State 2 entry in
#          /etc/inittab. It's purpose is to update /etc/idrc.d,
#          /etc/idsd.d, /etc/inittab, and the device nodes in /dev.
#
#          A second function of idmkenv is to recover the Kernel
#          environment when a reconfiguration is aborted due to
#          losing power or the user hitting RESET.
#

BIN=/etc/conf/bin
USAGE="$0 [-y|-n]"
OK=0
FAIL=1

getyn() {
	while	echo "$* (y/n):  \c"
	do	read yn rest
		case $yn in
		[yY])	return $OK 			;;
		[nN])	return $FAIL			;;
		*)	echo "
	Please answer 'y' or 'n'."	;;
		esac
	done
	}
#
# kinstall - install new kernel in /
#
kinstall() {
	echo "	Backing up /unix to /unix.old"
	[ -f /unix ] && mv /unix /unix.old
	echo "	Installing new /unix"
	mv /etc/conf/cf.d/unix /unix

	# Install symbols for SCODB if the right components exist

	if [ -x $BIN/putsym -a -f /etc/conf/cf.d/stun.def ]
	then
		$BIN/putsym /unix /etc/conf/cf.d/stun.def > /dev/null 2>&1
	fi

	# Extract the comment section to reduce kernel size

	if [ -x /usr/bin/mcs ]
	then
		/usr/bin/mcs -d /unix > /dev/null 2>&1
	fi
	}
#
# askinstall - conditionally install new kernel (interactively)
#
askinstall() {
cd /etc/conf/cf.d
if [ -f unix ]
then
	chmod 040 ./unix
	chgrp mem ./unix
	chown bin ./unix
	getyn "
	Do you want this kernel to boot by default?" || {
		# selected no.
		echo "
	The new kernel is in the directory /etc/conf/cf.d.
	To activate this kernel, reboot the system.

	Then, at the 'Boot: ' prompt, enter:
	
		/etc/conf/cf.d/unix
" >&2
		echo "
	Device node or inittab changes associated with this 
	new kernel have not been made. These changes should 
	be made by running 

		touch /etc/.new_unix
		/etc/conf/bin/idmkenv
" >&2
		rm -f /etc/.new_unix
		return $OK
	}
	# backup kernel & install
	kinstall
fi
echo "
	The kernel environment includes device node files 
	and /etc/inittab.  The new kernel may require changes 
	to /etc/inittab or device nodes.
" >&2
getyn "
	Do you want the kernel environment rebuilt?" || {
	# selected no.
	echo "
	Device node or inittab changes associated with this 
	new kernel have not been made. These changes should 
	be made by running 
	
		touch /etc/.new_unix
		/etc/conf/bin/idmkenv
" >&2
		rm -f /etc/.new_unix
		return $OK
	}
	echo "
	The kernel has been successfully linked and installed.
	To activate it, reboot your system.
" >&2
	return $OK
	}

set +e
set +v

#
# argument processing (for automated invocation)
# y - answers yes to questions (move kernel to / & run idmkenv)
# n - answers no
#
       set - - `getopt yn $*`
       if [ $? != 0 ]
       then
	    echo $USAGE
	    exit 2
       fi
       for i in	$*
       do
	    case $i in
	    -y)  FLAG=$i; shift;;
	    -n)  FLAG=$i; shift;;
	    --)      shift;	break;;
	    esac
       done

#
# main
#
case $FLAG in
	    -y)  kinstall;;
	    -n)  rm -f /etc/.new_unix; exit 0;;
	    *)   askinstall;;
esac
#
# run original idmkenv code here
#
if [ -f /etc/.new_unix ]
then
	echo "
	Setting up new kernel environment."
	rm -f /etc/idrc.d/* /etc/idsd.d/* /etc/ps_data
	if [ -f /etc/conf/rc.d/* ]
	then
		for rc in /etc/conf/rc.d/*
		do
			ln $rc /etc/idrc.d
		done
	fi
	if [ -f /etc/conf/sd.d/* ]
	then
		for sd in /etc/conf/sd.d/*
		do
			ln $sd /etc/idsd.d
		done
	fi
	MASK=`umask`
	umask 022
	$BIN/idmknod
	umask $MASK
	$BIN/idmkinit
	chown root /etc/conf/cf.d/inittab
	chgrp bin /etc/conf/cf.d/inittab
	chmod 644 /etc/conf/cf.d/inittab
	mv /etc/conf/cf.d/inittab /etc/inittab
	rm -rf /etc/.new_unix /etc/.unix_reconf /etc/.last_dev_add /etc/.last_dev_del
fi
#
# This case attempts to recover an aborted reconfiguration.
#
if [ -f /etc/.unix_reconf ]
then
	echo "
	Recovering Kernel configuration."
	if [ -f /etc/.last_dev_add ]
	then
		DEV=`cat /etc/.last_dev_add`
		$BIN/idinstall -d $DEV > /dev/null 2>&1
	elif [ -d /etc/.last_dev_del ]
	then
		cd /etc/.last_dev_del
		DEV=`cat dev`
		if [ -f mdevice ]
		then
			mv mdevice /etc/conf/cf.d/mdevice
		fi
		if [ -d pack.d ]
		then
			mkdir /etc/conf/pack.d/$DEV > /dev/null 2>&1
			mv pack.d/* /etc/conf/pack.d/$DEV
		fi
		for REC in  node.d sdevice.d init.d rc.d sd.d mfsys.d sfsys.d
		do
			if [ -f $REC ]
				then mv $REC /etc/conf/$REC/$DEV
			fi
		done
	fi
	cd /etc/conf/cf.d
	# what to do here (with /etc/conf/cf.d/unix ?)
	rm -f unix config.h conf.c vector.c direct ifile buffers.o conf.o gdt.o linesw.o space.o vector.o sdevice.new
	rm -f /etc/conf/pack.d/*/Space.o
	rm -rf /etc/.unix_reconf /etc/.last_dev_add /etc/.last_dev_del
fi
telinit q
