:
#	@(#) rtsinstall 22.2 90/05/03 
#
#	Copyright (C) The Santa Cruz Operation, 1988, 1989, 1990.
#	This Module contains Proprietary Information of
#	The Santa Cruz Operation, and should be treated as Confidential.
#
# rtsinstall
#
# tar off the entire OS from the rest of the tape
# also does some of the stuff the custom would do
: ${OK=0} ${FAIL=1} ${STOP=10}

PATH=/bin:/etc:/usr/bin:/altos/bin
LANG=english_us.ascii
SCOLIB="/usr/lib/sco"
export PATH LANG SCOLIB
usrfs="/dev/usr"
MIRROR_INFO=/tmp/mirror_info
version_info=version.tmp
kbd_info=kbd.tmp
MIRRORED_ROOT=1
NON_MIRRORED_ROOT=2


# Delete device nodes for usr, rusr, root and rroot and
# re-create them. 

create_tmp_nodes() {

	if [ ! -f /etc/mknod -o ! -f /bin/chmod -o \
	     ! -f /bin/chown -o ! -f /bin/chgrp ]
 	then
 		echo "
Error: One of the files mknod, chmod, chown or chgrp missing.
Cannot proceed with upgrade."
 		return $FAIL
 	fi
 	rm -f /dev/usr  		> /dev/null 2>&1
 	rm -f /dev/rusr  		> /dev/null 2>&1
 	rm -f /dev/root  		> /dev/null 2>&1
 	rm -f /dev/rroot  		> /dev/null 2>&1
 	mknod /dev/usr   b 64 42	> /dev/null 2>&1
 	mknod /dev/rusr  c 64 42	> /dev/null 2>&1
 	mknod /dev/root  b 64 40	> /dev/null 2>&1
 	mknod /dev/rroot c 64 40	> /dev/null 2>&1
 	chmod 440 	/dev/usr	> /dev/null 2>&1
 	chown root 	/dev/usr	> /dev/null 2>&1
 	chgrp backup 	/dev/usr	> /dev/null 2>&1
 	chmod 640 	/dev/rusr	> /dev/null 2>&1
 	chown sysinfo 	/dev/rusr	> /dev/null 2>&1
 	chgrp sysinfo 	/dev/rusr	> /dev/null 2>&1
 	chmod 440 	/dev/root	> /dev/null 2>&1
 	chown root 	/dev/root	> /dev/null 2>&1
 	chgrp backup 	/dev/root	> /dev/null 2>&1
 	chmod 640 	/dev/rroot	> /dev/null 2>&1
 	chown sysinfo 	/dev/rroot	> /dev/null 2>&1
 	chgrp sysinfo 	/dev/rroot	> /dev/null 2>&1

}


