:
# SCCSID(@(#)dosinst.sh	1.2 LCC) Merge 3.1 branch Modified 18:59:39 4/19/93
# SCCSID(@(#)dosinst.sh	1.37 LCC) Modified 21:09:45 3/5/93
#############################################################################
# Installation script for MS DOS and DR DOS
#############################################################################
#
#	Copyright (c) 1991-1993 Locus Computing Corporation.
#	All rights reserved.
#	This is an unpublished work containing CONFIDENTIAL INFORMATION
#	that is the property of Locus Computing Corporation.
#	Any unauthorized use, duplication or disclosure is prohibited.
#
#############################################################################

#DOSINSTALLTRACE=1

EXIT_NOW()
{
	rm -f /usr/merge/doing_dosinst
	exit $1
}

Setup_MSDOS33()
{
	# This function sets various file lists that other parts of
	# this install script uses.
	# This function sets the file lists for standard MS DOS 3.3
	# which normally is on two 5.25 inch double density disks.
	echo "Setup_MSDOS33" >> $TRACE

	# The following two lists "Dosopt_NB_List" and "Dosopt_B_List" contain
	# the filenames of the standard MS DOS 3.3 files that are to be made
	# executable from UNIX.  When exectuable from UNIX, they must also
	# be "dosopted".  The "Dosopt_NB_List" are those that are to be
	# dosopted "not-behaved" (i.e. display oriented).  The "Dosopt_B_List"
	# are those that are to be dosopted "behaved" (i.e. stream oriented).

	# List of files to dosopt "not behaved".
	Dosopt_NB_List="	\
		gwbasic.exe	\
		command.com	\
		restore.com	\
		"

	# List of files to dosopt "behaved".
	Dosopt_B_List="		\
		backup.com	\
		chkdsk.com	\
		comp.com	\
		debug.com	\
		diskcomp.com	\
		diskcopy.com	\
		edlin.com	\
		exe2bin.exe	\
		fc.exe		\
		find.exe	\
		format.com	\
		link.exe	\
		more.com	\
		print.exe	\
		recover.com	\
		replace.exe	\
		select.com	\
		sort.exe	\
		sys.com		\
		tree.com	\
		xcopy.exe	\
		"

	# List of the files in Dosopt_NB_List and Dosopt_B_List that
	# we want to be executable from unix with their "short" name
	# (i.e. without the ".exe" or ".com" extention).
	X_Shortname_List="	\
		gwbasic.exe	\
		command.com	\
		restore.com	\
		backup.com	\
		chkdsk.com	\
		comp.com	\
		debug.com	\
		diskcomp.com	\
		diskcopy.com	\
		edlin.com	\
		exe2bin.exe	\
		fc.exe		\
		find.exe	\
		link.exe	\
		more.com	\
		print.exe	\
		recover.com	\
		replace.exe	\
		select.com	\
		sort.exe	\
		sys.com		\
		tree.com	\
		xcopy.exe	\
		"

	# Built-In DOS commands.
	# These are the commands that are built into command.com, which are
	# stream-oriented and which we want to be executable from UNIX.

	X_BI_F="copy"
	X_BI_List="		\
		del		\
		dir		\
		erase		\
		for		\
		md		\
		rd		\
		ren		\
		rename		\
		time		\
		type		\
		ver		\
		vol		\
		"
	# Note: Don't want built-in "date" to be run from unix.

	Delete_List=""
	Hide_List=""
}

Setup_MSDOS50()
{
	# This function sets various file lists that other parts of
	# this install script uses.
	# This function sets the file lists for standard MS DOS 5.0.
	# which normally is on two 5.25 inch double density disks.
	echo "Setup_MSDOS50" >> $TRACE

	# The following two lists "Dosopt_NB_List" and "Dosopt_B_List" contain
	# the filenames of the standard MS DOS 5.0 files that are to be made
	# executable from UNIX.  When exectuable from UNIX, they must also
	# be "dosopted".  The "Dosopt_NB_List" are those that are to be
	# dosopted "not-behaved" (i.e. display oriented).  The "Dosopt_B_List"
	# are those that are to be dosopted "behaved" (i.e. stream oriented).

	# List of files to dosopt "not behaved".
	Dosopt_NB_List="	\
		qbasic.exe	\
		gorilla.bas	\
		money.bas	\
		nibbles.bas	\
		remline.bas	\
		edit.com	\
		dosshell.com	\
		dosshell.exe	\
		command.com	\
		restore.exe	\
		"

	# List of files to dosopt "behaved".
	Dosopt_B_List="		\
		backup.exe	\
		chkdsk.exe	\
		comp.exe	\
		debug.exe	\
		diskcomp.com	\
		diskcopy.com	\
		edlin.exe	\
		exe2bin.exe	\
		expand.exe	\
		fc.exe		\
		find.exe	\
		format.com	\
		help.exe	\
		more.com	\
		print.exe	\
		recover.exe	\
		replace.exe	\
		sort.exe	\
		sys.com		\
		tree.com	\
		xcopy.exe	\
		"

	# List of the files in Dosopt_NB_List and Dosopt_B_List that
	# we want to be executable from unix with their "short" name
	# (i.e. without the ".exe" or ".com" extention).
	X_Shortname_List="	\
		qbasic.exe	\
		gorilla.bas	\
		money.bas	\
		nibbles.bas	\
		remline.bas	\
		edit.com	\
		dosshell.com	\
		dosshell.exe	\
		command.com	\
		restore.exe	\
		backup.exe	\
		chkdsk.exe	\
		comp.exe	\
		debug.exe	\
		diskcomp.com	\
		diskcopy.com	\
		edlin.exe	\
		exe2bin.exe	\
		expand.exe	\
		fc.exe		\
		find.exe	\
		help.exe	\
		more.com	\
		print.exe	\
		recover.exe	\
		replace.exe	\
		sort.exe	\
		sys.com		\
		tree.com	\
		xcopy.exe	\
		"

	# Built-In DOS commands.
	# These are the commands that are built into command.com, which are
	# stream-oriented and which we want to be executable from UNIX.

	X_BI_F="copy"
	X_BI_List="		\
		del		\
		dir		\
		erase		\
		for		\
		md		\
		rd		\
		ren		\
		rename		\
		time		\
		type		\
		ver		\
		vol		\
		"
	# Note: Don't want built-in "date" to be run from unix.

	# Files to delete.
	Delete_List="	\
		autoexec.bat	\
		config.sys	\
		disk		\
		setup.exe	\
		setup.ini	\
		packing.lst	\
		readme.1st	\
		"

	# Files to hide.
	Hide_List="	\
		emm386.exe	\
		smartdrv.sys	\
		wina20.386	\
		"
	# Need to run the expand program as a part of installation.
	DO_EXPAND=do_expand

	###########################################
}

Setup_DRDOS60()
{
	# This function sets various file lists that other parts of
	# this install script uses.
	# This function sets the file lists for standard DR DOS 6.0.
	# which normally is on two 5.25 inch high density disks.
	echo "Setup_DRDOS60" >> $TRACE

	# The following two lists "Dosopt_NB_List" and "Dosopt_B_List" contain
	# the filenames of the standard DR DOS 6.0 files that are to be made
	# executable from UNIX.  When exectuable from UNIX, they must also
	# be "dosopted".  The "Dosopt_NB_List" are those that are to be
	# dosopted "not-behaved" (i.e. display oriented).  The "Dosopt_B_List"
	# are those that are to be dosopted "behaved" (i.e. stream oriented).

	# List of files to dosopt "not behaved".
	Dosopt_NB_List="	\
		command.com	\
		restore.com	\
		"

	# List of files to dosopt "behaved".
	Dosopt_B_List="		\
		backup.com	\
		chkdsk.com	\
		comp.com	\
		diskcomp.com	\
		diskcopy.com	\
		exe2bin.exe	\
		fc.exe		\
		find.exe	\
		format.com	\
		filelink.exe	\
		print.exe	\
		recover.com	\
		replace.exe	\
		sort.exe	\
		sys.com		\
		tree.com	\
		xcopy.exe	\
		xdir.exe	\
		"

	# List of the files in Dosopt_NB_List and Dosopt_B_List that
	# we want to be executable from unix with their "short" name
	# (i.e. without the ".exe" or ".com" extention).
	X_Shortname_List="	\
		command.com	\
		restore.com	\
		backup.com	\
		chkdsk.com	\
		comp.com	\
		diskcomp.com	\
		diskcopy.com	\
		exe2bin.exe	\
		fc.exe		\
		find.exe	\
		filelink.exe	\
		print.exe	\
		recover.com	\
		replace.exe	\
		sort.exe	\
		sys.com		\
		tree.com	\
		xcopy.exe	\
		xdir.exe	\
		"

	# Built-In DOS commands.
	# These are the commands that are built into command.com, which are
	# stream-oriented and which we want to be executable from UNIX.

	X_BI_F="copy"
	X_BI_List="		\
		del		\
		delq		\
		dir		\
		era		\
		eraq		\
		erase		\
		for		\
		md		\
		rd		\
		ren		\
		rename		\
		time		\
		type		\
		ver		\
		vol		\
		"
	# Note: Don't want built-in "date" to be run from unix.

	# Files to delete.
	# so remove them.  The other files have no functionality with Merge.
	Delete_List="		\
		autoexec.bat	\
		config.sys	\
		emm386.sys	\
		emmxma.sys	\
		install.exe	\
		install.ini	\
		startup		\
		vmax		\
		utils_1		\
		utils_2		\
		utils_3		\
		utils_4		\
		utils_5		\
		"

	# Files to hide.
	# Note: subst, join and assign are not currently supported with
	# DR-DOS when the PCI bridge is in use. (i.e. they don't work).
	# They can be useful when booting from a floppy, so leave them
	# around, but hidden, so they can be copied to a floppy.
	# Note: "subst.exe" is copied to the snap disk when making images.
	Hide_List="	\
		subst.exe	\
		join.exe	\
		assign.com	\
		"
}

