
AORB=""; export AORB
DEV=""; export DEV
HOW="force"; export HOW
MEDIA=""; export MEDIA
FS=/dev/dsk/c1d0s0
cd /opus
REV="`grep '^' LATEST.REV`"
if [ -z "${REV}" ];
	then REV="G4"
fi
/opus/bin/opdos asciiread /opus/genesis > /dev/null 2>&1
if [ $? -eq 0 ]; then
	GeNeSiS=1; export GeNeSiS
else
	GeNeSiS=""; export GeNeSiS
fi

AUTO=""; export AUTO
/opus/bin/opdos asciiread /opus/inst_all > /dev/null 2>&1
RTN1=$?
/opus/bin/opdos asciiread /opus/inst_os > /dev/null 2>&1
RTN2=$?
if [ $RTN1 -eq 0 -a $RTN2 -ne 0 ]; then
	AUTO=ALL
elif [ $RTN1 -ne 0 -a $RTN2 -eq 0 ]; then
	AUTO=OS
fi

exit_bad()
{
	trap "" 1 2 3 15
	cd /
	umount $FS
	echo "\n\nInstallation of release $REV root file system"
	echo "on $FS complete FAILED."
	echo "\tPlease see manual."
	sync
	if [ $GeNeSiS ]; then 
		/opus/bin/opdos del /opus/genesis > /dev/null 2>&1
		/opus/bin/opdos del /opus/opfs/opfil10 > /dev/null 2>&1
	fi
	/opus/bin/opdos exit 8
}

exit_good()
{
	trap "" 1 2 3 15
	cd /
	umount $FS
	echo "\n\nInstallation of release $REV root file system"
	echo "on $FS complete."
	echo "\nOpus5 will now be booted from the root file system."
	sync
	if [ $GeNeSiS ]; then 
		/opus/bin/opdos del /opus/genesis > /dev/null 2>&1
		/opus/bin/opdos del /opus/opfs/opfil10 > /dev/null 2>&1
	fi
	/opus/bin/opdos exit 1
}

cd /
echo "\nOpus5 Rev ${REV} installation on $FS\n"
/opus/bin/opdos asciiread mkfscmd > mkfscmd
BLOCKS=""
if [ ! "$GeNeSiS" ]; then
	if [ ! -f mkfscmd ] ; then
		echo "How many blocks are allocated for the root file system? \c"
		read BLOCKS
		echo
		if [ "$BLOCKS" -le 7000 ] ; then
			echo "Insufficient disk space available to install Opus5."
			exit 4
		fi
	fi
	if [ "$BLOCKS" -o "`grep mkfs mkfscmd 2>/dev/null`" ]; then
		echo "Are you ready to initialize your root file system?"
		echo "  (This will create or overwrite the root file system!) \c"
		echo "[y-n] \c"
		read ANS
		if [ "$ANS" != y  -a "$ANS" != Y ]; then
			mount $FS /install > /dev/null 2>&1
			if [ $? -ne 0 ] ; then
				echo "\n\007$0: mount of $FS on /install failed, trying fsck...\n"
				fsck -y $FS
				if [ $? -ne 0 ]; then
					echo "\n\007fsck failed."
					exit 4
				fi
				mount $FS /install > /dev/null 2>&1
				if [ $? -ne 0 ]; then
					echo "\n\007$0: mount of $FS on /install failed"
					exit 4
				fi
			fi
			if [ ! -d /install/save ]; then
				mkdir /install/save
			fi
			cd /
			find bin dev etc tmp opus shlib -print | cpio -pdmvl /install/save
			find bin dev etc tmp opus shlib -print | cpio -pdmvu /install
			umount $FS
			sync
			echo "Critical files copied to root file system."
			echo "\nOpus5 will now be booted from the root file system.\n"
			/opus/bin/opdos exit 1
		fi
	fi
fi
if [ ! -f mkfscmd ]; then
	mkfs $FS $BLOCKS
elif [ $GeNeSiS ]; then
	sh mkfscmd
	if [ $? -ne 0 ]; then
		exit_bad
	fi
elif [ "`grep mkfs mkfscmd`" ]; then
	sh mkfscmd
	if [ $? -ne 0 ] ; then
		/opus/bin/opdos exit 6
	fi
fi
# Label the root file system
/etc/labelit $FS root "Rev:${REV}" > /dev/null 2>&1
if [ $? -ne 0 ]; then
	echo "\n$0: labelit of $FS failed"
	exit_bad
fi

mount $FS /install > /dev/null 2>&1
if [ $? -ne 0 ] ; then
	echo "\n\007$0: mount of $FS on /install failed, trying fsck...\n"
	fsck -y $FS
	if [ $? -ne 0 ]; then
		echo "\n\007fsck failed."
		exit_bad
	fi
	mount $FS /install > /dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo "\n\007$0: mount of $FS on /install failed."
		exit_bad
	fi
fi

if [ $GeNeSiS ]; then 
	for CNT in 1 2 3 4 5 6; do
		GREP=`grep "mkfs.*c1d0s$CNT" mkfscmd`
		if [ "$GREP" ]; then
			/etc/labelit /dev/dsk/c1d0s$CNT u$CNT "Rev:${REV}" > /dev/null 2>&1
			if [ $? -ne 0 ]; then
				echo "\n$0: labelit of /dev/dsk/c1d0s$CNT failed"
				exit_bad
			fi
			sync
			umask 002
			su bin -c "mkdir /install/u$CNT"
		fi
	done
fi

echo "\n"
while :; do
	if [ $AUTO ]; then
		trap : 1 2 3 15
		tapeload
		RTN=$?
		break
	fi
	echo "\n\nWould you like to load from diskette or tape?"
	echo "------------------------------------------------------------------------------"
	echo "\nChoose:  0) Quit"
	echo "         1) Diskette"
	echo "         2) Tape"
	echo "\nEnter [0-2]: \c"
	read ANS
	case $ANS in
		0)
			exit 0;;
		1)
			trap "sync; exit_bad" 1 2 3 15
			while :; do
				echo "\nFrom which device would you like to load [/dev/flpa]? \c"
				read DEV
				case $DEV in
					""|/dev/flp[aA]|/dev/rflp[aA])
						AORB="A"
						DEV=/dev/flpa
						MEDIA=diskette
						echo "\nThe CORE files will now be loaded."
						/opus/bin/loadflp
						RTN=$?
						break 2;;
					/dev/flp[bB]|/dev/rflp[bB])
						AORB="B"
						DEV=/dev/flpb
						echo "\nThe CORE files will now be loaded."
						/opus/bin/loadflp
						RTN=$?
						break 2;;
					/dev/rmt/[01]m)
						MEDIA=tape
						if [ $DEV = /dev/rmt/0m ]; then
							AORB="0"
							CFG_DEV=0
						else
							AORB="1"
							CFG_DEV=1
						fi
						TAPE_STAT=`/opus/bin/opdos asciiread \\\opus\\\opus.cfg | grep "^<mt/${CFG_DEV}="`
						if [ "$TAPE_STAT" = "" ]; then
							echo "\007There is no entry in opus.cfg for the device $DEV"
						else
							echo "\nThe CORE files will now be loaded."
							/opus/bin/loadflp
							RTN=$?
							break 2
						fi;;
					*)
						echo "\007Invalid response - must be a valid floppy or tape device.";;
				esac
			done;;
		2)
			trap : 1 2 3 15
			tapeload
			RTN=$?
			break;;
		*)
			echo "\n\007Invalid response."
			sleep 1;;
	esac
done

if [ $RTN -eq 0 ] ; then
	exit_good
else
	exit_bad
fi