create_nodes() {

	if [ $# -ne 1 ]
	then
		echo "
	create_nodes: Insufficient arguments"
		return $FAIL
	fi
	var=$1
	if [ "$var" -eq $MIRRORED_ROOT ]
	then
		read mirror_count usr_fs < $MIRROR_INFO
		rm -f /dev/root
		rm -f /dev/rroot
		mknod /dev/root  b 25 0		> /dev/null 2>&1
		mknod /dev/rroot c 25 0		> /dev/null 2>&1
	fi
	chmod 440 	/dev/root		> /dev/null 2>&1
	chown root 	/dev/root		> /dev/null 2>&1
	chgrp backup 	/dev/root		> /dev/null 2>&1
	chmod 640 	/dev/rroot		> /dev/null 2>&1
	chown sysinfo 	/dev/rroot		> /dev/null 2>&1
	chgrp sysinfo 	/dev/rroot		> /dev/null 2>&1
	if [ "$var" -eq $MIRRORED_ROOT ]
	then
		if [ "$usr_fs" = "MIRROR_USR=yes" ]
		then
			rm -f /dev/usr
			rm -f /dev/rusr
			mknod /dev/usr   b 25 1	> /dev/null 2>&1
			mknod /dev/rusr  c 25 1	> /dev/null 2>&1
		fi
	fi
	chmod 440 	/dev/usr		> /dev/null 2>&1
	chown root 	/dev/usr		> /dev/null 2>&1
	chgrp backup 	/dev/usr		> /dev/null 2>&1
	chmod 640 	/dev/rusr		> /dev/null 2>&1
	chown sysinfo 	/dev/rusr		> /dev/null 2>&1
	chgrp sysinfo 	/dev/rusr		> /dev/null 2>&1
}

# Install On-line diagnostics and give the user
# an option to activate/deactivate them.

online_diag() {
 	# Install On-line diagnostics.
 	if [ -f /usr/lib/aodinst ]
 	then
 		/usr/lib/aodinst > /dev/null 2>&1
 	else
 		echo "
 	Warning: Online diagnostics, /usr/lib/aodinst missing.
 	Cannot install Online Diagnostics."
 		return
 	fi
 	# Option to deactivate Online diagnostics 
 	tmpdir="/tmp/"
	echo "
Online Diagnostics is a \"early warning\" system error 
detection package for Altos UNIX System V. It provides 
a convenient mechanism for:

- Gathering system error messages at a selected interval
- Reporting to root (via system mail) and/or remote site 
  via UUCP) about any errors that exceed selected 
  thresholds.

If you choose not to install Online Diagnostics now 
you can activate them at a later time, by running 
the  file /usr/lib/aodconfig to enter the Online 
Diagnostic's main configuration menu. Please check 
your Systems Administrator's Guide for more 
information on Online Diagnostics."

 	while :
 	do
 		echo "\nActivate Online Diagnostics now? (y/n) : \c" 
 		read ans
 		echo
 		case $ans in
 			y|Y|n|N) break
 				;;
 			*) echo "
 	Please answer 'y' or 'n'.
 	Press Return> to continue: \c"
 		   	read ignore
 				;;
 		esac
 	done
 	
 	if [ "$ans" = "N" -o "$ans" = "n" ]
 	then
         	# Get all of the current crontab entries
 		/usr/bin/crontab -l > $tmpdir$$.crontab 2> /dev/null
 		grep "/usr/lib/aodmon"  $tmpdir$$.crontab > $tmpdir$$.c_entry   
 	
  		# Filter out the Online diag. entry 		
 		grep -v "/usr/lib/aodmon" $tmpdir$$.crontab > $tmpdir$$.cronlst 
 		aodentry=`cat $tmpdir$$.c_entry`
 	
   		# Append a "#" to the Online entry and replace cron table
 		echo "# $aodentry" >> $tmpdir$$.cronlst
 		/usr/bin/crontab $tmpdir$$.cronlst 2> /dev/null
 		rm -f $tmpdir$$.*
 	fi
 }


 update_dflt_users() {
	
	TMPFILE=/tmp/dflt_users.tmp
	NEW_DFLT_USERS=/etc/auth/subsystems/dflt_users
	OLD_DFLT_USERS=/tmp/Old/etc/auth/subsystems/dflt_users
	count=0
	if [ ! -f "$OLD_DFLT_USERS" ]
	then
		# File dflt_users missing ! Just pick up the new one.
		return
	fi
	cat  $NEW_DFLT_USERS |
	while read name 
	do
		if [ $count -eq 0 ]
		then
			count=1
			list="$name"
		else
			list="$list|$name"
		fi
		echo $list  > $TMPFILE
	done
	read tmp_list <  $TMPFILE
	egrep -v  `echo $tmp_list` $OLD_DFLT_USERS > $TMPFILE 2> /dev/null
	cat $TMPFILE >> $NEW_DFLT_USERS
	rm -f $TMPFILE
}	
 

# Set up the keyboard environment on the hard disk, depending
# on what the user set it to in the initial part of the
# installation.