Setup_mini_DRDOS60()
{
	# This function sets various file lists that other parts of
	# this install script uses.
	# This function sets the file lists for minimum DR DOS 6.0.
	# With minimum, we don't have very many files executable from UNIX.
	# Also, only the files we want are on the disk, so don't have
	# to remove any.
	echo "Setup_DRDOS60" >> $TRACE

	# The following two lists "Dosopt_NB_List" and "Dosopt_B_List" contain
	# the filenames of the standard DR DOS 6.0 files that are to be made
	# executable from UNIX.  When exectuable from UNIX, they must also
	# be "dosopted".  The "Dosopt_NB_List" are those that are to be
	# dosopted "not-behaved" (i.e. display oriented).  The "Dosopt_B_List"
	# are those that are to be dosopted "behaved" (i.e. stream oriented).


	# List of files to dosopt "not behaved".
	Dosopt_NB_List="	\
		command.com	\
		"

	# List of files to dosopt "behaved".
	Dosopt_B_List="		\
		format.com	\
		"
	# List of the files in Dosopt_NB_List and Dosopt_B_List that
	# we want to be executable from unix with their "short" name
	# (i.e. without the ".exe" or ".com" extention).
	X_Shortname_List=""

	# Built-In DOS commands.
	# These are the commands that are built into command.com, which are
	# stream-oriented and which we want to be executable from UNIX.

	X_BI_F="copy"
	X_BI_List="		\
		del		\
		dir		\
		rename		\
		type		\
		ver		\
		"
	# Files to delete.
	Delete_List=""

	# Files to hide.
	# Note: subst, join and assign are not currently supported with
	# DR-DOS when the PCI bridge is in use. (i.e. they don't work).
	# They can be useful when booting from a floppy, so leave them
	# around, but hidden, so they can be copied to a floppy.
	# Note: "subst.exe" is copied to the snap disk when making images.
	Hide_List="		\
		subst.exe	\
		join.exe	\
		assign.com	\
		"
}

Exit_Clean()
{
	usrMsg ECHO EXITING "Exiting...\n"
	if [ "x$CLEAN_UP_OK" = "xclean_up_ok" ]
	then
		cd ${INST_ROOT}/
		[ -n "$TMPDIR" -a -d "$TMPDIR" ] && rm -rf $TMPDIR
		for i in `cat ${INST_DBIN}/dos.list`
		do
			rm -f ${INST_DBIN}/$i
		done
		rm -f ${INST_ROOT}/command.com
		rm -f ${INST_DBIN}/command.com
		rm -f ${INST_DBIN}/io.sys
		rm -f ${INST_DBIN}/msdos.sys
		rm -f ${INST_DBIN}/ibmbio.com
		rm -f ${INST_DBIN}/ibmdos.com
		rm -f ${INST_DBIN}/boot.dsk
		rm -f ${INST_DBIN}/dos.list
	fi
	EXIT_NOW $1
}

interruptProc()
{
	usrMsg LOG INST_CANCEL "Installation cancelled by the user.\n"
	Exit_Clean 2
}


Set_DOS_Lists()
{
	if [ "$DOSVER" = "MS-DOS 3.3" ]
	then
		Setup_MSDOS33
	elif [ "$DOSVER" = "MS-DOS 5.0" ]
	then
		Setup_MSDOS50
	elif [ "$DOSVER" = "DR-DOS 6.0" ]
	then
		if [ "$MINI_INSTALL" = "mini_install" ]
		then
			Setup_mini_DRDOS60
		else
			Setup_DRDOS60
		fi
	else
		usrMsg ECHO DOSINST_CANT \
			"Cannot install $DOSVER\n" "$DOSVER"
		Exit_Clean 1
	fi

}

Retry_Abort()
{
	usrMsg STRING DOSINST_DW_P \
"What do you want to do now:\n\
0: Abort the installation of DOS.\n\
1: Retry.\n\
Type 0 or 1 and then press Enter: "
	RA_PROMPT="$USRMSG_STRING\c"
	while true
	do
    		echo "$RA_PROMPT"
		read ANS XXX
		[ "x$ANS" = "x1" ] && break
		[ "x$ANS" = "x0" ] && Exit_Clean 2
		[ "x$ANS" = "x$ANS_Q" ] && Exit_Clean 2
		usrMsg ECHO DOSINST_DW_R "Please enter 0 or 1\n"
	done
}

Get_Disk1_Dir()
{
	while true
	do
		if [ "$NOPROMPT" != "noprompt" ]
		then
			usrMsg ECHO CHECK_DISK  "Checking the disk."
		fi
		echo "dir" | $DOSDISK -s -r $1 > disk1.dir 2>>$TRACE
		if [ $? -eq 0 ]
		then
			break
		fi
		if [ "$NOPROMPT" = "noprompt" ]
		then
			usrMsg ECHO VDISK_ERR \
			    "Error with virtual disk.  Cannot install DOS.\n"
			Exit_Clean 2
		fi
		usrMsg ECHO BAD_DISK \
"\nHad trouble reading the disk.\n\
Check that the disk is inserted in the correct drive.\n\n"
		Retry_Abort
	done
	if [ "$NOPROMPT" != "noprompt" ]
	then
		echo
	fi

	# Must have command.com, io.sys and msdos.sys OR
	#      must have ibmbio.com and ibmdos.com.
	# Note: grep returns 0 when a match.
	grep command.com disk1.dir >> $TRACE 2>&1
	[ $? -eq 0 ] && COMMAND_FILE=command.com
	grep io.sys      disk1.dir >> $TRACE 2>&1
	[ $? -eq 0 ] && IO_FILE=io.sys
	grep msdos.sys   disk1.dir >> $TRACE 2>&1
	[ $? -eq 0 ] && DOS_FILE=msdos.sys
	grep ibmbio.com      disk1.dir >> $TRACE 2>&1
	[ $? -eq 0 ] && IO_FILE=ibmbio.com
	grep ibmdos.com   disk1.dir >> $TRACE 2>&1
	[ $? -eq 0 ] && DOS_FILE=ibmdos.com
}

