#!/bin/sh -euk

### This is the second script file in the Athena workstation
### installation program.  It is called by the first script,
### 'install.sh', which resides on the installation media.  This
### script is a continuation of 'install.sh' but resides on the
### installation RVD pack instead.  It is executed as soon as
### 'install.sh' has spun up and mounted the the installation RVD
### pack.  This division of the installation program exists to make
### modification easier.  If something in this script needs to be
### modified, every installation floppy and tape need not be
### rewritten.

### $Header: /afs/rel-eng.athena.mit.edu/project/release/current/source/athena/config/files/Ultrix/Ultrix-4.2/RCS/phase2.sh,v 1.5 91/09/17 11:38:59 epeisach Exp $
### $Locker:  $

PATH=/srvd/etc/install:/srvd/bin:/srvd/bin/athena:/srvd/etc:/bin:/etc:
export PATH

# Get time set correctly before starting name service to avoid timeouts
# Not needed anymore!
#/srvd/etc/athena/gettime -s kerberos.mit.edu

SITE=site
CPP=-p

if [ $MACH = "DS3100" ]; then
	SITE=var ; export SITE
#	CPP=	; export CPP
fi

case $MACH in
VS2000)
	disktype=rd32
	diskdriver=rd0
	;;
VS3100|DS3100)
	disktype=rz23
	diskdriver=rz3
	cd /dev; ./MAKEDEV rz3
	;;
DS5000*)
	echo ""
	echo "The following drives are available to use:"
	machtype -r
	echo -n "Please select drive to install on: "
	read diskdriver
	if [ "$diskdriver" = "" ]; then
		echo "Invalid drive specified"
		echo ""
		exec /srvd/install/phase2.sh
	fi
	disktype=`machtype -r | grep $diskdriver: | awk -F: '{print \$2}'`
	if [ "$disktype" = "" ]; then
		echo "Invalid drive specified"
		echo ""
		exec /srvd/install/phase2.sh
	fi

	cd /dev; ./MAKEDEV $diskdriver
	KERNEL=.5000
	SITE=var ; export SITE
	;;
VS2)
	disktype=`machtype -r | /bin/slowgrep ra0:`
	diskdriver=ra0
	;;
VAX7*)
	disktype=`machtype -r | /bin/slowgrep ra0:`
	diskdriver=ra0
	;;
VAX650)
	disktype=`machtype -r | /bin/slowgrep ra0:`
	diskdriver=ra0
	;;
RT*)
	disktype=""
	diskdriver=hd0
	MACH=RTPC
	;;
	esac

# Make a faster and bigger /tmp.  For floppy based systems, this means
# putting it on the swap partition of the hard disk.
echo
case $media in
rx33 | rx50 | rt*)
	echo "Creating a file system for /tmp..."
	/etc/umount /tmp > /dev/null 2>&1
	mkfs /dev/${diskdriver}b 1000
	/etc/mount /dev/${diskdriver}b /tmp
	;;
esac

/etc/umount /root > /dev/null 2>&1
/etc/umount /${SITE} > /dev/null 2>&1

case $MACH in
VS3100)
	echo "Installing the new bootblocks..."
       	dd if=/srvd/install/bootblocks.vs3100 of=/dev/rz3a
	;;
DS*)
	echo "Installing the new bootblocks..."
       	dd if=/srvd/usr/mdec/bootblks of=/dev/${diskdriver}a
	;;
VS2)
	echo "Installing the new bootblocks..."
	dd if=/srvd/install/bootblocks.vs2 of=/dev/ra0a
	;;
VAX7*)
	echo "Installing the new bootblocks..."
	dd if=/srvd/install/bootblocks.vax750 of=/dev/ra0a
	;;
VAX650)
	echo "Installing the new bootblocks..."
	dd if=/srvd/install/vaxboot of=/dev/ra0a
	;;
VS2000)
	echo "Installing the new bootblocks..."
	dd if=/srvd/install/bootblocks.vs2000 of=/dev/rd0a
	;;
	esac

echo "Making filesystems..."
echo "   Don't worry if there is a warning message which says,"
echo "       'Warning: <number> sectors unallocated...'"
echo ""
case $MACH in
DS*|VS*|VAX*)
	newfs -v -n /dev/r${diskdriver}a $disktype
	;;
RT*)
	newfs -v /dev/r${diskdriver}a
	;;
esac

if [ ${save_site_flag}X = falseX ]
	then
	case $MACH in
VAX7*)
		echo "Creating /tmp and /site filesystems on hard disk..."
		newfs -v /dev/rra0d $disktype
		newfs -v /dev/rra0e $disktype
		;;
DS*)
		echo "Adding partition tables to disk"
		chpt -pa 0 32768 -pb 32768 98304 -pg 131072 73792 /dev/r${diskdriver}c
		newfs -v -n /dev/r${diskdriver}a $disktype
		newfs -v /dev/r${diskdriver}g $disktype
		;;