set_kbd() {
	
	if [ -f /tmp/"$kbd_info" ]
	then
	 	read kbd < /tmp/"$kbd_info"
	 	case "$kbd" in
	 		2) kbd_name=ukd.ibm  ;;
	 		3) kbd_name=fra.ibm  ;;
	 		4) kbd_name=ger.ibm  ;;
	 		5) kbd_name=ita.ibm  ;;
	 		6) kbd_name=spa.ibm  ;;
	 		*) echo "
	 rtsinstall: Unexpected entry in file /tmp/$kbd_info. Aborting
	 installation..."
	 		exit $FAIL;;
	 	esac
	 	echo "mapkey /usr/lib/keyboard/$kbd_name" >> /etc/bcheckrc
	 	rm -f /tmp/"$kbd_info"
	 fi
	 
}


# Update the value of NDISK in the /etc/conf/cf.d/stune file,
# as there may be more disks listed in the /etc/conf/cf.d/mscsi 
# file than allowed by the current value of NDISK.

update_ndisk() {
	
	NDISK_INFO=/tmp/ndisk.info
	
	if [ ! -f /etc/conf/cf.d/mscsi -o ! -f /etc/conf/cf.d/stune ]
	then
		echo "
	Error: /etc/conf/cf.d/mscsi & /etc/conf/cf.d/stune files 
	are missing. Aborting Upgrade..."
		return $FAIL
	fi
	
	ndisk=
	no_of_disks=`grep "Sdsk" /etc/conf/cf.d/mscsi | wc -l  2> /dev/null`
	grep "NDISK" /etc/conf/cf.d/stune > "$NDISK_INFO" 2> /dev/null
	read name ndisk < "$NDISK_INFO" > /dev/null 2>&1
	rm -f "$NDISK_INFO"
	
	if [ -z "$ndisk" -o "$no_of_disks" -gt "$ndisk" ]
	then
		/etc/conf/bin/idtune -f NDISK "$no_of_disks" > /dev/null 2>&1
		if [ $? -ne 0 ]
		then
			echo "
	Error: idtune failed. Couldn't update NDISK value in
	/etc/conf/cf.d/stune . Aborting Upgrade"
			return $FAIL
		fi
	fi
}



trap 'exit $FAIL' 1 2 3 15

TERM=ansi; export TERM

# main
# backup the installation /.profile
mv /.profile /tmp/prof

# We are still booted off the floppy kernel! The device nodes of
# root and usr may have some unexpected major/minor device numbers
# as far as the floppy kernel is concerned. So get rid of the nodes
# and re-create them. 

if [ -d /tmp/Old -a \( -f "$MIRROR_INFO" -o -f /tmp/"$version_info" \) ]
then
	create_tmp_nodes
	if [ $? -eq "$FAIL" ]
	then
		exit $FAIL
	fi
fi


mount $usrfs /usr > /dev/null 2>&1 || {
	echo "Failed to mount /usr. Cannot proceed with installation."
	exit $FAIL
}


# cleanup for PCU and startup scripts if upgrading
[ -d /tmp/Old ] && {
	su root -c "rm -f /etc/PCU/pcu.lockfile"
	su root -c "rm -rf /etc/rc?.d" > /dev/null 2>&1
}