First_Disk_Sys_Inst()
{
	# The first disk is "$DISK1".  This may be a floppy device or
	# a virtual floppy file.
	# Install the following system files from the first disk:
	#	/command.com
	#	/usr/dbin/command.com
	#	/usr/dbin/io.sys      (or ibmbio.com)
	#	/usr/dbin/msdos.sys   (or ibmdos.com)
	# Create the this bootable disk image from the first disk:
	#	/usr/dbin/boot.dsk
	# Also create the file /usr/dbin/dos.list.  It lists the system
	# files put into /usr/dbin.
	#
	rm -rf fdm.$$
	mkdir  fdm.$$
	cd     fdm.$$

	# Create the boot.dsk file that is used when making images.
	BOOTDSK=${INST_DBIN}/boot.dsk
	COPYFROM=tmpfile1
	COPYTO=tmpfile2
	echo "saveboot $BOOTDSK" > $COPYFROM
	> $COPYTO
	> ${INST_DBIN}/dos.list
	# Note: IO_FILE, DOS_FILE, and COMMAND_FILE are set in 
	#       the function "Get_Disk1_Dir".
	for i in $IO_FILE $DOS_FILE $COMMAND_FILE
	do
		echo "copyfrom $i" >> $COPYFROM
		echo "copyto $i $i" >> $COPYTO
		echo $i >> ${INST_DBIN}/dos.list
	done

	cat $COPYFROM | $DOSDISK -s -e -r $DISK1 >>$TRACE 2>&1
	ls -lt >> $TRACE
	if [ ! -f $COMMAND_FILE ]
	then
		usrMsg ECHO MISSING_FILE \
			"Error: Failed to find $COMMAND_FILE\n" $COMMAND_FILE
		Exit_Clean 2
	fi
	if [ ! -f $IO_FILE ]
	then
		usrMsg ECHO MISSING_FILE \
			"Error: Failed to find $IO_FILE\n" $IO_FILE
		Exit_Clean 2
	fi
	if [ ! -f $DOS_FILE ]
	then
		usrMsg ECHO MISSING_FILE \
			"Error: Failed to find $DOS_FILE\n" $DOS_FILE
		Exit_Clean 2
	fi
	if [ ! -f $BOOTDSK ]
	then
		usrMsg ECHO CANNOT_CREATE \
			"Error: Could not create $BOOTDSK" $BOOTDSK
		Exit_Clean 2
	fi
	cat $COPYTO | $DOSDISK -s -e $BOOTDSK >>$TRACE 2>&1

	echo "chmod,chgrp,chown $COMMAND_FILE $IO_FILE $DOS_FILE" >>$TRACE
	chmod 444 $COMMAND_FILE $IO_FILE $DOS_FILE $BOOTDSK >> $TRACE 2>&1
	chgrp sys $COMMAND_FILE $IO_FILE $DOS_FILE $BOOTDSK >> $TRACE 2>&1
	chown bin $COMMAND_FILE $IO_FILE $DOS_FILE $BOOTDSK >> $TRACE 2>&1
	mv $COMMAND_FILE $IO_FILE $DOS_FILE	${INST_DBIN}

	# Link or copy command.com into root.
	ln ${INST_DBIN}/$COMMAND_FILE ${INST_ROOT}/$COMMAND_FILE >> $TRACE 2>&1
	if [ ! -f ${INST_ROOT}/$COMMAND_FILE ]
	then
		cp ${INSTR_DBIN}/$COMMAND_FILE ${INST_ROOT} >> $TRACE 2>&1
	fi

	cd ..
	rm -rf fdm.$$
}

First_Disk_Mkimg()
{
	# Assume that "First_Disk_Sys_Inst" has already installed the
	# minumum set of system files so that "mkimg" can make images.
	# Do this so later we can run DOS to use the "expand" command to
	# uncompress  the rest of the DOS files.
	#
	# Note: When installing into a special heirarchy, don't need
	# or want to to make images.

	if [ "z$INST_ROOT" != "z" ]
	then
		# Installing into a special hierarchy.
		# Don't make images; assuming already made.
		return
	fi

	echo "\tFirst_Disk_Mkimg" >> $TRACE

	rm -f /usr/merge/image/config.mki
	mkimg mono >> $TRACE
	rm -f /usr/merge/image/config.mki

	if [ ! -f /usr/merge/image/std/mono/mono.img ]
	then
		usrMsg ERROR DOSINST_MKIMG_MONO_FAILED \
"Cannot make mono DOS image files.\n\
     Aborting installation of DOS.\n"
		Exit_Clean 1
	fi

	return
}


Make_executable()
{
	echo "\tMake_executable" >> $TRACE

	# All behaved must also be "-e"
	echo "dosopt +b -e -p $Dosopt_B_List" >> $TRACE
	$DOSOPT +b -e -p +s5 $Dosopt_B_List >> $TRACE 2>&1
	chmod 555 $Dosopt_B_List >> $TRACE 2>&1

	echo "dosopt -b +e +p $Dosopt_NB_List" >> $TRACE
	$DOSOPT -b +e +p +s5 $Dosopt_NB_List >> $TRACE 2>&1
	chmod 555 $Dosopt_NB_List >> $TRACE 2>&1

	echo "\tFinished Dosopting." >> $TRACE
	return 0
}

Make_builtins()
{
	# Assume the current directory is where to make these files.

	echo "\tMake builtins."  >> $TRACE
	echo "LZ" > $X_BI_F
	chmod 555 $X_BI_F >> $TRACE 2>&1
	$DOSOPT +b -e -p +s5 $X_BI_F >> $TRACE 2>&1

	for ii in $X_BI_List
	do
		ln $X_BI_F $ii >> $TRACE 2>&1
		echo $ii >> $TRACE
	done

	return 0
}

Make_Links()
{
	# Assume the current directory is where the files are.

	echo "\tMake_Links."  >> $TRACE
	# Link the filename with the extension to the filename
	# without the extenstion.
	for ii in $X_Shortname_List
	do
		FRONTPART=`echo $ii | cut -f1,1 -d"."`
		ln $ii $FRONTPART >> $TRACE 2>&1
		echo "ln $ii $FRONTPART" >> $TRACE
	done
	return 0
}

Do_copies()
{
	echo "\tDo_copies."    >> $TRACE
	# Put a copy of "country.sys" in root.

	rm -f ${INST_ROOT}/country.sys
	cp country.sys ${INST_ROOT}/ >> $TRACE 2>&1
	chmod 444 ${INST_ROOT}/country.sys >> $TRACE 2>&1
	chgrp sys ${INST_ROOT}/country.sys >> $TRACE 2>&1
	chown bin ${INST_ROOT}/country.sys >> $TRACE 2>&1

	# Install dosshell for VGA.
	[ -f vga.vid ] && cp vga.vid dosshell.vid
	[ -f vga.grb ] && cp vga.grb dosshell.grb
	[ -f ega.ini ] && cp ega.ini dosshell.ini
	# Other display types would use these files:
	# MONO:      (no vid) mono.ini  mono.grb
	# Herc:     herc.vid  mono.ini  herc.grb
	# CGA:       cga.vid   cga.ini  cga.grb
	# EGA:       ega.vid   ega.ini  ega.grb
	# EGA MONO:  ega.vid   ega.ini  egamono.grb
	# VGA:       vga.vid   ega.ini  vga.grb
	# HiRes MONO:vga.vid  mono.ini  vgamono.grb
	# (e.g. mono-vga/supervga, xga, 8514)

	return 0
}

# Prompt for one of two answers - returns 1 for the first answer.
FIRST_ANSWER=1
SECOND_ANSWER=2
QUIT_ANSWER=0
Get_Answer()
{
	while	echo "$3 \c"
	do	read ANS XXX
		[ "x$ANS" = "x$1" ] && return $FIRST_ANSWER
		[ "x$ANS" = "x$2" ] && return $SECOND_ANSWER
		[ "x$ANS" = "x0" ]  && return $QUIT_ANSWER
		[ "x$ANS" = "x$ANS_Q" ] && return $QUIT_ANSWER
		echo "$4"
	done
}

Drive_Name_Density()
{
	if [ -z "$FLOPDENSITY" ]
	then
		return;
	fi
	FLD=$FLOPDENSITY
	if [ "X$1" = "Xreverse" ]
	then
		if [ "$FLOPDENSITY" = "high" ]
		then
			FLD=low
		else
			FLD=high
		fi
	fi

	if [ "$DISKTYPE" = "3.5" ]
	then
		if [ "$FLD" = "high" ]
		then
			# high density 3.5 disk (1.44M)
			FLOPNAMEEND=3ht
		else
			# low density 3.5 disk (720K)
			FLOPNAMEEND=3dt
		fi
	else
		if [ "$FLD" = "high" ]
		then
			# high density 5.25 disk (1.2M)
			FLOPNAMEEND=q15dt
		else
			# low density 5.25 disk (360K)
			FLOPNAMEEND=d9dt
		fi
	fi
	DRIVE_DEV="${FLOPNAMEFRONT}${FLOPDEV}${FLOPNAMEEND}"
}