*)
		newfs -v /dev/r${diskdriver}g $disktype
		;;
	esac
	else
		echo ""
		echo "The site filesystem will be preserved"
  	fi

echo ""
echo "Mounting hard disk's root partition..."
/etc/umount /root > /dev/null 2>&1
/etc/umount /${SITE} > /dev/null 2>&1
/etc/mount /dev/${diskdriver}a /root
mkdir /root/${SITE}
if [ ${save_site_flag}X = falseX ]; then
	/etc/mount /dev/${diskdriver}g /${SITE}
	fi

echo "Copying file system from installation srvd to /dev/${diskdriver}a..."
echo "Running 'track'..."
/srvd/usr/athena/etc/track -d -F /srvd -T /root -W /srvd/usr/athena/lib
cd /root/dev
case "${MACH}" in
DS*)
	if [ ${MACH} = "DS5000_100" ]; then
		./MAKEDEV scc0
	fi
	/srvd/dev/MAKEDEV DECstation xcons
	chmod 662 xcons
	chgrp daemon xcons
	rm -f floppy
	/srvd/dev/MAKEDEV `/bin/machtype -r | awk -F: '{print \$1}'`
	floppy=`/bin/machtype -r | grep "RX23"| awk -F: '{print \$1}'`
	if [ ${floppy}x != 'x' ]; then
		echo "Creating floppy links"
		rm -f ${floppy} r${floppy} floppy rfloppy
		./MAKEDEV ${floppy}
		chmod 666 ${floppy}a
		chmod 666 r${floppy}a
		ln -s ${floppy}a floppy
		ln -s r${floppy}a rfloppy
	fi
	;;
*)
	/srvd/dev/MAKEDEV ws
	;;
esac

cd /root

echo "Creating other files/directories on the pack's root..."
mkdir afs mit mnt u1 u2 u3 
ln -s /${SITE}/tmp tmp
cp ${CPP} /srvd/*boot /srvd/.??* /root/
if [ -f /srvd/bin/athena/aklog -a ! -f /root/bin/athena/aklog ]; then
        cp ${CPP} /srvd/bin/athena/aklog /root/bin/athena/
fi

echo "/dev/${diskdriver}a / 4.2 rw 1 1"     > /root/etc/fstab
echo "/dev/${diskdriver}g /site 4.2 rw 1 2" >> /root/etc/fstab
case $MACH in
DS*)
	cp /srvd/vmunix${KERNEL} /root/vmunix
	cp /srvd/ultrixboot /root/
	echo "/dev/${diskdriver}a:/:rw:1:1:ufs::"		 > /root/etc/fstab
	echo "/dev/${diskdriver}g:/var:rw:1:2:ufs::"	 >> /root/etc/fstab
	;;
VS3100|VAX650)
	cp -p /srvd/vmb.exe /root/
	cp -p /srvd/vmunix /root/vmunix
	;;
VS2000)
	cp -p /srvd/vmb.exe /root/
	cp -p /srvd/vmunix.VS2000 /root/vmunix
	;;
VS2)
	cp -p /srvd/vmb.exe /root/
	cp -p /srvd/vmunix.VS2 /root/vmunix
	;;
VAX7*)
	echo "/dev/ra0a / 4.2 rw 1 1"		> /root/etc/fstab
	echo "/dev/ra0d /tmp 4.2 rw 1 2"	>>/root/etc/fstab
	echo "/dev/ra0e /site 4.2 rw 1 3"	>>/root/etc/fstab
	cp -p /srvd/boot /root/boot
	cp -p /srvd/pcs750.bin /root/
	cp -p /srvd/vmunix.VAX750 /root/vmunix
	;;
RT*)
	cp -p /srvd/vmunix /root/
	;;
	esac


PATH=/root/bin:/root/bin/athena:/root/etc:/bin:/etc

if [ ${save_site_flag}X = falseX ]; then
   echo "Building "${SITE}" from scratch"
   cd /${SITE}
case "${MACH}" in
DS*)
	for i in tmp usr usr/tmp adm adm/crash adm/syserr adm/lmf \
		lib spool \
		spool/mqueue \
		crash local preserve \
		vice vice/cache vice/etc
	  do mkdir $i
	  done
	ln -s /var/vice /var/usr/vice
	ln -s /var /root/usr/var
	chown daemon spool/mqueue
	for i in X0msgs acct elcsdlog shutdownlog sulog wtmp ../spool/mqueue/syslog
		do cp /dev/null /${SITE}/adm/$i
		done
	  for i in \
	vice/etc/cacheinfo vice/etc/ThisCell vice/etc/CellServDB \
	vice/etc/SuidCells
	    do cp ${CPP} /srvd/${SITE}/usr/$i /${SITE}/usr/$i
	done
	chown daemon /${SITE}/spool/mqueue/syslog
	chgrp daemon /${SITE}/spool/mqueue/syslog
	ln -s /dev/null /var/adm/lastlog
	;;
*)
	for i in tmp usr usr/tmp usr/adm \
		usr/lib usr/spool usr/spool/mqueue \
		usr/crash usr/local usr/msgs usr/preserve \
		usr/vice usr/vice/cache usr/vice/etc
	  do mkdir $i
	done
	for i in X0msgs messages msgbuf shutdownlog sulog wtmp ../spool/mqueue/syslog
	   do cp /dev/null /${SITE}/usr/adm/$i
	done
	for i in lib/crontab lib/sendmail.cf \
		vice/etc/cacheinfo vice/etc/ThisCell vice/etc/CellServDB \
		vice/etc/SuidCells
	   do cp ${CPP} /srvd/${SITE}/usr/$i /${SITE}/usr/$i
	done
	ln -s /dev/null /site/usr/adm/lastlog
	echo 1 0 > /site/usr/msgs/bounds
	chmod 666 /site/usr/msgs/bounds
	;;
esac

  chmod 1777 tmp usr/tmp
  mv /${SITE}/usr/vice/etc/CellServDB /${SITE}/usr/vice/etc/CellServDB.public
  ln -s CellServDB.public /${SITE}/usr/vice/etc/CellServDB
fi

echo "Editing/installing configuration files..."
cd /root
for i in motd mtab named.local utmp X0.hosts;
	do
	   cp /dev/null /root/etc/$i
	done
for i in crontab elcsd.conf passwd group sendmail.cf athena/attach.conf inetd.conf syslog.conf athena/newsyslog.conf;
	do
	   cp ${CPP} /srvd/etc/$i /root/etc/$i
	done
ln -s /srvd/etc/ftpusers /root/etc/ftpusers
sed -e "s/MIT.EDU/$domain/g" < /srvd/etc/resolv.conf > /root/etc/resolv.conf
sed -e 	"s/^HOST=NONAME/HOST=$hostname/
	s/^ADDR=NOADDR/ADDR=$netaddress/
	s/^NETDEV=NODEV/NETDEV=$NETDEV/
	s/^MACHINE=[^;]*/MACHINE=$MACH/" \
	< /srvd/etc/athena/rc.conf > /root/etc/athena/rc.conf