# if upgrade; prompt user for restoring the /etc/conf directory
[ -d /tmp/Old ] && {

	# Delete all the online man pages to make sure that some
	# of the old copies are not preserved.
	rm -rf /usr/man/bin
	rm -rf /usr/man/cat.ADM
	rm -rf /usr/man/cat.C
	rm -rf /usr/man/cat.F
	rm -rf /usr/man/cat.HW
	rm -rf /usr/man/cat.M

	while :
	do
		echo "

The Upgrade procedure installs a new Link-kit in 
/etc/conf.  Do you wish to backup /etc/conf in 
/tmp/Old before proceeding? (y/n) \c"
		read ret
		case $ret in
			Y|y)	cd /
				find ./etc/conf -print | cpio -pdmu /tmp/Old > /dev/null 2>&1
				su root -c "rm -rf /etc/conf" > /dev/null 2>&1
				break ;;
			N|n)	cd /
				if [ -f ./etc/conf/cf.d/mscsi ]
				then
					find ./etc/conf/cf.d/mscsi -print | cpio -pdmu /tmp/Old > /dev/null 2>&1
				fi
				if [ -f ./etc/conf/cf.d/stune ]
				then
					find ./etc/conf/cf.d/stune -print | cpio -pdmu /tmp/Old > /dev/null 2>&1
				fi
				if [ -d ./etc/conf/init.d ]
				then
					find ./etc/conf/init.d -print | cpio -pdmu /tmp/Old > /dev/null 2>&1
				fi
				if [ -d ./etc/conf/node.d ]
				then
					find ./etc/conf/node.d -print | cpio -pdmu /tmp/Old > /dev/null 2>&1
				fi
				if [ -f ./etc/conf/cf.d/init.base ]
				then
					find ./etc/conf/cf.d/init.base -print | cpio -pdmu /tmp/Old > /dev/null 2>&1
				fi
 				if [ -f ./etc/conf/pack.d/vd/space.c ]
 				then
 					find ./etc/conf/pack.d/vd/space.c -print | cpio -pdmu /tmp/Old > /dev/null 2>&1
 				fi
				if [ -f ./etc/conf/sdevice.d/mdc ]
				then
					find ./etc/conf/sdevice.d/mdc -print | cpio -pdmu /tmp/Old > /dev/null 2>&1
				fi
				if [ -f ./etc/conf/sdevice.d/pa ]
				then
					find ./etc/conf/sdevice.d/pa -print | cpio -pdmu /tmp/Old > /dev/null 2>&1
				fi
				if [ -f ./etc/conf/sdevice.d/sio ]
				then
					find ./etc/conf/sdevice.d/sio -print | cpio -pdmu /tmp/Old > /dev/null 2>&1
				fi
				if [ -f ./etc/conf/cf.d/mdevice ]
				then
					find ./etc/conf/cf.d/mdevice -print | cpio -pdmu /tmp/Old > /dev/null 2>&1
				fi
				su root -c "rm -rf /etc/conf" > /dev/null 2>&1
				break ;;
			*)	echo "
Please answer 'y' or 'n'. Press <Return> to continue.\c"
				read an
				continue ;;
		esac
	done
}

while :
do
	echo "\nExtracting OS from the Utilities Tape (T1). Please wait..."
	echo ""

	# Add an extra iteration for any new host adapters
	# that are supported depending on the number of
	# Stp entries in the mscsi file for the floppy kernel.
	for num in 0 1 2 
	do
		tar tfb /dev/nrStp$num 20 > /dev/null 2>&1
		ddstat=$?
		if [ $ddstat != 0 ]
		then
			continue
		else
			break
		fi
	done

	cd /
	if [ $ddstat = 0 ]
	then
		tar xfb /dev/rStp$num 20 2> /dev/null
		tarstat=$?
	fi

	if [ $ddstat != 0 -o $tarstat != 0 ]
	then
	{
		rm /etc/perms/rts /etc/perms/ext 2> /dev/null
		echo "

Failed to extract tape. Please check the tape.
Enter 'q' <Return> to quit the installation, <Return> to retry: \c"
		read inkey
		case $inkey in
			q)	exit $FAIL	;;
			*)	continue	;;
		esac
	}
	fi
	break
done

echo "Remove the Utilities Tape (T1) and press <Return> to continue:\007\c"
read anykey
echo ""

sed "s!AUTOBOOT=NO!AUTOBOOT=YES!" /etc/default/boot > /tmp/dboot
mv /tmp/dboot /etc/default/boot 2> /dev/null

# Set up the keyboard environment on the hard disk
set_kbd

if [ ! -d /tmp/Old ]
then
	# /usr/lib/mkdev/perms/HD was updated in hdinit script.
	# Create the device nodes here.
	fixperm -c /usr/lib/mkdev/perms/HD > /dev/null 2>&1
fi