# -----------------------------------------------------------------------
#
# -----------------------------------------------------------------------
Which_Drive()
{
	usrMsg STRING DRIVEA_NAME "A"
	DRIVEA_NAME="$USRMSG_STRING"
	usrMsg STRING DRIVEB_NAME "B"
	DRIVEB_NAME="$USRMSG_STRING"

	DRIVE_NAME="$DRIVEA_NAME"
	FLOPDEV=0

	#### See if a drive B is available
	if [ -x "$FLOPINFO" ]
	then
		$FLOPINFO b >> $TRACE
		RETVAL=$?
		echo "flopinfo ret $RETVAL" >> $TRACE
	else
		# Assume two drives because we cannot tell how many.
		RETVAL=1
	fi
	if [ $RETVAL = 0 ]
	then
		# No second drive.
		HAVE_BDRIVE=no_bdrive
	else
		# Have a second drive.
		HAVE_BDRIVE=have_bdrive
	fi
	### If a second drive and the drive override has NOT been set, then
	### ask user which drive to use.
	if [ "$HAVE_BDRIVE" = "have_bdrive" ]
	then
		while true
		do
echo
usrMsg ECHO DOSINST_FLOP_P "\
Which floppy drive are you installing from?\n\
\n\
      1: The first drive.  (drive '$DRIVEA_NAME')\n\
      2: The second drive. (drive '$DRIVEB_NAME')\n\
      0: Stop.  Abort installation of DOS.\n\
\n\
Type the item number and then press Enter: " "$DRIVEA_NAME" "$DRIVEB_NAME"
			read ANS XXX
			[ "x$ANS" = "x1" ] && break
			[ "x$ANS" = "x2" ] && break
			[ "x$ANS" = "x0" ] && Exit_Clean 2
			[ "x$ANS" = "x$ANS_Q" ] && Exit_Clean 2
			usrMsg ECHO DOSINST_F_R "Please enter 0, 1, or 2\n"
		done
		if [ $ANS -eq 2 ]
		then
			DRIVE_NAME="$DRIVEB_NAME"
			FLOPDEV=1
		fi
	fi
	if [ $FLOPDEV -eq 0 -a -n "$DRIVE_0_DEV" ]
	then
		DRIVE_DEV="$DRIVE_0_DEV"
	elif [ $FLOPDEV -eq 1 -a -n "$DRIVE_1_DEV" ]
	then
		DRIVE_DEV="$DRIVE_1_DEV"
	else
		# No override.  Construct the unix device name for the floppy.
		if [ "$SYSTEMTYPE" = "sco" ]
		then
			# For SCO, must determine density type.
			FLOPNAMEFRONT=/dev/dsk/f
			# Assume low density (3.5 disk: 720K) (5.25 disk: 360K)
			FLOPDENSITY=low
			# Call this function which sets DRIVE_DEV.
			Drive_Name_Density normal
		else
			# The SVR4 floppy device "f0t" automatically senses the
			# density of the floppy in the drive.
			DRIVE_DEV="/dev/dsk/f${FLOPDEV}t"
		fi
	fi

	if [ -n "$DRIVE_DEV" -a ! -c "$DRIVE_DEV" -a ! -b "$DRIVE_DEV" ]
	then
		# Invalid filename
		usrMsg ERROR BAD_FLOP_NAME \
		    "Invalid floppy drive device name '$DRIVE_DEV'\n" $DRIVE_DEV
		usrMsg ECHO CANNOT_CONTINUE "Cannot continue.\n"
		Exit_Clean 1
	fi

	# The above sets DRIVE_NAME, HAVE_BDRIVE, and DRIVE_DEV.  Log it.
	echo "DRIVE_NAME=$DRIVE_NAME" >> $TRACE
	echo "HAVE_BDRIVE=$HAVE_BDRIVE" >> $TRACE
	echo "DRIVE_DEV=$DRIVE_DEV" >> $TRACE

	echo
	return
}

# -----------------------------------------------------------------------
#
# -----------------------------------------------------------------------
Get_Disk_Count()
{
	while	true
	do
		usrMsg ECHO DOSINST_NDISKS_Q \
			"Number of $1 diskettes in your set: " "$1"
		read DISKCOUNT XXX
		[ "x$DISKCOUNT" = "x1" ] && break
		[ "x$DISKCOUNT" = "x2" ] && break
		[ "x$DISKCOUNT" = "x3" ] && break
		[ "x$DISKCOUNT" = "x4" ] && break
		[ "x$DISKCOUNT" = "x5" ] && break
		[ "x$DISKCOUNT" = "x6" ] && break
		[ "x$DISKCOUNT" = "x7" ] && break
		[ "x$DISKCOUNT" = "x8" ] && break
		[ "x$DISKCOUNT" = "x9" ] && break
		[ "x$DISKCOUNT" = "x0" ] && Exit_Clean 2
		[ "x$DISKCOUNT" = "x$ANS_Q" ] && Exit_Clean 2
		usrMsg ECHO DOSINST_0_9 \
			"Please enter a number 0 through 9\n"
		usrMsg ECHO DOSINST_0_ABORT \
			"Entering 0 will abort the installation of DOS.\n"
	done
	return
}


# -----------------------------------------------------------------------
# "Get_DOS_Brand" asks user what brand of DOS is being installed.
#
# Sets DOS_BRAND and DOSVER.
#------------------------------------------------------------------------
Get_DOS_Brand()
{
	while	true
	do
		echo
		usrMsg ECHO DOSINST_DOSBRAND_MENU "\
Choose which brand of DOS to install.\n\
\n\
      1: MS-DOS 5.0\n\
      2: DR-DOS 6.0\n\
      0: None of the above.  Abort installation of DOS.\n\
\n\
Type the item number and then press Enter: "
		read ANS XXX
		[ "x$ANS" = "x1" ] && break
		[ "x$ANS" = "x2" ] && break
		[ "x$ANS" = "x0" ] && Exit_Clean 2
		[ "x$ANS" = "x$ANS_Q" ] && Exit_Clean 2
		usrMsg ECHO DOSINST_DOSBRAND_HELP \
			"Please enter 0, 1, or 2\n"
	done
	if [ $ANS -eq 1 ]
	then
		DOS_BRAND="MSDOS"
		DOSVER="MS-DOS 5.0"
	elif [ $ANS -eq 2 ]
	then
		DOS_BRAND="DRDOS"
		DOSVER="DR-DOS 6.0"
	fi
}

# -----------------------------------------------------------------------
# "Get_Flop_Info" asks the user how many floppies, (and the size of the
#                 disks with the SCO version.)
#
#  Sets DISKTYPE, and DISKCOUNT.
#      (Note: DISKCOUNT is set calling Get_Disk_Count)
#      (Note: DISKTYPE is only set for non-svr4 versions)
#  Clears DRIVE_NAME and DRIVE_DEV.
#  Note: DOSVER must already be set.
#------------------------------------------------------------------------
Get_Flop_Info()
{
	DRIVE_NAME=""
	DRIVE_DEV=""

	if [ "$SYSTEMTYPE" = "sco" ]
	then
		# The SVR4 floppy device f0t (and f1t) automatically
		# works for both 3.5 and 5.25 inch drives.
		# So only need to ask this question on non-svr4 systems.
		while	true
		do
			echo
			usrMsg ECHO DOSINST_SCO_DISKSIZE "\
Please enter the size of DOS disks you have.\n\
\n\
      1: 5.25\" disks.\n\
      2: 3.5\"  disks.\n\
      0: None of the above.  Abort installation of DOS.\n\
\n\
Type the item number and then press Enter: "
			read ANS XXX
			[ "x$ANS" = "x1" ] && break
			[ "x$ANS" = "x2" ] && break
			[ "x$ANS" = "x0" ] && Exit_Clean 2
			[ "x$ANS" = "x$ANS_Q" ] && Exit_Clean 2
			usrMsg ECHO DOSINST_SCO_DS_R \
				"Please enter 0, 1, or 2\n"
		done
		[ $ANS -eq 1 ] && DISKTYPE=5.25
		[ $ANS -eq 2 ] && DISKTYPE=3.5
	fi
	Get_Disk_Count "$DOSVER"
	return
}

Get_First_Disk()
{
	if [ "$DOSVER" = "MS-DOS 3.3" ]
	then
		if [ "$HAVE_BDRIVE" = "no_bdrive" ]
		then
			# Single drive system, boot disk.
			usrMsg STRING INSERT_S_DOS33 \
"Insert the DOS Program disk in the drive and press ENTER. "
		else
			# Dual drive system, boot disk.
			usrMsg STRING INSERT_D_DOS33 \
"Insert the DOS Program disk in drive $DRIVE_NAME and press ENTER. " \
				"$DRIVE_NAME"
		fi
	else
		# MS-DOS 5.0 or DR-DOS 6.0
		if [ "$HAVE_BDRIVE" = "no_bdrive" ]
		then
			# Single drive system
			usrMsg STRING INSERT_S_DOS50 \
"Insert DOS disk #1 in the drive and press ENTER. "
		else
			# Dual drive system
			usrMsg STRING INSERT_D_DOS50 \
"Insert DOS disk #1 in drive $DRIVE_NAME and press ENTER. " \
				"$DRIVE_NAME"
		fi
	fi
	GFD_PROMPT="$USRMSG_STRING\c"
	Drive_Name_Density reverse
	REV_DEV=$DRIVE_DEV
	Drive_Name_Density normal
	NRM_DEV=$DRIVE_DEV
	while true
	do
    		echo "$GFD_PROMPT"
		read ANS
		[ "x$ANS" = "x0" ] && Exit_Clean 2
		[ "x$ANS" = "x$ANS_Q" ] && Exit_Clean 2
		if [ "$NRM_DEV" != "$REV_DEV" ]
		then
			# We guessed at the floppy density.  Now see
			# if we guessed right.
			dd if=$NRM_DEV of=/dev/null count=1 > /dev/null 2>&1
			if [ $? -ne 0 ]
			then
				# Read failed, so try other density.
				dd if=$REV_DEV of=/dev/null count=1 \
							> /dev/null 2>&1
				if [ $? -ne 0 ]
				then
					# Read failed.
					usrMsg ECHO BAD_DISK \
"\nHad trouble reading the disk.\n\
Check that the disk is inserted in the correct drive.\n\n"
					Retry_Abort
					continue
				fi
				# Read succeeded.  Use this device.
				DRIVE_DEV=$REV_DEV
			fi
		fi
		rm -f disk1.dir
		# Make sure it is the first disk.
		Get_Disk1_Dir $DRIVE_DEV
		# Note: IO_FILE, DOS_FILE, and COMMAND_FILE are set in 
		#       the function "Get_Disk1_Dir".
		if [ -n "$COMMAND_FILE" -a -n "$IO_FILE" -a -n "$DOS_FILE" ]
		then
			break
		fi
		echo
		usrMsg ECHO WRONG_DISK_1 \
"This disk does not appear to be the correct disk.\n\
Please check that you have the correct disk in the proper drive.\n"
		Retry_Abort
	done
	return
}

