:
#	@(#) idmkenv 23.8 91/10/31 
#
#	Copyright (C) 1988-1991 The Santa Cruz Operation, Inc.
#		All Rights Reserved.
#	The information in this file is provided for the exclusive use of
#	the licensees of The Santa Cruz Operation, Inc.  Such users have the
#	right to use, modify, and incorporate this code into other products
#	for purposes authorized by the license agreement provided they include
#	this notice and the associated copyright notice with any such product.
#	The information in this file is provided "AS IS" without warranty.
#
#	      UNIX is a registered trademark of AT&T
#		Portions Copyright 1976-1989 AT&T
#		      All Rights Reserved
#
#ident	"@)#(idcmd:idmkenv	1.4"
#
#	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. Its 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"
	chmod 0440 /etc/conf/cf.d/unix
	chgrp mem /etc/conf/cf.d/unix
	chown bin /etc/conf/cf.d/unix
	mv /etc/conf/cf.d/unix /unix
	}
#
# askinstall - conditionally install new kernel (interactively)
#
askinstall() {
cd /etc/conf/cf.d
if [ -f unix ]
then
	getyn "\nDo 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\n" >&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.
"
getyn "\nDo 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 "\nThe kernel has been successfully linked and installed.
	To activate it, reboot your system.\n" >&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 /etc/ps/*
	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 audit /etc/conf/cf.d/inittab
	chmod 0664 /etc/conf/cf.d/inittab
	mv /etc/conf/cf.d/inittab /etc/inittab
	rm -rf /etc/.new_unix /etc/.unix_reconf
	rm -rf /etc/.last_dev_add /etc/.last_dev_del
	grep "^NODE[ 	]" /etc/conf/cf.d/stune >/dev/null 2>&1 && \
		rm -f /etc/rc2.d/S11uname
fi
#
# This case attempts to recover an aborted reconfiguration.
#
if [ -f /etc/.unix_reconf ]
then
	echo "\n\tRecovering 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