if [ -d /tmp/Old ]
then
{
	update_dflt_users
 	# For UPS shutsave.
	if [ -f /tmp/Old/etc/default/boot ]
	then
		grep "RESTART" /tmp/Old/etc/default/boot >> /etc/default/boot
		grep "RSPART"  /tmp/Old/etc/default/boot >> /etc/default/boot
	fi
	cp /tcb/files/auth/r/root /tmp > /dev/null 2>&1
	cd /tmp/Old
	find ./tcb/files/auth -print | cpio -pdmu / > /dev/null 2>&1
	find ./etc/conf/init.d -print | cpio -pdmu / > /dev/null 2>&1
	cpio -pdmu / < /fcopylist > /dev/null 2>&1

	# These files shouldn't exist in releases >= 2.0
	rm -f /usr/lib/grafinfo/altos/avga.xgi
	rm -f /usr/lib/grafinfo/altos/avgani.xgi
	rm -f /usr/lib/mkdev/scsi
	# Do not retreive the perms file if the upgrade is from
	# a version prior to 2.0aC0 (New major device numbering scheme).
	if [ ! -f /tmp/"$version_info" ]
	then
		# Upgrade from 2.0aC0 ----> 2.*
		cp /tmp/Old/usr/lib/mkdev/perms/HD /usr/lib/mkdev/perms/HD > /dev/null 2>&1
		cp /tmp/Old/etc/conf/cf.d/mscsi /etc/conf/cf.d/mscsi > /dev/null 2>&1
		cp /tmp/Old/etc/default/filesys /etc/default/filesys > /dev/null 2>&1
		cp /tmp/Old/etc/checklist /etc/checklist > /dev/null 2>&1
		update_ndisk
		if [ $? -eq "$FAIL" ]
		then
			exit $FAIL
		fi
		rm -f /tmp/"$version_info"
	fi
 	# Upgrade on a system with a mirrored root filesystem.
 	if [ -f "$MIRROR_INFO" ]
 	then
 		cp /tmp/Old/etc/conf/pack.d/vd/space.c /etc/conf/pack.d/vd/space.c  > /dev/null 2>&1
 	fi
	cp /tmp/root /tcb/files/auth/r/root > /dev/null 2>&1
	su root -c "rm -f /etc/default/filesys.u"
} else  {
	if [ -b /dev/u -a -c /dev/ru ]
	then
		su root -c "mv /etc/default/filesys.u /etc/default/filesys"
	else
		su root -c "rm -f /etc/default/filesys.u"
	fi
}
fi

su root -c "rm -f /fcopylist"

# need to be done before setting permsissions
cd /tmp
sh init.mmdf
sh init.aom
cd /

echo "Creating device files..."
echo ""
/etc/fixperm -c /etc/perms/[c-z]* > /dev/null 2>&1

# Install On-line diagnostics.
online_diag

# link rct0, nrct0 and xct0 to the device that is installed.
rm -f /dev/rct0
rm -f /dev/nrct0
rm -f /dev/xct0
rm -f /dev/rmt/0b
rm -f /dev/rmt/0bn
ln /dev/rStp$num /dev/rct0
ln /dev/rStp$num /dev/rmt/0b
ln /dev/nrStp$num /dev/nrct0
ln /dev/nrStp$num /dev/rmt/0bn
ln /dev/xStp$num /dev/xct0

if [ -f "$MIRROR_INFO" ]
then
	create_nodes $MIRRORED_ROOT
	stat=$?
else
	create_nodes $NON_MIRRORED_ROOT
	stat=$?
fi
if [ "$stat" -eq "$FAIL" ]
then
	exit $FAIL
fi

init="init.rts init.link init.calendar"
cd /tmp
for initfiles in $init
do	[ -f "$initfiles" ] || continue
	sh $initfiles
	if [ $? != 0 ]
	then
		exit $FAIL
	fi
done

echo "\n
Resolving any possible discrepancies between protected
password database entries and their system database
entries..."
echo "y" | /tcb/bin/authck -s > /dev/null 2>&1

trap '' 1 2 3 15

exit $OK