Get_Next_Disk()
{
	echo "Get_Next_Disk $1 $2" >> $TRACE
	THIS=$1
	PREV=`expr $THIS - 1`
	if [ "$DOSVER" = "MS-DOS 3.3" -a $DISKCOUNT -eq 2 ]
	then
		usrMsg ECHO INSERT_N_DOS33 \
"Replace the DOS Program disk with the Supplemental disk and press ENTER. "
	else
		# MS-DOS 5.0 or DR-DOS 6.0
		usrMsg ECHO INSERT_N_DOS50 \
"Replace disk #$PREV with disk #$THIS and press ENTER. " $PREV $THIS
	fi
	read ANS
	[ "x$ANS" = "x0" ] && Exit_Clean 2
	[ "x$ANS" = "x$ANS_Q" ] && Exit_Clean 2
	while true
	do
		usrMsg ECHO CHECK_DISK  "Checking the disk."
		echo "dir" | $DOSDISK -s -r $DRIVE_DEV > disk$THIS.dir 2>>$TRACE
		if [ $? -ne 0 ]
		then
			usrMsg ECHO BAD_DISK \
"\nHad trouble reading the disk.\n\
Check that the disk is inserted in the correct drive.\n\n"
			Retry_Abort
			continue
		fi
		echo
		NUM=0
		# Check against previous disks.
		while true
		do
			NUM=`expr $NUM + 1`
			if [ $NUM -eq $THIS ]
			then
				# Did not match any of the previous
				# disks, so assume it is the right one.
				return
			fi
			# Note: cmp returns 0 when identical files.
			cmp -s disk$THIS.dir disk$NUM.dir >> $TRACE 2>&1
			RETVAL=$?
			echo "cmp of disk $THIS to $NUM ret $RETVAL" >> $TRACE
			if [ $RETVAL -eq 0 ]
			then
				# Is identical, which means this disk
				# was already read in.  Go to recovery code.
				break
			fi
			# Not identical.  This is correct.
			# Passed test. Test the next.
		done
		# File is identical, which means the user
		# inserted a disk twice.
		echo
		usrMsg ECHO WRONG_DISK_N \
"This disk was already read in.\n\
To retry, and continue to install DOS, insert disk #$THIS\n\n" $THIS
		Retry_Abort
	done
}

Check_Room()
{
	# Check for needed disk space for the image file.
	HAVE_ROOT=`${INST_LIB_MERGE}/dfmerge -b ${INST_ROOT}/`
	HAVE_TMP=`${INST_LIB_MERGE}/dfmerge -b  /tmp`
	HAVE_USR=`${INST_LIB_MERGE}/dfmerge -b ${INST_ROOT}/usr`
	## DOS 5.0 requirements. #########
	# Needed disk space for the DOS images: (/usr/merge)
	   # low image + umb image = each 144 blocks (3).
	   # high image = each 128 blocks (3).
	   # vga rom = 64 blocks (1)
	   # Total = 880 blocks
	# When making images, 840 blocks are needed in -/usr/tmp for two
	# virtual disks, and one set of image files (336 blocks) in -/tmp
	# Needed disk space for -/usr/dbin/boot.dsk: 250 blocks
	# Needed disk space for the DOS files:
	   # All the files from the DOS floppies go into -/usr/dbin.
	   # Note: command.com is also copied to root (100 blocks)
	   # Needed room in -/usr/dbin : 4650
	   # Note: if installing from the vdisks in -/usr/dbin, then we
	   #  recover 4344 blocks when they are removed, which means we need
	   #  156 blocks.  BUT, we still need about 2100 blocks head-room
	   #  in -/usr for this while installing.
	   #   
	# Assume that -/usr/dbin and -/usr/merge are in the -/usr file system.
	# Assume that -/usr/tmp is in the -/usr file system.
	#
	# We pad these sizes up to be safe, and end up with these values:
	NEED_TMP=250
	NEED_ROOT=110
	NEED_USR=6650

	if [ "$HAVE_VDISKS" = "have_vdisks" ]
	then
		# When we have vdisks, it gets complicated to
		# calculate the headroom needed.  The headroom
		# needed is greater than the ending room needed.
		# The ending space needed is
		#   NEED_TMP + NEED_ROOT + 1100 in -/usr
		#    (1630 blocks = 815k)
		# The headroom needed is:
		# When -/usr is in the root filesystem: 2100 blocks.

		NEED_USR=2100
		if [ "$DEV_ROOT" = "$DEV_USR" ]
		then
			# Add NEED_ROOT.
			NEED_USR=`expr $NEED_USR + $NEED_ROOT`
		fi
	fi

	# Now check for the needed space.
	if [ "$DEV_ROOT" = "$DEV_TMP" -a \
	     "$DEV_ROOT" = "$DEV_USR" ]
	then
		# -/tmp and -/usr are in the same (root) filesystem.
		TOTALNEED=`expr $NEED_ROOT + $NEED_TMP + $NEED_USR`
		# Same filesystem
		if [ $HAVE_ROOT -ge $TOTALNEED ]
		then
			return
		fi
		usrMsg ERROR ERR_0 "Cannot install DOS.\n"
		usrMsg ECHO NO_SPACE "     Not enough space.\n"
		usrMsg ECHO NEED_SPACE \
			"     Need: $TOTALNEED blocks, Have: $HAVE_ROOT.\n" \
			$TOTALNEED $HAVE_ROOT
		usrMsg ECHO CLEAN_UP \
"To complete installation of DOS, make more disk space available, and reboot.\n"
		Exit_Clean 1
	fi
	if [ "$DEV_ROOT" != "$DEV_TMP" -a $HAVE_TMP -lt $NEED_TMP ]
	then
		usrMsg ERROR ERR_0 "Cannot install DOS.\n"
		usrMsg ECHO NO_SPACE_IN "     Not enough space in /tmp\n" /tmp
		usrMsg ECHO NEED_SPACE \
			"     Need: $NEED_TMP blocks, Have: $HAVE_TMP.\n" \
			$NEED_TMP $HAVE_TMP
		usrMsg ECHO CLEAN_UP \
"To complete installation of DOS, make more disk space available, and reboot.\n"
		Exit_Clean 1
	fi
	if [ "$DEV_ROOT" = "$DEV_TMP" ]
	then
		NEED_ROOT=`expr $NEED_ROOT + $NEED_TMP`
		NEED_TMP=0
	fi
	if [ "$DEV_ROOT" = "$DEV_USR" ]
	then
		NEED_ROOT=`expr $NEED_ROOT + $NEED_USR`
		NEED_USR=0
	fi

	if [ "$HAVE_ROOT" -lt "$NEED_ROOT" ]
	then
		usrMsg ERROR ERR_0 "Cannot install DOS.\n"
		usrMsg ECHO NO_SPACE_IN "     Not enough space in /\n" /
		usrMsg ECHO NEED_SPACE \
			"     Need: $NEED_ROOT blocks, Have: $HAVE_ROOT.\n" \
			$NEED_ROOT $HAVE_ROOT
		usrMsg ECHO CLEAN_UP \
"To complete installation of DOS, make more disk space available, and reboot.\n"
		Exit_Clean 1
	fi
	if [ $HAVE_USR -lt $NEED_USR ]
	then
		usrMsg ERROR ERR_0 "Cannot install DOS.\n"
		usrMsg ECHO NO_SPACE_IN "     Not enough space in /usr\n" /usr
		usrMsg ECHO NEED_SPACE \
			"     Need: $NEED_USR blocks, Have: $HAVE_USR.\n" \
			$NEED_USR $HAVE_USR
		usrMsg ECHO CLEAN_UP \
"To complete installation of DOS, make more disk space available, and reboot.\n"
		Exit_Clean 1
	fi
}