cp /srvd/etc/ttys /root/etc/ttys
disp=`machtype -d`
case "${disp}" in
QVSS|SM)
	sed -n	-e '/qvss/s/^#//' \
		-e p /srvd/etc/athena/login/config \
		> /root/etc/athena/login/config
	rm -f /root/etc/X; ln -s Xqvss /root/etc/X
	;;
QDSS|SG)
	sed -n	-e '/qdss/s/^#//' \
		-e p /srvd/etc/athena/login/config \
		> /root/etc/athena/login/config
	rm -f /root/etc/X; ln -s Xdvss /root/etc/X
	;;
PLX)
	sed -n	-e '/plx/s/^#//' \
		-e p /srvd/etc/athena/login/config \
		> /root/etc/athena/login/config
	rm -f /root/etc/X; ln -s Xplx /root/etc/X
	;;
apa16*|megapel*|*plx*)
	rm -f /root/etc/X; ln -s Xibm /root/etc/X
	;;
PM*)
	XSERVER=Xws
	XSERVERPATH=/usr/bin
	sed -n	-e '/Xws/s/^#//' \
		-e '/^console/s/^/#/' \
		-e '/^#ULTconsole/s/^#ULT//' \
		-e '/^login/s/^/#/' \
		-e '/^#ULT75login/s/^#ULT75//' \
		-e p /srvd/etc/athena/login/config \
		> /root/etc/athena/login/config
		rm -f /root/etc/X; ln -s Xws /root/etc/X
		;;

*)
	sed -n	-e '/^#console.*getty/s/#//' \
		-e '/^console.*dm /s/^/#/' \
		-e 's/CONSOLE/vt100/' \
		-e p /srvd/etc/ttys > /root/etc/ttys
	rm -f /root/etc/X
	;;
esac

if [ ${XSERVER}x != x ]; then
	echo "Copying X server from" ${XSERVERPATH}/${XSERVER} "to" /etc
	rm -f /etc/${XSERVER}
	/srvd/bin/cp -p /srvd/${XSERVERPATH}/${XSERVER} /root/etc
	fi

echo "Updating version number..."
rm -f /root/.rvdinfo
echo installed on `date` > /root/etc/athena/version
sed -n -e '1s/RVD/Workstation/' \
	-e '1s/DECMIPS/'$MACH'/p' \
	-e '1s/MIPS/'$MACH'/p' /srvd/.rvdinfo >> /root/etc/athena/version
#echo "Athena Install ($MACH) Version 0.0A `date`" >> /root/etc/athena/version

echo "Unmounting filesystems and checking them"
cd /
/etc/umount /root > /dev/null 2>&1
/srvd/etc/fsck -p /dev/r${diskdriver}a
/etc/umount /${SITE} > /dev/null 2>&1
/srvd/etc/fsck -p /dev/r${diskdriver}g

# set success flag
echo > /tmp/success
exit 0