Check_Installed()
{
	if [ ! -f ${INST_ROOT}/command.com ]
	then
		return
	fi
	if [ ! -f ${INST_DBIN}/io.sys -o \
	     ! -f ${INST_DBIN}/msdos.sys ]
	then
		if [ ! -f ${INST_DBIN}/ibmbio.com -o \
		     ! -f ${INST_DBIN}/ibmdos.com ]
		then
			return
		fi
	fi
	echo
	usrMsg ECHO ERR_2 \
"DOS is already installed.  The currently installed DOS\n\
must be removed before re-installation can be done.\n"
	echo
	if [ "$NOPROMPT" = "noprompt" ]
	then
		usrMsg ECHO DOSINST_NOTINST "Not installing DOS.\n"
		EXIT_NOW 1
	fi

	usrMsg STRING Q_REMOVE \
	    "Do you want to remove the currently installed DOS now (y/n/q) ? "
	Q_REMOVE="$USRMSG_STRING"
	Get_Answer "$ANS_Y" "$ANS_N" "$Q_REMOVE" "$Q_PROMPT"
	if [ $? -ne $FIRST_ANSWER ]
	then
		usrMsg ECHO CANCEL_MSG "Cancelling DOS re-installation.\n"
		EXIT_NOW 0
	fi
	echo
	usrMsg ECHO RM_MSG "Removing DOS\n"
	${INST_LIB_MERGE}/dosremove
	if [ $? -ne 0 ]
	then
		usrMsg ECHO ERR_RM "Had problems removing DOS.\n"
		usrMsg STRING Q_CONTINUE \
			"Do you want to continue DOS installation (y/n/q) ? "
		Q_CONTINUE="$USRMSG_STRING"
		Get_Answer "$ANS_Y" "$ANS_N" "$Q_CONTINUE" "$Q_PROMPT"
		if [ $? -ne $FIRST_ANSWER ]
		then
			echo
			usrMsg ECHO CANCEL_MSG \
				"Cancelling DOS re-installation.\n"
			EXIT_NOW 0
		fi
	fi
	echo
	usrMsg ECHO RE_INST "Re-Installing DOS\n"
	echo
}

Trace_df()
{
	echo "Free blocks in ${INST_ROOT}/: \c" >> $TRACE
	${INST_LIB_MERGE}/dfmerge -b ${INST_ROOT}/ >> $TRACE
	if [ "$DEV_ROOT" != "$DEV_TMP" ]
	then
		echo "Free blocks in /tmp: \c" >> $TRACE
		${INST_LIB_MERGE}/dfmerge -b /tmp >> $TRACE
	fi
	if [ "$DEV_ROOT" != "$DEV_USR" ]
	then
		echo "Free blocks in ${INST_ROOT}/usr: \c" >> $TRACE
		${INST_LIB_MERGE}/dfmerge -b ${INST_ROOT}/usr >> $TRACE
	fi
}


##--------------------------------------------------------------------------
## Start: The main part of the script starts here.
##--------------------------------------------------------------------------

# Force to install in the real root.
INST_ROOT=

INST_DBIN=${INST_ROOT}/usr/dbin
INST_LIB_MERGE=${INST_ROOT}/usr/lib/merge
if [ -n "$DOSINSTALLTRACE" ]
then
	NULL=/tmp/null
	TRACE=/tmp/trace
else
	NULL=/dev/null
	TRACE=/dev/null
fi
LOG_FILE=$TRACE
>$NULL
>$TRACE

echo "Trace of dosinstall" >> $TRACE
[ -n "$DOSINSTALLTRACE" ] && Trace_df

if [ ! -d $INST_LIB_MERGE ]
then
	# Cannot nlsize this message, because the nls message stuff
	# is in the directory that is missing.
	echo "Error: Missing directory $INST_LIB_MERGE"
	echo "Cannot continue.\n"
	EXIT_NOW 1
fi
# Set version variables from the Version file.
. ${INST_LIB_MERGE}/Version

# Set up NLS variables and functions.
DM="LCC.MERGE.UNIX.DOSINSTALL"
. ${INST_LIB_MERGE}/set_nls.sh

if [ ! -d $INST_DBIN ]
then
	usrMsg ECHO MISSING_DIR \
		"Error: Missing directory $INST_DBIN" $INST_DBIN
	usrMsg ECHO CANNOT_CONTINUE "Cannot continue.\n"
	EXIT_NOW 1
fi

CLEAN_UP_OK=
if [ ! -r /dev/kmem ]
then
	usrMsg ERROR ERR_1 "Must be root or su to install DOS.\n"
	EXIT_NOW 1
fi

if [ "z$INST_ROOT" != "z" ]
then
	# Installing into a special hierarchy.
	echo "Installing into a special hierarchy $INST_DBIN" >> $TRACE
	if [ ! -d /bin/dos ]
	then
		usrMsg ECHO NO_FILE \
			"Error: Missing /bin/dos" /bin/dos
		usrMsg ECHO CANNOT_CONTINUE "Cannot continue.\n"
		EXIT_NOW 1
	fi
	if [ ! -d /usr/merge/image/std/mono/mono.img ]
	then
		usrMsg ECHO NO_FILE \
			"Error: Missing /usr/merge/image/std/mono/mono.img" \
			/usr/merge/image/std/mono/mono.img
		usrMsg ECHO CANNOT_CONTINUE "Cannot continue.\n"
		EXIT_NOW 1
	fi
fi

PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin:${INST_DBIN} ; export PATH
DOSOPT=${INST_ROOT}/usr/bin/dosopt
DOSDISK=${INST_LIB_MERGE}/dosdisk
FLOPINFO=${INST_LIB_MERGE}/flopinfo
TMPDIR=${INST_DBIN}/tmpdir.$$

DEV_ROOT=`${INST_LIB_MERGE}/dfmerge -n  ${INST_ROOT}/`
DEV_TMP=`${INST_LIB_MERGE}/dfmerge -n   /tmp`
DEV_USR=`${INST_LIB_MERGE}/dfmerge -n  ${INST_ROOT}/usr`

trap interruptProc 1 2 3 15 16 17

if [ "x$1" = "xnoprompt" ]
then
	NOPROMPT=noprompt
	shift
else
	NOPROMPT=interactive
fi
DOS_BRAND="unknown"
DOSVER=unknown
if [ "x$1" = "xdrdos" -o "x$1" = "xDRDOS" -o "x$1" = "xdr" -o "x$1" = "xDR" ]
then
	DOS_BRAND="DRDOS"
	DOSVER="DR-DOS 6.0"
fi
if [ "x$1" = "xmsdos" -o "x$1" = "xMSDOS" -o "x$1" = "xms" -o "x$1" = "xMS" ]
then
	DOS_BRAND="MSDOS"
	DOSVER="MS-DOS 5.0"
	[ "x$2" = "x3.3" ] && DOSVER="MS-DOS 3.3"
fi
# Install DOS into the INST_DBIN directory.
cd ${INST_DBIN}

echo INST_ROOT=${INST_ROOT} >> $TRACE
echo INST_DBIN=${INST_DBIN} >> $TRACE
echo INST_LIB_MERGE=${INST_LIB_MERGE} >> $TRACE
echo DOSOPT=$DOSOPT >> $TRACE
echo TRACE=$TRACE >> $TRACE
echo NULL=$NULL >> $TRACE

# Set the standard yes/no question variables.

usrMsg STRING ANS_Q "q"
ANS_Q="$USRMSG_STRING"
usrMsg STRING ANS_Y "y"
ANS_Y="$USRMSG_STRING"
usrMsg STRING ANS_N "n"
ANS_N="$USRMSG_STRING"
usrMsg STRING Q_PROMPT "Please answer y or n or q\n"
Q_PROMPT="$USRMSG_STRING"

mkdir $TMPDIR
cd $TMPDIR

# If the virtual DOS disks don't exist, ask user what kind of DOS
# is being installed, size of disk, and which drive.
HAVE_VDISKS=no_vdisks
DISKTYPE=unknown
DISKCOUNT=0
MINI_INSTALL=off

if [ "$HAVE_VDISKS" = "no_vdisks" ]
then
	# See if 720 size vdisks are there.
	DISK1=${INST_DBIN}/MS50.720.1.dsk
	DISK2=${INST_DBIN}/MS50.720.2.dsk
	DISK3=${INST_DBIN}/MS50.720.3.dsk

	if [ -f $DISK1 -a -f $DISK2 -a -f $DISK3 ]
	then
		HAVE_VDISKS=have_vdisks
		DOSVER="MS-DOS 5.0"
		DOS_BRAND="MSDOS"
		DISKCOUNT=3
	fi
fi
if [ "$HAVE_VDISKS" = "no_vdisks" ]
then
	# See if 360 size vdisks are there.
	DISK1=${INST_DBIN}/MS50.360.1.dsk
	DISK2=${INST_DBIN}/MS50.360.2.dsk
	DISK3=${INST_DBIN}/MS50.360.3.dsk
	DISK4=${INST_DBIN}/MS50.360.4.dsk
	DISK5=${INST_DBIN}/MS50.360.5.dsk

	if [ -f $DISK1 -a -f $DISK2 -a  \
	     -f $DISK3 -a -f $DISK4 -a -f $DISK5 ]
	then
		HAVE_VDISKS=have_vdisks
		DOSVER="MS-DOS 5.0"
		DOS_BRAND="MSDOS"
		DISKCOUNT=5
	fi
fi
if [ "$HAVE_VDISKS" = "no_vdisks" ]
then
	# See if DOS 3.3 vdisk(s) are there.
	DISK1=${INST_DBIN}/MS33.360.1.dsk
	DISK2=${INST_DBIN}/MS33.360.2.dsk

	if [ -f $DISK1 ]
	then
		HAVE_VDISKS=have_vdisks
		DOSVER="MS-DOS 3.3"
		DOS_BRAND="MSDOS"
		DISKCOUNT=1
		if [ -f $DISK2 ]
		then
			DISKCOUNT=2
		fi
	fi
fi
if [ "$HAVE_VDISKS" = "no_vdisks" ]
then
	# See if 1.2M DR DOS vdisks are there.
	DISK1=${INST_DBIN}/DR60.12.1.dsk
	DISK2=${INST_DBIN}/DR60.12.2.dsk

	if [ -f $DISK1 -a -f $DISK2 ]
	then
		HAVE_VDISKS=have_vdisks
		DOSVER="DR-DOS 6.0"
		DOS_BRAND="DRDOS"
		DISKCOUNT=2
	fi
fi
if [ "$HAVE_VDISKS" = "no_vdisks" ]
then
	# See if 360K DR DOS vdisks are there.
	DISK1=${INST_DBIN}/DR60.360.1.dsk
	DISK2=${INST_DBIN}/DR60.360.2.dsk
	DISK3=${INST_DBIN}/DR60.360.3.dsk
	DISK4=${INST_DBIN}/DR60.360.4.dsk
	DISK5=${INST_DBIN}/DR60.360.5.dsk
	DISK6=${INST_DBIN}/DR60.360.6.dsk
	DISK7=${INST_DBIN}/DR60.360.7.dsk

	if [ -f $DISK1 -a -f $DISK2 -a -f $DISK3 -a -f $DISK4 -a \
	     -f $DISK5 -a -f $DISK6 -a -f $DISK7 ]
	then
		HAVE_VDISKS=have_vdisks
		DOSVER="DR-DOS 6.0"
		DOS_BRAND="DRDOS"
		DISKCOUNT=7
	fi
fi
if [ "$HAVE_VDISKS" = "no_vdisks" ]
then
	# See if the special "minimum" DR DOS vdisks is there.
	DISK1=${INST_DBIN}/DR60.mini.dsk

	if [ -f $DISK1 ]
	then
		HAVE_VDISKS=have_vdisks
		DOSVER="DR-DOS 6.0"
		DOS_BRAND="DRDOS"
		DISKCOUNT=1
		MINI_INSTALL=mini_install
	fi
fi

if [ "$HAVE_VDISKS" = "no_vdisks" ]
then
	if [ "$NOPROMPT" = "noprompt" ]
	then
		echo
		usrMsg ECHO DOSINST_AUTO_NA "\
Unable to automatically install DOS.\n\
To install DOS from your DOS floppy disks\n\
run the 'dosinstall' program.\n"
		echo
		Exit_Clean 1
	fi

	echo
	usrMsg ECHO INSTALLING_DOS "Installing DOS\n"
	echo
	usrMsg ECHO DOSINST_0Q_MSG \
	 "Entering 0 or q at any prompt will abort the installation of DOS.\n"

	if [ "$DOS_BRAND" = "unknown" ]
	then
		Get_DOS_Brand
	fi
	# Call "Get_Flop_Info" to set DISKCOUNT and DISKTYPE.
	Get_Flop_Info

	# "Which_Drive" sets DRIVE_NAME, DRIVE_DEV and HAVE_BDRIVE.
	Which_Drive

	# Make sure any previous installation of DOS is removed before
	# continuing.
	Check_Installed

	# At this point any previous installed DOS is removed, so
	# enable the CLEAN_UP_OK feature of the "Exit_Clean" function.
	CLEAN_UP_OK=clean_up_ok

	usrMsg ECHO DOSINST_MSG1 \
	 "Installing $DOSVER from $DISKCOUNT disks.\n" \
	 "$DOSVER" $DISKCOUNT
	if [ "$HAVE_BDRIVE" = "have_bdrive" ]
	then
		usrMsg ECHO DOSINST_FROM "Installing from drive $DRIVE_NAME.\n"\
			$DRIVE_NAME
	fi
	echo
	DISK1=$DRIVE_DEV
	DISK2=$DRIVE_DEV
	DISK3=$DRIVE_DEV
	DISK4=$DRIVE_DEV
	DISK5=$DRIVE_DEV
	DISK6=$DRIVE_DEV
	DISK7=$DRIVE_DEV
	DISK8=$DRIVE_DEV
	DISK9=$DRIVE_DEV
	Check_Room
	# Prompt for the first disk, and verify it.
	Get_First_Disk
else
	# Make sure any previous installation of DOS is removed before
	# continuing.
	Check_Installed

	# At this point any previous installed DOS is removed, so
	# enable the CLEAN_UP_OK feature of the "Exit_Clean" function.
	CLEAN_UP_OK=clean_up_ok

	usrMsg ECHO INSTALLING_DOSVER "Installing $DOSVER\n" "$DOSVER"

	Check_Room
	Get_Disk1_Dir $DISK1
	# Note: IO_FILE, DOS_FILE, and COMMAND_FILE are set in 
	#       the function "Get_Disk1_Dir".
	if [ -z "$IO_FILE" -o -z "$DOS_FILE" -o -z "$COMMAND_FILE" ]
	then
		usrMsg ECHO VDISK_ERR \
			"Error with virtual disk.  Cannot install DOS.\n"
		echo "$IO_FILE $DOS_FILE $COMMAND_FILE" >> $TRACE
		Exit_Clean 2
	fi
fi
echo "HAVE_VDISKS= $HAVE_VDISKS" >> $TRACE
echo "DOSVER= $DOSVER" >> $TRACE
echo "DOS_BRAND= $DOS_BRAND" >> $TRACE
echo "DISKTYPE= $DISKTYPE" >> $TRACE
echo "DISKCOUNT= $DISKCOUNT" >> $TRACE

usrMsg ECHO INST_SYS "Installing DOS system files.\n"

# Initialize the file lists
Set_DOS_Lists

# Install the minimal DOS system files from the first disk.
First_Disk_Sys_Inst

if [ "x$DO_EXPAND" = "xdo_expand" ]
then
	# Make an initial mono image so can use Merge to
	# help install the DOS files from the disks.
	First_Disk_Mkimg
fi

# Copy all the files from the disks into the current directory.
CNTR=1
for i in $DISK1 $DISK2 $DISK3 $DISK4 $DISK5 $DISK6 $DISK7 $DISK8 $DISK9
do
	RF=/tmp/rf.$$
	> $RF
	if [ "$HAVE_VDISKS" = "no_vdisks" ]
	then
		usrMsg ECHO READING_MESSAGE "Reading the floppy disk."
	fi
	echo "About to copyfrom $i. df=" >> $TRACE
	[ -n "$DOSINSTALLTRACE" ] && Trace_df
	echo "copyfrom" | $DOSDISK -s -e -r $i >>$TRACE 2>&1
	echo "copyfrom done. df=" >> $TRACE
	[ -n "$DOSINSTALLTRACE" ] && Trace_df
	if [ "$HAVE_VDISKS" = "have_vdisks" ]
	then
		if [ -f $i ]
		then
			# This is unsafe to remove the vdisk before
			# everything is installed correctly, but
			# it takes up too much disk space.  If the
			# installation fails or is aborted, then
			# the user must re-install Merge to try again.
			echo "About to remove $i" >> $TRACE
			if [ "$INSTALLTYPE" = "pkgadd" ]
			then
				# Save the name of the file being removed,
				# so we can "removef" it later.  It is very
				# inefficient to call removef for each file.
				echo $i >> $RF
			fi
			rm -f $i
			echo "Just removed $i" >> $TRACE
		fi
	fi
	CNTR=`expr $CNTR + 1`
	[ $CNTR -gt $DISKCOUNT ] && break
	echo "CNTR=$CNTR DISKCOUNT=$DISKCOUNT" >> $TRACE
	if [ "$HAVE_VDISKS" = "no_vdisks" ]
	then
		echo
		Get_Next_Disk $CNTR $i
	fi
done
echo "All done reading in DOS files. df=" >> $TRACE
if [ "$INSTALLTYPE" = "pkgadd" -a -s $RF ]
then
	removef merge `cat $RF` >> $TRACE 2>&1
	removef -f merge	>> $TRACE 2>&1
fi
rm -f $RF
[ -n "$DOSINSTALLTRACE" ] && Trace_df
if [ "$HAVE_VDISKS" = "no_vdisks" ]
then
	echo
	usrMsg ECHO DSK_DONE "You may remove the disk now.\n"
	echo
fi
echo "Deleting unneeded files." >> $TRACE
for i in $Delete_List
do
	rm -f $i
done
if [ "x$DO_EXPAND" = "xdo_expand" ]
then
	# Need to "expand" all the compressed files.
	EXPLIST=exp.list
	TMPBAT=tempfile.bat

	# The DOS "expand" commmand decompresses DOS installation files.
	# It takes two parameters: the input and output file names.

	# For all known compressed types:
	for i in sys exe com cpi hlp bas pro 386
	do
		# Get the base filename.
		if   [ "$i" = "sys" ] ; then CX=sy_
		elif [ "$i" = "exe" ] ; then CX=ex_
		elif [ "$i" = "com" ] ; then CX=co_
		elif [ "$i" = "cpi" ] ; then CX=cp_
		elif [ "$i" = "hlp" ] ; then CX=hl_
		elif [ "$i" = "bas" ] ; then CX=ba_
		elif [ "$i" = "pro" ] ; then CX=pr_
		elif [ "$i" = "386" ] ; then CX=38_
		fi
		for j in *.$CX
		do
			if [ -f "$j" ]
			then
				echo "expand $j `basename $j $CX`$i" >> $EXPLIST
				echo "del $j" >> $EXPLIST
			fi
		done
	done
	# Expand the vga dosshell files, but leave the compressed versions.
	[ -f vga.vi_ ] && echo "expand vga.vi_ vga.vid" >> $EXPLIST
	[ -f vga.gr_ ] && echo "expand vga.gr_ vga.grb" >> $EXPLIST
	[ -f ega.in_ ] && echo "expand ega.in_ ega.ini" >> $EXPLIST
	if [ -s $EXPLIST ]
	then
		usrMsg ECHO EXPANDING "Expanding DOS files.\n"
		# Do a UNIX to DOS converstion.
		${INST_LIB_MERGE}/charconv -p -x $EXPLIST $TMPBAT

		echo "About to do the expansion. df=" >> $TRACE
		[ -n "$DOSINSTALLTRACE" ] && Trace_df
		# Now run the batch file we just made to do all the expansion.
		dos +b +c -e -p $TMPBAT >> $TRACE
		echo "Done with the expansion. df=" >> $TRACE
	fi
	[ -n "$DOSINSTALLTRACE" ] && Trace_df
	rm -f $EXPLIST $TMPBAT
fi
if [ "$DOSVER" = "DR-DOS 6.0" ]
then
	echo "Special DR DOS 6.0 setup" >> $TRACE
	# As needed, put here special installation needed for DR DOS 6.0.

fi

# At this point all the files are read in.
# Now configure the DOS files.
usrMsg ECHO CONFIGURING "Configuring DOS files.\n"

# First make all the files read only.

chmod 444 *

Do_copies
Make_executable
Make_builtins
Make_Links

# Now just set the file ownerships, and configuration is done.
chgrp sys *
chown bin *

echo "DOSVER=\"$DOSVER\"" > ${INST_DBIN}/version.dos
chgrp sys ${INST_DBIN}/version.dos
chown bin ${INST_DBIN}/version.dos
chmod 444 ${INST_DBIN}/version.dos

echo "Hiding unsupported files." >> $TRACE
HH=/tmp/tmp$$
> $HH
for i in $Hide_List
do
	[ ! -f $i ] && continue
	# Hide the file, but save the name to put into "dos.list" later.
	mv $i .$i
	echo .$i >> $HH
done

# Everything is configured correctly now.
# Make the "dos.list" file, that lists all the DOS files that
# need to be removed from INST_DBIN when DOS is removed.
# Then move the files from the temporary directory to the real one.

ls      > ${INST_DBIN}/dos.list
cat $HH >>${INST_DBIN}/dos.list
rm -f $HH
chgrp sys ${INST_DBIN}/dos.list
chown bin ${INST_DBIN}/dos.list
chmod 444 ${INST_DBIN}/dos.list
mv * .??* .. 2>> $TRACE
cd ..
rm -rf $TMPDIR

Re_Make_Boot_Dsk()
{
# Just in case installed DOS from a low density disk,
# recreate the "boot.dsk" to make sure it is high density.
echo "Make new boot.dsk" >> $TRACE
D1=/tmp/d1.$$
D2=/tmp/d2.$$
BOOTDSK=${INST_DBIN}/boot.dsk
NEWDSK=/tmp/$$.dsk
if [ ! -f /usr/merge/image/std/mono/mono.img ]
then
	mkimg mono >> $TRACE 2>&1
fi
${INST_LIB_MERGE}/mkvfloppy $NEWDSK -s >> $TRACE 2>&1
echo dir | $DOSDISK -s -r $BOOTDSK 2>> $NULL | sort > $D1
echo dir | $DOSDISK -s -r $NEWDSK  2>> $NULL | sort > $D2
cmp -s $D1 $D2	>> $TRACE 2>&1
if [ $? -eq 0 ]
then
	chmod 666 $BOOTDSK
	cp $NEWDSK $BOOTDSK
	chmod 444 $BOOTDSK
else
	echo "Warning: failed to create new boot.dsk"
fi
rm -f  $NEWDSK $D1 $D2
}
if [ "$SYSTEMTYPE" = "sco" -a -c /dev/string/cfg ]
then
	# Workaround for problem when A drive is 2.88M.  This workaround
	# Only works for SCO because /dev/string/cfg is only on SCO systems.
	FD0=`grep floppy /dev/string/cfg | grep unit=0 | awk '{ print $6 }'`
	if [ "x$FD0" != "xtype=135ds36" ]
	then
		# Only remake boot.dsk when drive A is NOT a 2.88M drive.
		Re_Make_Boot_Dsk
	fi
else
	Re_Make_Boot_Dsk
fi

sync

echo "Now DOS is fully installed." >> $TRACE
# At this point DOS is fully installed.
if [ "z$INST_ROOT" = "z" ]
then
	RF=/tmp/rf.$$
	> $RF
	# Now that DOS is installed, remove any virtual DOS disks.
	for ii in	\
		MS50.360.1.dsk MS50.360.2.dsk MS50.360.3.dsk \
			       MS50.360.4.dsk MS50.360.5.dsk \
		MS50.720.1.dsk MS50.720.2.dsk MS50.720.3.dsk \
		MS33.360.1.dsk MS33.360.2.dsk \
		DR60.12.1.dsk  DR60.12.2.dsk  \
		DR60.360.1.dsk DR60.360.2.dsk DR60.360.3.dsk DR60.360.4.dsk \
			       DR60.360.5.dsk DR60.360.6.dsk DR60.360.7.dsk \
		DR60.mini.dsk
	do
		if [ -f ${INST_DBIN}/$ii ]
		then
			if [ "$INSTALLTYPE" = "pkgadd" ]
			then
				# Save the name of the file being removed,
				# so we can "removef" it later.  It is very
				# inefficient to call removef for each file.
				echo ${INST_DBIN}/$ii >> $RF
			fi
			rm -f ${INST_DBIN}/$ii
		fi
	done
	echo "Just removed the vdisks" >> $TRACE
	if [ "$INSTALLTYPE" = "pkgadd" -a -s $RF ]
	then
		removef merge `cat $RF` >> $TRACE 2>&1
		removef -f merge	>> $TRACE 2>&1
	fi
	rm -f $RF

	# At this point DOS is completely installed.
	# Run any needed installation scripts that require DOS to be
	# installed first.

	#  Make all the images
	echo "About to make images. df=" >> $TRACE
	[ -n "$DOSINSTALLTRACE" ] && Trace_df
	mkimg
	if [ -f /usr/merge/image/std/mono/mono.img  -a \
	     -f /usr/merge/image/std/cga/cga.img ]
	then
		# Print out the version of DOS we just installed.
		dos +b +c -e -p ver
	else
		# DOS is installed, but image file(s) did not get made.
		usrMsg ERROR DOSINST_MKIMG_FAILED \
"Failed to make all DOS image files.\n\
     Installation of DOS partialy failed.\n"
		[ -n "$DOSINSTALLTRACE" ] && Trace_df
		EXIT_NOW 0
	fi
fi

usrMsg ECHO DOS_INST_DONE "DOS installation completed.\n"
[ -n "$DOSINSTALLTRACE" ] && Trace_df
EXIT_NOW 0
