#!/bin/sh
# $Id: installwindows.sh,v 1.37 2005/01/16 20:47:30 lreiter Exp $ 
# SCCSID(@(#)installwindows.sh	1.181) Modified 20:14:35 10/18/01

# Script to install Win9x for a Merge/Win4Lin user.

# Start of hardwired parameters. --------------------------------------

#PRINTER_HACK=no
PRINTER_HACK=yes

#DO_NOT_ASK=no
DO_NOT_ASK=yes

INTERACTIVE_CONFIG=no

SAVEFILE=$HOME/savedphase1.cpio

#
# Don't allow auto-restart.  We need to know when each phase of
# installation is done so that we can do the magic step
#
MERGE_DISABLE_RESTART=on
export MERGE_DISABLE_RESTART

#
# Disable HD0 emulation, since it gets in the way of windows install
#
MERGE_DISABLE_HD0_EMULATION=on
export MERGE_DISABLE_HD0_EMULATION


# End of hardwired parameters. ----------------------------------------

# Status codes:
#
# Exit codes 9 and below are considered successful by the GUI.
#
# "Status 0: Fresh installation of Windows"
# "Status 1: Upgraded from Windows 3.x"
# "Status 2: Upgrade from network installation of Windows for Workgroup"
#
#
# The GUI expects error codes to start at 10 and above.  Currently the
# GUI uses error code 100 to indicates that it can't read the exit code
# returned by installwindows. (see winsetup/configutils.c checkwin9x())
#
# Error codes:
# "Error  6: User canceled the installation.
# "Error 10: The personal drive directory in the $WIN_CONFIG"
# "Error 11: cdletter value incomplete"
# "Error 12: $SAVEFILE is a directory!"
# "Error 13: saved phase1 file $SAVEFILE does not exist."
# "Error 14: Unknown parameter $1"
# "Error 15: Missing file $FN"
# "Error 16: No CD device name specified and no default"
# "Error 17: Invalid CD-ROM device name specified in $FN"
# "Error 18: fromdir value incomplete"
# "Error 19: The personal drive directory $PDIR"
# "Error 20: Not enough disk space to install Windows."
# "Error 21: \"$PDIR\" does not exist!"
# "Error 22: cddevice value incomplete"
# "Error 23: $PDIR directory already exists."
# "Error 24: Windows failed the initial install."
# "Error 25: $SAVEFILE is a directory!"
# "Error 26: Failed to patch \"$PATTARG\"!"
# "Error 27: Failed to patch \"$PATTARG\"!"
# "Error 28: \"$PDIR\" does not exist!"
# "Error 29: $HWIN/.hide does not exist and cannot be created!"
# "Error 30: \"$PDIR\" does not exist!"
# "Error 31: $INF/.hide does not exist and cannot be created!"
# "Error 32: Cannot install Windows as root"
# "Error 33: Invalid CD-ROM device name specified:"
# "Error 34: Failed to find drive letter for directory install,"
# "Error 35: Unsupported Merge/X display configuration.  Can't upgrade."
# "Error 36: Unsupported upgrade scenario.  Can't upgrade."
# "Error 37: Personal drive is not drive C."
# "Error 38: Invalid parameters."
# "Error 39: xxx is not a directory!"
#
# "Error 40: Automatic installation of DOS 7 failed."
# "Error 41: Missing file $FN"
# "Error 42: Missing STATICMERGE setting in file $FN."
# "Error 43: Missing directory $STATICMERGE"
# "Error 44: Missing VARMERGE setting in file $FN."
# "Error 45: Missing directory $VARMERGE"
# "Error 46: Missing file $VARMERGE/Version"
# "Error 47: Missing SYSTEMTYPE in $VARMERGE/Version file."
# "Error 48: Internal error, xxx file is damaged."
# "Error 49: Zooming required but not available."
# 
# "Error 50: Win4Lin is not licensed on this system."
# "Error 51: Can't run Windows right now (too many sessions)."
# "Error 52: Cannot install this version of Windows now.
# "Error 53: Wrong version of DOS 7 was automatically installed."
# "Error 54: Windows  version of DOS 7 was automatically installed."
# "Error 55: Win4Lin license is not readable."
#SRI
# "Error 56: No SRI_OUTPUT_FILE set"
#ENDSRI
# "Error 57: Internal error, mysterious failure."
# "Error 58: Cannot install with Winsock 2 on Windows 95"
#
# "Error 99: Internal error."


NULL=/dev/null

# "Global" variables that are shared between the functions:
PLETTER=
PDIR=
WINDIR=
INF=
SOURCE_DRIVE_LETTER=
CDDEVICE=
PRODUCT_ID=
SETUP_DIR=
SETUPCMD=
TRACE=
DO_SILENT=notsilent
OK_TO_LOG=no
GUI_OPT=
PHASE1_OP=
PHASE1_ONLY=
PHASE2_ONLY=
GLOBAL_AUTOEXEC=
FROMDIRECTORY=
WIN_VERSION=unknown
WIN95_VERSION=not95
WIN98_VERSION=not98
export WIN95_VERSION
DID_MOUNT=
DOS_VERSION=x
WINDOWMEMSIZE=
# The default configuration to use is "win".
WIN_CONFIG=win
ARG_X=
WIN3x_TO_WIN95_UPGRADE=FALSE
ISOSR25=no
INST_FROM_CD=no
EPT=
DEFAULT_CAB_DIR=
CAB_DIR=
DOING_SLURP=
COUNTRY=
WIN_CODESET=
CODEPAGE=
MYLANG=
ORIG_SIZEAUTO=
ORIG_WIDTH=
ORIG_HEIGHT=
USE_MSBATCH_PRODUCT_KEY=TRUE

#SRI got an error, think this is why
VNET_DHCP=0
#ENDSRI
# This function prints to stdout, and properly deals with escaped chars.
# On some system, plain "echo" does this, but others need a special option.
Text()
{
	echo $EPT "$*"
}

# This function prints to stdout, and properly deals with escaped chars,
# and converts the output to DOS text.
DOSText()
{
	echo $EPT $* | $STATICMERGE/charconv -p -x
}

# Print out to the log file when ok to log, and to the user when trace is on.
Log()
{
	[ "$OK_TO_LOG" = "ok_to_log" ] && echo "$*" >> $LOGFILE
	[ "x$TRACE" = "xtrace" ]       && echo "$*"
}

# Print out to the user, and to the log file when ok to log.
Echo()
{
	[ "$OK_TO_LOG" = "ok_to_log" ] && echo "$*" >> $LOGFILE
	[ "$DO_SILENT" = "notsilent" ] && echo "$*"
}

# Run patchit saving output if needed
Patchit_tome()
{
	if [ "$OK_TO_LOG" = "ok_to_log" ]
	then
	    $STATICMERGE/patch/patchit -d $* >> $LOGFILE
	else
	    if [ "$DO_SILENT" = "notsilent" ]
	    then
		$STATICMERGE/patch/patchit -d $*
	    else
		$STATICMERGE/patch/patchit $*
	    fi
	fi
}

Exit()
{
	STATUS=$1

	[ -n "$ORIG_SIZEAUTO" ] && winconfig $WIN_CONFIG set "$ORIG_SIZEAUTO"
	[ -n "$ORIG_WIDTH" ] && winconfig $WIN_CONFIG set "$ORIG_WIDTH"
	[ -n "$ORIG_HEIGHT" ] && winconfig $WIN_CONFIG set "$ORIG_HEIGHT"

	if [ -n "$DID_MOUNT" ]
	then
		$STATICMERGE/mcd -d $MERGE_CD_DEV_NAME -m $VARMERGE/mnt -u
		if [ $? -ne 0 ]
		then
			Echo "Warning: Can't unmount CD-ROM"
		fi
	fi
	if [ "x$GUI_OPT" = "xgui" ]
	then
		if [ $STATUS -ne 0 ]
		then
			Echo
			Echo "Windows installation failed."
			Echo "Press return to continue..."
			read XXX
		else
			#
			# We want to let the GUI front-end known what type
			# of installation was done:
			# 	Fresh install of Windows 9x or
			#	Upgrade from Windows 3.x or
			#	Upgrade from a network install of WFW
			#
			if [ "$WIN3x_TO_WIN95_UPGRADE" = "TRUE" ]
			then
				STATUS=1
				if [ "$WIN3x_NETWORK_UPGRADE" = "TRUE" ]
				then
					STATUS=2
				fi
			fi
			sleep 2
		fi
		#
		# Since we were invoked as the backend of the GUI, put
		# the exit status into a file so that the GUI front-end
		# can get it.  It is the GUI's responsibility to remove
		# this file.
		#
		Echo $STATUS > $USEHOME/.merge/installwindows.exitval
	fi
	exit $STATUS
}

Get_System_Variables()
{
	# Note, none of the errors detected in mrgssv.sh should
	# ever happen, so there is no need to get tricky and to
	# suppress the error message output if the "silent" option
	# might be used.
	
	Log Get_System_Variables
	# Get the STATICMERGE, VARMERGE, DEFAULTMERGE, SYSTEMTYPE, USEHOME,
	# etc settings by dotting in mrgssv.sh.
	# This also defines the Dos_Cmd and Win_Cmd functions.
	. /etc/mrgssv.sh

	USEHOME="$HOME"
	[ ! -d "$USEHOME"/mydata ] \
			 && mkdir "$USEHOME"/mydata
	if [ ! -d "$USEHOME"/mydata/MyDocuments ]
	then
		if [ -f /etc/lindowsos-version ]; then

			# if we're running on LindowsOS, we want to create
			# a symlink under mydata to $HOME/My Documents;
			# of course if we're root we have to figure out what
			# the real home directory is
			if [ "$USEHOME" = "$MERGE_ROOT_HOME" ]
			then
				REALHOME="/root"
			else
				REALHOME="$HOME"
			fi
			[ -d "$REALHOME"/My\ Documents ] \
				&& ln -s "$REALHOME"/My\ Documents \
				"$USEHOME"/mydata/MyDocuments
		fi
			
		[ ! -d "$USEHOME"/mydata/MyDocuments ] \
			&& mkdir "$USEHOME"/mydata/MyDocuments
	fi
	DOTMERGE=$USEHOME/.merge
	[ -d $DOTMERGE ] || winconfig dos list personaldrive > /dev/null 2>&1
	
	LOGFILE="$DOTMERGE/installwindows.log"
	touch "$LOGFILE"
	if [ -f $LOGFILE -a -w $LOGFILE -a ! -L $LOGFILE ]
	then
		date > $LOGFILE
		OK_TO_LOG="ok_to_log"
	else
		LOGFILE=/dev/null
	fi
	rm -f $DOTMERGE/msbatch.inf

	MERGE_AUTOBACKGROUND=off
	export MERGE_AUTOBACKGROUND
	if [ "$SYSTEMTYPE" = "linux" ]
	then
		EPT="-e"
	fi
	DEFAULT_CAB_DIR=$VARMERGE/dosroot/wincabs
}


Must_Not_Be_Root()
{
	# Make sure we are not root.
	#
	# Although on some systems such as AIX, SCO, UnixWare the id
	# command has a -u option that we can use so that we don't
	# have to parse (i.e. we can simply use uid=`$ID -u'), there
	# are systems that do not have the -u option (CHOTS, Solaris).
	# Thus, we use the more general approach of parsing with sed.
	#
	# The line
	#
	#	eval `LANG=C $ID | sed -e 's/(.*//'`
	#
	# first passes the output of $ID (id)
	#
	#	uid=7317(user_name) gid=124(group_name) ...
	#
	# to the sed command which removes the first parentheses and
	# everything after it.  This gives
	#
	#	uid=7317
	#
	# The eval command then assign the value 7317 to the variable
	# uid.  Notice that the value uid itself comes from parsing
	# the output of "id".
	#
	# Note that to make sure the "id" program outputs something
	# that we know how to parse, it must be run with the "C" locale.
	# That is what the "LANG=C" stuff does.
	#
	ID=/usr/bin/id
	[ -x $ID ] || ID=/bin/id
	eval `LANG=C $ID | sed -e 's/(.*//'`
	if [ "$uid" = 0 ]
	then
		Echo "Error 32: Cannot install Windows as root"
		Exit 32
	fi
}

# Check if DOS 7.x is installed.
# Returns zero if 7.0 or 7.1 is installed.
# Otherwise returns 1.
# (If version.dos is badly corrupted, then might exit with an error message.)
# Also sets DOS_VERSION to one of DOS71_W98, DOS71, DOS70, OLDDOS, or NODOS.
Check_DOS7()
{
	Log Check_DOS7
	if [ ! -s $VARMERGE/dosroot/dos/version.dos ]
	then
		# This is no DOS currently installed.
		DOS_VERSION=NODOS
		Log "No DOS is installed."
		return 1
	fi
	# Make sure DOS 7 is installed!
	WVAL=`grep -i "^WDOS_VER=" $VARMERGE/dosroot/dos/version.dos`
	case "$WVAL" in
	*MEDOS80*)
		# This is the WinME Dos 8.0
		DOS_VERSION=DOS80_WME
		Log "WinME DOS 8.0 is installed"
		return 0
		;;
	*98DOS71*)
		# This is the Win98 Dos 7.1
		DOS_VERSION=DOS71_W98
		Log "Win98 DOS 7.1 is installed"
		return 0
		;;
	*DOS71*)
		# This is dos 7.1
		DOS_VERSION=DOS71
		Log "DOS 7.1 is installed"
		return 0
		;;
	*DOS70*)
		# This is dos 7.0
		DOS_VERSION=DOS70
		Log "DOS 7.0 is installed"
		return 0
		;;
	*OLDDOS*)
		# This is old dos 
		DOS_VERSION=OLDDOS
		Log "PRE-DOS 7.0 is installed"
		return 1
		;;
	*)
		Echo "ERROR 48: $VARMERGE/dosroot/dos/version.dos"
		Echo "is damaged.  To recover, reinstall DOS."
		Echo "Cannot continue."
		Exit 48
		;;
	esac
}

# Make sure that DOS 7 is installed.
# In some cases installs DOS 7 from the CD.
# Only returns when a proper DOS 7 is installed.
# Exits with an error message if DOS 7 is installed but is not
# the version that works with the version of Windows being installed.
Determine_DOS7()
{
	Check_DOS7
	if [ "$DOS_VERSION" = "DOS80_WME" -a "$WIN_VERSION" = "winME" ]
	then
		# A valid DOS 7 is installed.  Nothing more need to be done.
		return
	fi
	if [ "$DOS_VERSION" = "DOS71_W98" -a "$WIN_VERSION" = "win98" ]
	then
		# A valid DOS 7 is installed.  Nothing more need to be done.
		return
	fi
	if [ "$DOS_VERSION" != "DOS70" -a "$WIN95_VERSION" = "OSR1" ]
	then
		Echo ""
		Echo "Error 52: Cannot install this version of Windows now."
		Echo "This version of Windows requires MS-DOS 7.0 and"
		Echo "currently MS-DOS 7.1 is installed."
		Echo "You should install Windows from the same CD that"
		Echo "this MS-DOS was installed from."
		Echo ""
		Exit 52
	fi
	if [ "$DOS_VERSION" = "DOS70" -a "$WIN95_VERSION" != "OSR1" ]
	then
		Echo ""
		Echo "Error 52: Cannot install this version of Windows now."
		Echo "This version of Windows requires MS-DOS 7.1 and"
		Echo "currently MS-DOS 7.0 is installed."
		Echo "You should install Windows from the same CD that"
		Echo "this MS-DOS was installed from."
		Echo ""
		Exit 52
	fi
	if [ "$DOS_VERSION" = "DOS71" -o "$DOS_VERSION" = "DOS70" ]
	then
		# A valid DOS 7 is installed.  Nothing more need to be done.
		return
	fi
	if [ "$DOS_VERSION" = "OLDDOS" -a "$WIN95_VERSION" != "OSR1" ]
	then
		# We do not want to automatically upgrade DOS 6.x to DOS 7.1
		# because if anyone has Win 3.1 installed, it will break.
		Echo ""
		Echo "Cannot install this version of Windows now."
		Echo ""
		Echo "This version of Windows requires MS-DOS 7.1 and"
		Echo "currently MS-DOS 6.x or older is installed."
		Echo "If someone already has Windows 3.1 installed and"
		Echo "this MS-DOS is upgraded to MS-DOS 7.1, then Windows"
		Echo "3.1 will not work anymore."
		Echo "If this is the situation it is best to install the"
		Echo "'Upgrade' version of Windows95, because it has"
		Echo "MS-DOS 7.0 which works with Windows 3.1."
		Echo "If you really want to install the Windows from"
		Echo "this CD then you first need to log in as 'root'"
		Echo "and install DOS from this CD."
		Echo ""
		Exit 52
	fi

	# A DOS 7 is not already installed, so run loadwindowsCD to install it.
	if [ "z$SETUP_DIR" = "z" ]
	then
		FROMSUBDIR=
	else
		FROMSUBDIR="dir $SETUP_DIR"
	fi
	if [ "x$GUI_OPT" = "xgui" ]
	then
		ARG_GUI="gui"
	else
		ARG_GUI=
	fi
	/usr/bin/loadwindowsCD $ARG_GUI $INSTFROM $FROMSUBDIR
	if [ $? -ne 0 ]
	then
       		Echo ""
       		Echo "Error 40: Load of the Windows CD files failed."
       		Echo "To install Windows, the Windows files must be loaded."
       		Echo ""
		Exit 40
	fi

	Check_DOS7
	if [ $? -ne 0 ]
	then
       		Echo ""
       		Echo "Error 40: Load of the Windows CD files failed."
       		Echo "To install Windows, the Windows files must be loaded."
       		Echo ""
		Exit 40
	fi
	# The correct version of DOS should have been installed, but
	# just make sure.
	if [ "$WIN95_VERSION" = "OSR1" -a "$DOS_VERSION" != "DOS70" ]
	then
		Echo ""
		Echo "Error 53: Wrong version of DOS 7 was automatically installed."
		Echo "This version of Windows requires MS-DOS 7.0 and that"
		Echo "was not the version that got automatically installed."
		Echo ""
		Exit 53
	fi
	if [ "$WIN95_VERSION" = "OSR2" -a "$DOS_VERSION" != "DOS71" ]
	then
		Echo ""
		Echo "Error 53: Wrong version of DOS 7 was automatically installed."
		Echo "This version of Windows requires MS-DOS 7.1 and that"
		Echo "was not the version that got automatically installed."
		Echo ""
		Echo ""
		Exit 53
	fi
	if [ "$WIN_VERSION" = "win98" -a "$DOS_VERSION" != "DOS71_W98" ]
	then
		Echo ""
		Echo "Error 53: Wrong version of DOS 7 was automatically installed."
		Echo "This version of Windows requires Win98 MS-DOS 7.1 and that"
		Echo "was not the version that got automatically installed."
		Echo ""
		Echo ""
		Exit 53
	fi
	return
}

# Function Get_CD_DEVICE()
#	Set the global MERGE_CD_DEV_NAME with the CD device name we 
#	are to use. Note: This is only called from Source_Setup.
# 	Returns 0 when MERGE_CD_DEV_NAME is set properly.
#	When MERGE_CD_DEV_NAME is not set properly, it exits
#	unless the "no_fail" parameter is used, in which case
#	a 1 is returned.
Get_CD_DEVICE()
{
	# When NO_FAIL is set to "no_fail", then don't fail if some
	# problem is detected in this function, and return 1.
	NO_FAIL=$1
	if [ "x$PHASE2_ONLY" != "x" ]
	then
		Log skipping Get_CD_DEVICE
		return 1
	fi
	Log Get_CD_DEVICE
	if [ "x$CDDEVICE" != "x" ]
	then
		MERGE_CD_DEV_NAME="$CDDEVICE"
		return 0
	fi

	if [ ! -s $DEFAULTMERGE ]
	then
		[ "x$NO_FAIL" = "xno_fail" ] && return 1
		Echo "Error 15: Missing file $DEFAULTMERGE"
		Echo "Cannot continue.\n"
		Exit 15
	fi
	eval `grep "^MERGE_CD_DEV_NAME=" $DEFAULTMERGE`
	if [ "x$MERGE_CD_DEV_NAME" = "x" ]
	then
		Log "No CD device name specified and no default"
		[ "x$NO_FAIL" = "xno_fail" ] && return 1
		Echo "Error 16: No CD device name specified and no default"
		Echo "MERGE_CD_DEV_NAME setting in file $DEFAULTMERGE."
		Echo "Cannot continue.\n"
		Exit 16
	fi
	if [ ! -b $MERGE_CD_DEV_NAME ]
	then
		Log "Invalid CD-ROM device name specified in $DEFAULTMERGE"
		[ "x$NO_FAIL" = "xno_fail" ] && return 1
		Echo "Error 17: Invalid CD-ROM device name specified in $DEFAULTMERGE"
		Echo "(MERGE_CD_DEV_NAME=$MERGE_CD_DEV_NAME)"
		Echo "Cannot continue.\n"
		Exit 17
	fi
	Log Using cd drive setting from $DEFAULTMERGE: $MERGE_CD_DEV_NAME
	return 0
}

# Check if user has Windows already installed.
Check_Win9x()
{
	cd $PDIR
	if [ ! -d $WINDIR/inf ]
	then
		# Not installed already.
		return 0
	fi
	if [ ! -f $WINDIR/inf/layout.inf ]
	then
		# Not installed already.
		return 0
	fi
	grep "Windows 95" $WINDIR/inf/layout.inf >> $NULL 2>&1
	if [ $? -eq 0 ]
	then
		# Already installed.
		return 1
	fi
	grep "Windows 98" $WINDIR/inf/layout.inf >> $NULL 2>&1
	if [ $? -eq 0 ]
	then
		# Already installed.
		return 1
	fi
	grep "Windows ME" $WINDIR/inf/layout.inf >> $NULL 2>&1
	if [ $? -eq 0 ]
	then
		# Already installed.
		return 1
	fi
	return 0
}

# Check if the windows cabs files have been installed in the standard place.
# Returns zero if the files are NOT there.
# Returns one when the file are there.
Check_Wincabs()
{
        # For Simplified Chinese/Japanese Win98se we probably need 
	# check precopy2.cab by adding 
	#		" -f "$DEFAULT_CAB_DIR/precopy2.cab" -a \"
        # However, we have no idea about the version now
        # But I figure it is OK skip this check for a while
        # Since it won't hurt a good guy, just kill bad guy later

	if [ -f "$DEFAULT_CAB_DIR/setup.exe" -o \
	     -f "$DEFAULT_CAB_DIR/install.exe" -o \
	     -f "$DEFAULT_CAB_DIR/config.exe" -o \
	     -f "$DEFAULT_CAB_DIR/instalar.exe" -o \
	     -f "$DEFAULT_CAB_DIR/asenna.exe" -o \
	     -f "$DEFAULT_CAB_DIR/instala.exe" -o \
	     -f "$DEFAULT_CAB_DIR/setupcur.exe" -o \
	     -f "$DEFAULT_CAB_DIR/instal.exe" -o \
	     -f "$DEFAULT_CAB_DIR/kur.exe" -a \
	     -f "$DEFAULT_CAB_DIR/precopy1.cab" ]
	then
		return 1
	fi
	return 0
}

# Determine the "Personal Drive Letter" and where it is located
# from the configuration info.
# Put this info in the variables PLETTER and PDIR.
Determine_Personal()
{
	Log Determine_Personal

	# The fuctions get_personal_drive_letter and get_drive_root
	# are in the file unixdrive.sh.
	. $STATICMERGE/bin/unixdrives.sh

	get_personal_drive_letter $WIN_CONFIG
	PLETTER=$PERS_DRV_LETTER
	if [ "x$PLETTER" != "xc" -a "x$PLETTER" != "xC" ]
	then
		Echo "Error 37: Personal drive is not drive C."
		Echo "To install Windows, your personal drive must be"
		Echo "set to drive C in your $WIN_CONFIG configuration."
		Echo "Cannot continue.  Exiting..."
		Exit 37
	fi
	get_drive_root c $WIN_CONFIG
	if [ $? -ne 0 ]
	then
		Echo "Error 10: The personal drive directory in the $WIN_CONFIG"
		Echo "configuration is not valid."
		Echo "Cannot continue.  Exiting..."
		Exit 10
	fi
	PDIR=$DRV_ROOT

	Log C drive rooted at $PDIR

	if [ ! -d $PDIR ]
	then
		mkdir $PDIR
	fi
	if [ ! -d $PDIR ]
	then
		Echo "Error 19: The personal drive directory $PDIR"
		Echo "does not exist, and could not be created."
		Echo "Cannot continue.  Exiting..."
		Exit 19
	fi
}

# Determine the directory name where windows is to be installed under
# the personal drive. PDIR must already be set!
Determine_Windir()
{
	if [ "x$WINDIR" = "x" ]
	then
		# Search for an existing windows directory.
		CURDIR=`pwd`
		cd $PDIR
		for ii in *
		do
			if [ -d "$ii" -a -s "$ii/win.com" ]
			then
				WINDIR="$ii"
				break
			fi
		done
		cd $CURDIR
		if [ "x$WINDIR" = "x" ]
		then
			WINDIR=windows
		fi
	fi
	Log "WINDIR=$WINDIR"
	return
}

# Check if this is an upgrade install from Win31 or WFW.
Check_Win_Upgrade()
{
	#
	# Note that dosx.exe exists for both win31 and WFW and doesn't
	# on a fresh win9x install.
	#
	CURDIR=`pwd`
	cd $PDIR

	# Setup defaults
	WIN3x_TO_WIN95_UPGRADE=FALSE
	WIN3x_USING_VWDRV=FALSE
	WIN3x_NETWORK_UPGRADE=FALSE

	# Is this an upgrade from either a stand-alone or network install?
	#
	# Run winver to find out if Windows is installed and what version.
	# If winver runs then we know that Windows is already installed.
	# Thus, we are in an upgrade situation.  By using winver instead of
	# looking for certain files in the windows directory we don't have
	# to know where the windows directory is located.
	#
	WINTEST=wt$$.bat
	HAVEWIN=hw$$

	DOSText "winver > $HAVEWIN" >  $WINTEST
	DOSText "quit"              >> $WINTEST

	Dos_Cmd +k$WIN_CONFIG +b $WINTEST > /dev/null 2>&1
	rm $WINTEST
	if [ -f $HAVEWIN ]
	then
		$STATICMERGE/charconv -d -x $HAVEWIN $HAVEWIN.unix
		mv $HAVEWIN.unix $HAVEWIN

		OLD_WIN_VER=`cat $HAVEWIN`
		Echo ""
		Echo "$OLD_WIN_VER is already installed"
		Echo ""
		XXX=`grep -i "95" $HAVEWIN`
		if [ "x$XXX" != "x" ]
		then
			Echo ""
			Echo "Cannot install Windows now:"
			Echo "Cannot install again or do an upgrade."
			Echo ""
			Exit 52
		fi
		XXX=`grep -i "98" $HAVEWIN`
		if [ "x$XXX" != "x" ]
		then
			Echo ""
			Echo "Cannot install Windows now:"
			Echo "Cannot install again or do an upgrade."
			Echo ""
			Exit 52
		fi
		# If we get this far then it is Win3.1/WFW that is installed.
		Echo "$OLD_WIN_VER is already installed"

		XXX=`grep -i "workgroup" $HAVEWIN`
		if [ "x$XXX" = "x" ]
		then
			OLD_WIN_VER=WIN31
		else
			OLD_WIN_VER=WFW
		fi
		rm $HAVEWIN

		#
		# Check upgrade pre-requisite:
		#	0: Cannot upgrade from Win31 to OSR2
		#	1. Must have drive C: as the personal drive
		#
		if [ "$WIN95_VERSION" != "OSR1" ]
		then
			Echo ""
			Echo "Cannot install this version of Windows now."
			Echo "Windows 3.1 cannot be upgraded to this version."
			Echo ""
			Exit 36
		fi
		PERSONALDRIVE=`winconfig $WIN_CONFIG list personaldrive | cut -d= -f2`
		if [ "x$PERSONALDRIVE" != "xc" ]
		then
			Echo "Cannot upgrade Windows."
			Echo "Must have drive C: as the personal drive."
			Exit 36
		fi

		Echo "Performing an upgrade install of Windows 95"

		WIN3x_TO_WIN95_UPGRADE=TRUE

		# In a non-network install of Windows 3.x the file krnl286.exe
		# is located in the system subdirectory which is NORMALY not
		# in the path.  Thus, if krnl286 doesn't run then we assume
		# that we are not doing a network install upgrade.
		#
		# In a network install of Windows 3.x the file krnl286.exe is
		# located in the shared directory which MUST be in the path
		# in order for Windows to run.  Thus, if krnl286.exe run then
		# we assume that we are doing a network install upgrade.
		#
		# NOTE: If these assumptions are incorrectly then the worse
		# thing that can happen is that we print the wrong message at
		# the end of the installation (via the gui front end which
		# print out the message according to the exit value of
		# installwindows).
		#
		# WARNING: If you add code that uses the WIN3x_NETWORK_UPGRADE
		# varible and that code needs WIN3x_NETWORK_UPGRADE to be
		# absolutely correct then don't rely on the code below to
		# set WIN3x_NETWORK_UPGRADE correctly in all situations.
		#
		NETTEST=nt$$.bat
		NETINST=ni$$
		DOSText "krnl286 > $NETINST" > $NETTEST
		Dos_Cmd +k$WIN_CONFIG +b $NETTEST > /dev/null 2>&1
		if [ -f $NETINST ]
		then
			WIN3x_NETWORK_UPGRADE=TRUE
			rm $NETINST
		fi
		rm $NETTEST

		#
		# See if we are running with the VW driver and if so
		# if it is in a valid configuration for upgrading,
		# i.e. no large fonts, or CGA or Hercules or Mono
		# modes supported.
		#
		if [ ! -f $WINDIR/system.ini ]
		then
			# We don't know where the system.ini file is so
			# assume that we are using the vw driver.
			#
			# If we don't assume that we are using the vw
			# driver and do a startzoom incorrectly then the
			# Merge session will hang tryng to start Windows
			# under fullscreen using the vw driver.
			#
			WIN3x_USING_VWDRV=TRUE
		else
		    grep -i 'display\.drv[ 	]*=[ 	]*vw\.drv' \
			$WINDIR/system.ini > $NULL 2>&1
		    if [ $? -eq 0 ]
		    then
			VW_STRING=`grep 'Merge Windows/X' $WINDIR/system.ini \
			    | sed -e 's,display\.drv *= *,,' \
			    | $STATICMERGE/charconv -d -x`
			NON_STANDARD_VW=`echo "$VW_STRING" | \
				sed -e 's,.*Merge Windows/X[ ]*,,'`
			if [ -n "$NON_STANDARD_VW" ]
			then
				Echo "Error 35: Unsupported Merge/X display " \
				    "configuration.  Can't upgrade."
				Echo "Current Configuration is:"
				Echo "\t${VW_STRING}"
				Echo "Needed Configuration is:"
				Echo "\tDOS Merge Windows/X\n or\n\tVGA"
				Echo Cannot continue.  Exiting.
				Exit 35
			fi
			WIN3x_USING_VWDRV=TRUE
		    fi
		fi

		# Save the win31 config file just for reference
		[ ! -f $DOTMERGE/win.cfw.upd ] &&
			cp $DOTMERGE/$WIN_CONFIG.cfw $DOTMERGE/$WIN_CONFIG.cfw.upd 2>/dev/null
	fi
	cd $CURDIR
	return
}

# Determine_Zoom() --
# Note: This function must be called after Check_Win_Upgrade() so that
# both WIN3x_TO_WIN95_UPGRADE and WIN3x_USING_VWDRV are set appropriately.
Determine_Zoom()
{
	# By default do not zoom.
	ZOOM_OPTION=-z
	eval `grep "^MERGE_INSTWIN_FORCE_ZOOM=" $DEFAULTMERGE`
	if [ "x$MERGE_INSTWIN_FORCE_ZOOM" = "xon" ]
	then
		Log Force zoom
		ZOOM_OPTION=+z
	fi

	# If this is a Win3x to Win95 upgrade and if that version
	# of windows uses the vw driver, then we must not zoom.
	# Otherwise we MUST zoom.
	if [ "$WIN3x_TO_WIN95_UPGRADE" = "TRUE" ]
	then
		if [ $WIN3x_USING_VWDRV = TRUE ]
		then
			Log Upgrade determines non-zoom.
			ZOOM_OPTION=-z
		else
			Log Upgrade determines zoom.
			ZOOM_OPTION=+z
		fi
	fi
	if [ "$ZOOM_OPTION" = "+z" ]
	then
		# When zooming, we need to make sure zooming is supported.
		# When zooming is possible, the XCONSOLENAME environment
		# variable normally has to be set and be a valid character
		# device.
		if [ "x$XCONSOLENAME" = "x" ]
		then
			ZOOM_OPTION=x
		elif [ ! -c "$XCONSOLENAME" ]
		then
			ZOOM_OPTION=x
		fi
		if [ "$ZOOM_OPTION" = "x" ]
		then
			Echo "Error 49: Zooming required but not available."
			Echo Cannot continue.  Exiting.
			Exit 49
		fi
	fi

}

# Given $1 the patch file and patchtarget $2
# use patchits -l lang option determin the langauge of
# the windows to install.
# This sets :
#   COUNTRY
#   WIN_CODESET
#   CODEPAGE
# These will get written to .boot/info.txt which will get
# read by dosexec and used when win or dos is run.
Setup_Win_Locale()
{
    Log Setup_Win_Locale

    $STATICMERGE/patch/patchit -l $HOME/.w4l_locale_info -p $1 $2
    if [ $? -eq 0 ] 
    then
	#
	# We have to change LANG to MLANG to avoid it being considered
	# as a locale to the shell
	#
	eval `cat $HOME/.w4l_locale_info | sed -e 's/LANG=/MLANG=/'`
    else 
	Log Cannot decide locale, defaulting to English
	COUNTRY=1
	WIN_CODESET=pc1252
	CODEPAGE=pc437
    fi
    rm -f $HOME/.w4l_locale_info
	
    Log COUNTRY=$COUNTRY CODEPAGE=$CODEPAGE WIN_CODESET=$WIN_CODESET
}

Determine_Win98Version_by_Kernel32()
{
	Log Determine_Win98Version_by_Kernel32_patches
	Log Using patchit to check Windows 98 version
	
	# TBD
	# We could use this to get the exact language version
	# by using the -l option of patchit
	# for now just check for second edition

	# for the WINDOWS_VERSION field (3rd filed), DO NOT name a 
	# version more than (VERNAMELEN(32) - 1) characters! because this 
	# version name will be read in in patch.c in em where its length 
	# is limited by VERNAMELEN defined there, any character byond 
	# that limit will be simply truncated!
	# and one should keep this WINDOWS_VERSION in sync with the one in
	# struct Win95VersionInfo array defined in patch.c!
	# NOTE: This WINDOWS_VERSION should also keep in sync with the same
	# variable in loadwindows.sh and should use that one as the standard!
	Win98_VerList="
	    wMEkernel32.pat:ME:MEorig:English/European
	    w98SEkernel32.pat:2ndEdition:98se:English/European 
	    w98SEzhkernel32.pat:2ndEdition:98sezhcn:Simplified_Chinese 
	    w98SEztkernel32.pat:2ndEdition:98sezhtw:Traditional_Chinese 
	    w98SEkrkernel32.pat:2ndEdition:98sekr:Korean 
	    w98SEJkernel32.pat:2ndEdition:98sejp:Japanese 
	    w98kernel32.pat:Original:98orig:English/European"
	
	set -- $Win98_VerList
	PATTARG=$VER_CHK_DIR/kernel32.dll

	while [ $# -gt 0 ]
	do
		eval `echo $1 | sed -e 's/\(.*\):\(.*\):\(.*\):\(.*\)/ \
					FILENAME=\1 WIN98_VERSION=\2 \
					WINDOWS_VERSION=\3 MSG=\4 /'`
		shift

		PATFILE=$STATICMERGE/patch/$FILENAME
		$STATICMERGE/patch/patchit -c -p $PATFILE $PATTARG
		STATUS=$?
		Log "patchit -v -c -p $PATFILE $PATTARG returned $STATUS"
		if [ $STATUS -eq 0 ]
		then
			case "$WINDOWS_VERSION" in
			ME*)
				Echo "$MSG Windows $WIN98_VERSION"
				;;
			*)
				Echo "$MSG Windows 98 $WIN98_VERSION"
				;;
			esac
			Setup_Win_Locale $PATFILE $PATTARG
			return
		fi
	done

	Echo "Unknown Windows 98 version!!!"
}

Determine_Win98Version_by_CabFile()
{
	Log Determine_Win98Version_by_CabFile

	# the following list has to be sorted as descent on the cab number
	# for the WINDOWS_VERSION field (3rd filed), DO NOT name a 
	# version more than (VERNAMELEN(32) - 1) characters! because this 
	# version name will be read in in patch.c in em where its length 
	# is limited by VERNAMELEN defined there, any character byond 
	# that limit will be simply truncated!
	# and one should keep this WINDOWS_VERSION in sync with the one in
	# struct Win95VersionInfo array defined in patch.c!
	# NOTE: This WINDOWS_VERSION should also keep in sync with the same
	# variable in loadwindows.sh and should use that one as the standard!
	Win98Cab_VerList="
	    win98_86.cab:2ndEdition:98sejp:Japanese 
	    win98_80.cab:2ndEdition:98sezhcn:Simplified_Chinese 
	    win98_76.cab:2ndEdition:98sezhtw:Traditional_Chinese 
	    win98_74.cab:2ndEdition:98se:English/European 
	    win98_73.cab:2ndEdition:98sekr:Korean 
	    win98_69.cab:Original:98orig:English/European"

	set -- $Win98Cab_VerList
	
	while [ $# -gt 0 ]
	do
		eval `echo $1 | sed -e 's/\(.*\):\(.*\):\(.*\):\(.*\)/ \
					FILENAME=\1 WIN98_VERSION=\2 \
					WINDOWS_VERSION=\3 MSG=\4 /'`
		shift

		if [ -f "$VER_CHK_DIR/FILENAME" ]
		then
			Echo "$MSG Windows 98 $WIN98_VERSION"
			Echo "No kernel32.dll file can not set LANGUAGE"
			Log No kernel32.dll file can not set LANGUAGE
			return
		fi
	done

	Echo "Unkown Windows 98 Version !!!"
}

Determine_Win95Version()
{
	# !!!!!!!!!!!!!  ATTENTION !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	# for the WINDOWS_VERSION variable, DO NOT name a 
	# version more than (VERNAMELEN(32) - 1) characters! because this 
	# version name will be read in in patch.c in em where its length 
	# is limited by VERNAMELEN defined there, any character byond 
	# that limit will be simply truncated!
	# and one should keep this WINDOWS_VERSION in sync with the one in
	# struct Win95VersionInfo array defined in patch.c!
	# NOTE: This WINDOWS_VERSION should also keep in sync with the same
	# variable in loadwindows.sh and should use that one as the standard!
	
	WIN_VERSION=win95
	# Note: We refer to "Classic" Win95 as "OSR1".  This
	# might not be strictly correct in the broad sense, but
	# it is sufficient for the version tests we have to do.
	WIN95_VERSION=OSR1
	WIN98_VERSION=not98
	# The presence of the file msinfo.inf indiates that a 
	# directory is an OSR2 setup directory.
	# Just to be safe we also check the contents of this file
	# to make sure it has the string "OSR2"

	INFOFILE="$VER_CHK_DIR/msinfo.inf"

	if [ -f $INFOFILE ]
	then
		SAVE_LOC=`pwd`
		cd "$VER_CHK_DIR"
		for F_NAME in */msinfo.inf
		do
			Log "Checking for file $F_NAME"
			if [ -f $F_NAME ]
			then
				INFOFILE="$VER_CHK_DIR/$F_NAME"
				if [ "z$SETUP_DIR" = "z" ]
				then
					SETUP_DIR=`echo $F_NAME \
					    | sed -e "s!/msinfo.inf!!"`
				fi
				break
			fi
		done
		cd $SAVE_LOC
	fi
	if [ -f $INFOFILE ]
	then
		grep -q OSR2 $INFOFILE
		if [ $? -eq 0 ]
		then
			Log Found OSR2.x version of windows
			WIN95_VERSION=OSR2
		fi
	fi
	if [ "$WIN95_VERSION" = "OSR1" -a "x$SETUP_DIR" = "x" ]
	then
		SAVE_LOC=`pwd`
		cd $VER_CHK_DIR
		for i in *
		do
			if [ -f $i/win95_04.cab ]
			then
				SETUP_DIR=$i
				break;
			fi
		done
		cd $SAVE_LOC
	fi
	PATTARG=$VER_CHK_DIR/kernel32.dll
	if [ "$WIN95_VERSION" = "OSR1" ]
	then
		PATFILE=$STATICMERGE/patch/w95kernel32.pat
		WINDOWS_VERSION=95classic
		Echo "Windows 95 Classic"
	else
		PATFILE=$STATICMERGE/patch/w95osr2kernel32.pat
		WINDOWS_VERSION=95osr2
		Echo "Windows 95 OSR2"
	fi
	Setup_Win_Locale $PATFILE $PATTARG
}

# Determine the directory on the CD where the setup command is.
# Validate and/or set the variable SETUP_DIR
# Also determine the version of Win9x there.
# (Sets WIN_VERSION, WIN95_VERSION and WIN98_VERSION)
Determine_CD_Setup()
{
	# Note, if SETUP_DIR is not set it means either the setup directory
	# is in the root of the mount point (or right at FROMDIRECTORY),
	# or, it means that we need to automatically find it as an immediate
	# subdirectory.
	Log Determine_CD_Setup

	if [ "x$FROMDIRECTORY" = "x" ]
	then
		DID_MOUNT=did_mount
		$STATICMERGE/mcd -d $MERGE_CD_DEV_NAME -m $VARMERGE/mnt -u
		$STATICMERGE/mcd -d $MERGE_CD_DEV_NAME -m $VARMERGE/mnt -c
		if [ "z$SETUP_DIR" != "z" ]
		then
			VER_CHK_DIR=$VARMERGE/mnt/$SETUP_DIR
		else
			VER_CHK_DIR=$VARMERGE/mnt
		fi
	else
		if [ "z$SETUP_DIR" != "z" ]
		then
			VER_CHK_DIR=$FROMDIRECTORY/$SETUP_DIR
		else
			VER_CHK_DIR=$FROMDIRECTORY
		fi
	fi
	if [ ! -d $VER_CHK_DIR ]
	then
		Echo "Error 39: $VER_CHK_DIR is not a directory!"
		Exit 39
	fi

	WIN_VERSION=""
	CHECK_LIST="win98#w98setup.bin winME#w9xsetup.bin"
	for CHECK_ITEM in $CHECK_LIST
	do
		CHECKVER="`expr $CHECK_ITEM : '\(.*\)#.*'`"
		CHECKFILE="`expr $CHECK_ITEM : '.*#\(.*\)'`"
		FULL_CHECKFILE="$VER_CHK_DIR/$CHECKFILE"

		Log "Checking for file $FULL_CHECKFILE (Ver $CHECKVER)"
		if [ -f "$FULL_CHECKFILE" ]
		then
			WIN_VERSION="$CHECKVER"
			break
		fi
		SAVE_LOC=`pwd`
		cd "$VER_CHK_DIR"
		for F_NAME in */$CHECKFILE
		do
			Log "Checking for file $F_NAME"
			if [ -f $F_NAME ]
			then
				WIN_VERSION="$CHECKVER"
				WIN98FILE="$VER_CHK_DIR/$F_NAME"
				if [ "z$SETUP_DIR" = "z" ]
				then
					SETUP_DIR=`echo $F_NAME \
						| sed -e "s!/$CHECKFILE!!"`
				fi
				break
			fi
		done
		cd $SAVE_LOC
		[ -n "$WIN_VERSION" ] && break
	done

	# Some fun facts about various English Windows 9x versions:
	#   Win95-classic - Upgrade:
	#		first win cab file: win95_02.cab
	#		last win cab file:  win95_17.cab
	#   Win95-classic - Full:
	#		first win cab file: win95_03.cab
	#		last win cab file:  win95_21.cab
	#   Win95-OSR2:
	#		first win cab file: win95_02.cab
	#		last win cab file:  win95_28.cab
	#   Win98-original:
	#		first win cab file: win98_22.cab
	#		last win cab file:  win98_69.cab
	#   Win98-second-edition:
	#		first win cab file: win98_21.cab
	#		last two win cabs:  win98_74.cab and win98_ol.cab
        #   Win98-second-simplified-Chinese-edition:
        #             first win cab file: win98_22.cab
        #             last win cab file:  win98_80.cab
        #   Win98-second-Korean-edition:
        #             first win cab file: win98_21.cab
        #             last win cab file:  win98_73.cab
        #   Win98-second-Japanese-edition:
        #             first win cab file: win98_23.cab
        #             last win cab file:  win98_86.cab
	if [ "$WIN_VERSION" = "win98" -o "$WIN_VERSION" = "winME" ]
	then
		if [ -f "$VER_CHK_DIR/kernel32.dll" ]
		then
			Determine_Win98Version_by_Kernel32
		else
			Determine_Win98Version_by_CabFile
		fi
		WIN95_VERSION=not95
	else
		Determine_Win95Version
	fi

	if [ -n "$DID_MOUNT" ]
	then
		$STATICMERGE/mcd -d $MERGE_CD_DEV_NAME -m $VARMERGE/mnt -u
		DID_MOUNT=
	fi

	Log SETUP_DIR=$SETUP_DIR

	return
}

# Determine the setup command to use.
Determine_Command()
{
	if [ "x$PHASE2_ONLY" != "x" ]
	then
		Log skipping Determine_Command
		return
	fi
	if [ "x$SETUPCMD" != "x" ]
	then
		Log SETUPCMD=$SETUPCMD
		return
	fi

	if [ "x$FROMDIRECTORY" = "x" ]
	then
		DID_MOUNT=did_mount
		$STATICMERGE/mcd -d $MERGE_CD_DEV_NAME -m $VARMERGE/mnt -u
		$STATICMERGE/mcd -d $MERGE_CD_DEV_NAME -m $VARMERGE/mnt -c
		if [ "z$SETUP_DIR" != "z" ]
		then
		    CMD_CHK_DIR=$VARMERGE/mnt/$SETUP_DIR
		else
		    CMD_CHK_DIR=$VARMERGE/mnt
		fi    
	else
		if [ "z$SETUP_DIR" != "z" ]
		then
		    CMD_CHK_DIR=$FROMDIRECTORY/$SETUP_DIR
		else
		    CMD_CHK_DIR=$FROMDIRECTORY
		fi    
	fi
	if [ ! -d $CMD_CHK_DIR ]
	then
		Echo "Error 39: $CMD_CHK_DIR is not a directory!"
		Exit 39
	fi

	Log Checking for setup command in directory $CMD_CHK_FILE

	# We will look for any of the Known names for the setup
	# program. Starting with the most likly to the least.
	if [ -f $CMD_CHK_DIR/"setup.exe" ]
	then
	    SETUPCMD="setup"
	elif [ -f $CMD_CHK_DIR/"install.exe" ]
	then
	    SETUPCMD="install"
	elif [ -f $CMD_CHK_DIR/"config.exe" ]
	then
	    SETUPCMD="config"
	elif [ -f $CMD_CHK_DIR/"instalar.exe" ]
	then
	    SETUPCMD="instalar"
	elif [ -f $CMD_CHK_DIR/"asenna.exe" ]
	then
	    SETUPCMD="asenna"
	elif [ -f $CMD_CHK_DIR/"instala.exe" ]
	then
	    SETUPCMD="instala"
	elif [ -f $CMD_CHK_DIR/"setupcur.exe" ]
	then
	    SETUPCMD="setupcur"
	elif [ -f $CMD_CHK_DIR/"instal.exe" ]
	then
	    SETUPCMD="instal"
	elif [ -f $CMD_CHK_DIR/"kur.exe" ]
	then
	    SETUPCMD="kur"
	else
		Echo "Error 39: Could not find setup command in $CMD_CHK_DIR!"
		Exit 39
	fi
	Log SETUPCMD=$SETUPCMD
	
	if [ -n "$DID_MOUNT" ]
	then
		$STATICMERGE/mcd -d $MERGE_CD_DEV_NAME -m $VARMERGE/mnt -u
		DID_MOUNT=
	fi
	return
}
 

# Get the product id (aka the "CD KEY") either from the setting used
# a previous time, or ask the user.
# The command line "productid" setting overrides this.
Get_Product_ID()
{
	if [ "x$PHASE2_ONLY" != "x" ]
	then
		if [ "$DO_NOT_ASK" != "yes" ]
		then
			Log skipping Get_Product_ID
		fi
		return
	fi
	if [ "x$PRODUCT_ID" != "x" ]
	then
		Log PRODUCT_ID=$PRODUCT_ID
		Text "PRODUCT_ID=$PRODUCT_ID" > $DOTMERGE/product_id
		return
	fi
	if [ "$DO_NOT_ASK" = "yes" -o "$DO_SILENT" = "silent" ]
	then
		# Do not ask, and use "nothing".  Plus do not save
		# this setting in the file.
		PRODUCT_ID=
		return
	fi
	while true
	do
		Echo "Please type in the CD \"Key\" for your Windows CD,"
		Echo "  and then press return."
		read PRODUCT_ID
		Echo
		Echo "You entered: $PRODUCT_ID"
		Echo "If correct, then press return."
		Echo "If not correct, then press \"n\" and then return."
		read UI
		[ "x$UI" = "x" ] && break
	done
	Text "PRODUCT_ID=$PRODUCT_ID" > $DOTMERGE/product_id
	Log PRODUCT_ID=$PRODUCT_ID
	return
}

# Function "Check_Space()" --
#	Make sure enough room exists installation.
Check_Space()
{
	Log Check_Space
	# Make sure there is enough room to install.
	# Pad slightly up (2MB) from the bare minimum.  Windows setup itself
	# checks for disk space required too.
	if [ "$WIN_VERSION" = "winME" ]
	then
		# Need at least 285MB to install WinME but we
		# say 300 for padding
		WINMB=300
	elif [ "$WIN_VERSION" = "win98" ]
	then
		if [ "$WIN98_VERSION" = "Original" ]
		then
			# Need at least 148MB to install Win98 Original.
			WINMB=150
		else
			# Need at least 195MB to install Win98 2nd Ed.
			WINMB=197
		fi
	else
		if [ "$WIN95_VERSION" = "OSR2" ]
		then
			# Need at least 93MB to install Win95 OSR2 safely.
			WINMB=95
		else
			# Need at least 41MB to install Win95 Classic safely.
			WINMB=43
		fi
	fi
	# Do a sync and sleep to hopefully give enough time
	# for the statfs that dfmerge does to return accurate numbers.
	sync
	sleep 1
	# Check the free space in the filesystem where the C drive is rooted.
	FREE_KB=`$STATICMERGE/dfmerge -k $PDIR`
	FREEMB=`expr $FREE_KB / 1024`
	if [ $FREEMB -lt $WINMB ]
	then
		Echo "!"
		Echo "Error 20: Not enough disk space to install Windows."
		Echo "Need $WINMB MB.   Available $FREEMB."
		Exit 20
	fi
	return
}

# Function "Get_Configs()" --
#	Set the list of configurations, CFGLIST, that need to be fix up.
#	Check all configurations (with non-DOS session type, != "dos")
#	to see if they have the C drive configured to the same location
#	as the "$WIN_CONFIG" configuration.  If they do then add them to the list
#	CFGLIST.
Get_Configs()
{
	WINCFG_CDRV=`winconfig $WIN_CONFIG list drive | grep "drive=c," | cut -d, -f3`

	CFGLIST="$WIN_CONFIG"
	for NAME in `listwinconfig`
	do
		[ "$NAME" = "$WIN_CONFIG" ] && continue

		SESS=`winconfig $NAME list session | cut -d= -f2`
		[ "$SESS" = "dos" ] && continue

		CDRV=`winconfig $NAME list drive |grep "drive=c," |cut -d, -f3`
		if [ "x$CDRV" = "x$WINCFG_CDRV" ]
		then
			CFGLIST="$CFGLIST $NAME"
		fi
	done
}

# Function "Fixup_Configs()" --
#	Make sure the Windows configurations to have at least 16M memory.
#	Also make sure that the local autoexec.bat and config.sys are used.
Fixup_Configs()
{
	Log Fixup_Configs

	# setup CFGLIST
	Get_Configs
	for NAME in $CFGLIST
	do
		MSG="in configuration $NAME"

		MEM=`winconfig $NAME list memory | cut -d= -f2`
		if [ "$WIN_VERSION" = "winME" ]
		then
			# ME requires 32MB
			MIN_MEMORY=32
		elif [ "$WIN_VERSION" = "win98" ]
		then
			if [ "$WIN98_VERSION" = "2ndEdition" ]
			then
				# Second Edition is a BLOATED PIG. (IE5 ?)
				MIN_MEMORY=24
			else
				# First Edition is a PIG.
				MIN_MEMORY=20
			fi
		else
			# 16MB is fine for Win95.  This is reasonable.
			MIN_MEMORY=16
		fi
		if [ $MEM -lt $MIN_MEMORY ]
		then
			Log "Changing memory setting from $MEM to $MIN_MEMORY $MSG"
			winconfig $NAME set memory=${MIN_MEMORY}
		fi
		XX=`winconfig $NAME list autoexeclocal | cut -d= -f2`
		if [ "$XX" != "true" ]
		then
			Log "Changing autoexeclocal to true $MSG"
			winconfig $NAME set autoexeclocal=true
		fi
		XX=`winconfig $NAME list configlocal | cut -d= -f2`
		if [ "$XX" != "true" ]
		then
			Log "Changing configlocal to true $MSG"
			winconfig $NAME set configlocal=true
		fi
		XX=`winconfig $NAME list windowsdir | cut -d= -f2`
		if [ "$XX" != "$WINDIR" ]
		then
			Log "Changing windowsdir to $WINDIR $MSG"
			winconfig $NAME set windowsdir="$WINDIR"
		fi

		# For drive C to use the Windows filename mapping style.
		#
		DRVLINE=`winconfig $NAME list drive | grep "drive=c,"`
		if [ "x$DRVLINE" != "x" ]
		then
			WIN95STYLE=`echo $DRVLINE | grep map_win95`
			MAPSTYLE=`echo $DRVLINE | grep map_`
			if [ "x$WIN95STYLE" != "x" ]
			then
				: Already Windows 95 mapping style
			elif [ "x$MAPSTYLE" = "x" ]
			then
				Log "Set Windows 9x filename mapping style" \
					$MSG
				winconfig $NAME set $DRVLINE,map_win95
			else
				DRVOPT="drive=c,"
				for jj in 2 3 4 5 6 7
				do
					OPT=`echo $DRVLINE | cut -d, -f$jj`
					MAPOPT=`echo "x$OPT" | grep map_`
					if [ "x$MAPOPT" != "x" ]
					then
						OPT="map_win95"
					fi
					if [ "x$OPT" = "x" ]
					then
						break
					fi

					DRVOPT="$DRVOPT,$OPT"
				done
				Log "Change filename mapping style to"\
					"Windows 9x style" $MSG
				winconfig $NAME set $DRVOPT
			fi
		fi
	done
}

# Function "Directory_setup()" --
#	This function is called from Source_Setup in the case when we are
#	installing from a UNIX directory and not from a "mscdex" attached CD.
#	This sets the "$WIN_CONFIG" configuration to have a drive letter
#	for this UNIX directory and sets SOURCE_DRIVE_LETTER to this drive letter.
Directory_Setup()
{
	Log Directory_Setup for $FROMDIRECTORY
	if [ "$FROMDIRECTORY" = "$DEFAULT_CAB_DIR" ]
	then
		# The J drive is already set up for this location.
		SOURCE_DRIVE_LETTER=j
		FROMDIRECTORY="$VARMERGE/dosroot"
		SETUP_DIR=wincabs
		Log Using existing drive $SOURCE_DRIVE_LETTER
		return
	fi

	winconfig $WIN_CONFIG list drive > $USEHOME/wdrives.$$
	# First see if there is already a drive letter set up for this
	# directory.
	[ "$OK_TO_LOG" = "ok_to_log" ] && cat $USEHOME/wdrives.$$ >> $LOGFILE

	for drive in i h g f e d
	do
		PATTERN="drive=$drive,${FROMDIRECTORY}/"
		# First search for a setting with an option (i.e. a comma)
		grep "${PATTERN}," $USEHOME/wdrives.$$ >> $NULL 2>&1
		if [ $? -eq 0 ]
		then
			SOURCE_DRIVE_LETTER=$drive
			Log Using existing drive $SOURCE_DRIVE_LETTER
			rm -f $USEHOME/wdrives.$$
			return
		fi
		# Now try for no option.
		grep "${PATTERN}'$'," $USEHOME/wdrives.$$ >> $NULL 2>&1
		if [ $? -eq 0 ]
		then
			SOURCE_DRIVE_LETTER=$drive
			Log Using existing drive $SOURCE_DRIVE_LETTER
			rm -f $USEHOME/wdrives.$$
			return
		fi
	done
	# So there are no existing drives (d thru i) that already
	# are set up for this directory, so find a free drive letter
	# to use for this purpose.
	for drive in i h g f e d
	do
		grep drive=$drive,none $USEHOME/wdrives.$$ >> $NULL 2>&1
		if [ $? -eq 0 ]
		then
			SOURCE_DRIVE_LETTER=$drive
			Log Seting up new drive $SOURCE_DRIVE_LETTER
			winconfig $WIN_CONFIG set drive=$drive,unix,$FROMDIRECTORY
			rm -f $USEHOME/wdrives.$$
			return
		fi
	done
	rm -f $USEHOME/wdrives.$$
	if [ "x$SOURCE_DRIVE_LETTER" = "x" ]
	then
		Echo "Error 34: Failed to find drive letter for directory install,"
		Exit 34
	fi
}

# Function "Check_WinME_Fromdir_Override()" --
# 	For winME we need to create a wincabs symlink to prevent
#	winME setup from making another copy of the wincabs directory.
#
Check_WinME_Fromdir_Override()
{
	if [ "$WIN_VERSION" = "winME" -a "$SOURCE_DRIVE_LETTER" = "j" -a \
	     "$FROMDIRECTORY" = "$VARMERGE/dosroot" -a \
	     "$SETUP_DIR" = "wincabs" ]
	then
		# For WinME we create a symlink into the "C" drive
		# and then install from there so that ME setup does
		# not make an extra copy of the wincabs directory
		# into windows/options/install
		FROMDIRECTORY="`echo $PDIR | sed -e 's,/$,,;'`"
		rm -f $PDIR/wincabs
		ln -s $VARMERGE/dosroot/wincabs $PDIR/wincabs
		SOURCE_DRIVE_LETTER=c
		Log "WinME: Override FROMDIRECTORY to $FROMDIRECTORY"
		Log "WinME: Override SOURCE_DRIVE_LETTER to " \
			"$SOURCE_DRIVE_LETTER"
	fi
}

# Function "Get_Configured_CD_ROM_Drive()" --
# 	Find the lowest CD drive letter in the "$WIN_CONFIG" configuration.
Get_Configured_CD_ROM_Drive()
{
	IN_CONFIG_CD=
	for ii in `winconfig $WIN_CONFIG list drive`
	do
		jj=`echo $ii | cut -d, -f2`
		if [ "x$jj" = "xcd" ]
		then
			IN_CONFIG_CD=`echo $ii | cut -d, -f1 | cut -d= -f2`
			return
		fi
	done
	return
}

# Function "Source_Setup()" --
#	Determine
#	Put the drive letter used for the CD-ROM in the variable "SOURCE_DRIVE_LETTER".
#	Warning, $PDIR must be set and exist for this to work properly.
#	(PDIR is set in Determine_Personal)
Source_Setup()
{
	if [ "x$PHASE2_ONLY" != "x" ]
	then
		Log skipping SourceSetup
		return
	fi
	if [ "x$SOURCE_DRIVE_LETTER" != "x" ]
	then
		Echo "Installing from drive letter $SOURCE_DRIVE_LETTER"
		return
	fi
	Log SourceSetup

	INAUTOEXEC=
	if [ -d $PDIR ]
	then
		grep -i mergecd1 $PDIR/autoexec.bat > $NULL 2> $NULL
		if [ $? -eq 0 ]
		then
			INAUTOEXEC=$PDIR/autoexec.bat
		fi
	fi
	if [ "x$INAUTOEXEC" = "x" ]
	then
		# It is not in personal drive autoexec.
		# So now if configured to use the global autoexec.bat,
		# and it exists too, then see if it has the mscdex line.
		XX=`winconfig $WIN_CONFIG list autoexecglobal | cut -d= -f2`
		if [ "$XX" = "true" -a -f $GLOBAL_AUTOEXEC ]
		then
			grep -i mergecd1 $GLOBAL_AUTOEXEC > $NULL 2> $NULL
			if [ $? -eq 0 ]
			then
				INAUTOEXEC=$GLOBAL_AUTOEXEC
			fi
		fi
	fi
	if [ "x$INAUTOEXEC" != "x" ]
	then
		LINE=`grep -i mergecd1 $INAUTOEXEC`

		# Now determine the drive letter for the CD-ROM drive.
		SOURCE_DRIVE_LETTER=
		for ii in 1 2 3 4 5 6 7 8
		do
			jj=`echo $LINE | cut -d/ -f$ii`
			case $jj in
			L:*)
				SOURCE_DRIVE_LETTER=`echo $jj | cut -c3`
				;;
			l:*)
				SOURCE_DRIVE_LETTER=`echo $jj | cut -c3`
				;;
			esac
		done
		Log "$INAUTOEXEC has SOURCE_DRIVE_LETTER $SOURCE_DRIVE_LETTER"
	fi

	# Call Get_CD_DEVICE to set MERGE_CD_DEV_NAME
	if [ "x$FROMDIRECTORY" != "x" ]
	then
		Get_CD_DEVICE no_fail
		RETVAL=$?
	else
		Get_CD_DEVICE
		RETVAL=0
	fi
	if [ $RETVAL -eq 0 ]
	then
		put_CD_in_config
	fi
	if [ "x$FROMDIRECTORY" != "x" ]
	then
		Directory_Setup
		INSTFROM="fromdir $FROMDIRECTORY"
	else
		Echo "Installing from default CD device: $MERGE_CD_DEV_NAME"
		INSTFROM="cddevice $MERGE_CD_DEV_NAME"
	fi
	return
}

put_CD_in_config()
{
	# Get_Configured_CD_ROM_Drive puts the CD drive letter setting
	# that is in the "$WIN_CONFIG" configuration into IN_CONFIG_CD, if any.
	Get_Configured_CD_ROM_Drive

	# If SOURCE_DRIVE_LETTER is not set, then use the setting from the "$WIN_CONFIG"
	# configuration, if any.
	if [ "x$SOURCE_DRIVE_LETTER" = "x" ]
	then
		if [ "x$IN_CONFIG_CD" != "x" ]
		then
			SOURCE_DRIVE_LETTER=$IN_CONFIG_CD
			Log "Using SOURCE_DRIVE_LETTER $SOURCE_DRIVE_LETTER from configuration."
			return
		fi
		# Nothing is set in the configuration so use the default "n".
		SOURCE_DRIVE_LETTER="n"
		Log "Using default SOURCE_DRIVE_LETTER $SOURCE_DRIVE_LETTER"
		Log "Configure to use SOURCE_DRIVE_LETTER $SOURCE_DRIVE_LETTER"
		winconfig $WIN_CONFIG set drive=$SOURCE_DRIVE_LETTER,cd,$MERGE_CD_DEV_NAME
		IN_CONFIG_CD=$SOURCE_DRIVE_LETTER
		return
	fi

	# At this point SOURCE_DRIVE_LETTER is set.
	# If it is the same as in the configuration, then nothing more to do.
	if [ "x$SOURCE_DRIVE_LETTER" = "x$IN_CONFIG_CD" ]
	then
		return
	fi
	If the configuration is not set with a CD drive then set it now.
	if [ "z$IN_CONFIG_CD" = "z" ]
	then
		Log "Configure to use SOURCE_DRIVE_LETTER $SOURCE_DRIVE_LETTER"
		winconfig $WIN_CONFIG set drive=$SOURCE_DRIVE_LETTER,cd,$MERGE_CD_DEV_NAME
		IN_CONFIG_CD=$SOURCE_DRIVE_LETTER
		return
	fi
	# At this point there is a conflict with the SOURCE_DRIVE_LETTER setting
	# and the IN_CONFIG_CD setting.  So remove all lower CD settings
	# from the configuration, and then set just the one we want.
	for ii in `winconfig $WIN_CONFIG list drive`
	do
		jj=`echo $ii | cut -d, -f2`
		if [ "x$jj" = "xcd" ]
		then
			DL=`echo $ii | cut -d, -f1 | cut -d= -f2`
			if [ $DL -lt $SOURCE_DRIVE_LETTER ]
			then
				winconfig $WIN_CONFIG set drive=$DL,none
			fi
		fi
	done
	Log "Configure to use SOURCE_DRIVE_LETTER $SOURCE_DRIVE_LETTER"
	winconfig $WIN_CONFIG set drive=$SOURCE_DRIVE_LETTER,cd,$MERGE_CD_DEV_NAME
	IN_CONFIG_CD=$SOURCE_DRIVE_LETTER

	Log "The drive letter for the CD is: $SOURCE_DRIVE_LETTER"
	return
}

Update_msbatch_for_vnet()
{
	# The function Query_vnet_info has already set these globals for
	# the matching msbatch settings:
	# VNET_COMPUTERNAME	ComputerName
	# VNET_WORKGROUP	Workgroup
	# VNET_DESCRIPTION	Description
	# VNET_LOGONDOMAIN	LogonDomain
	# VNET_DHCP		DHCP
	# VNET_IPADDRESS	IPAddress
	# VNET_IPMASK		IPMASK
	# VNET_HOSTNAME		Hostname
	# VNET_DOMAIN_N		Domain
	# VNET_DNSSERVERS	DNSServers
	# VNET_DOMAINORDER	DomainOrder
	# VNET_GATEWAYS		Gateways 

	TMPFILE=$HOME/ivnet.$$
	rm -f $TMPFILE
	cp $PDIR/msbatch.inf $TMPFILE
	cat $TMPFILE \
	 | sed -e "s/^ComputerName=VNET/ComputerName=\"$VNET_COMPUTERNAME\"/g" \
	 | sed -e "s/^Workgroup=VNET/Workgroup=\"$VNET_WORKGROUP\"/g" \
	 | sed -e "s/^Description=VNET/Description=\"$VNET_DESCRIPTION\"/g" \
	 | sed -e "s/^LogonDomain=VNET/LogonDomain=\"$VNET_LOGONDOMAIN\"/g" \
	 | sed -e "s/^DHCP=VNET/DHCP=\"$VNET_DHCP\"/g" \
	 | sed -e "s/^IPAddress=VNET/IPAddress=\"$VNET_IPADDRESS\"/g" \
	 | sed -e "s/^IPMask=VNET/IPMask=\"$VNET_IPMASK\"/g" \
	 | sed -e "s/^Hostname=VNET/Hostname=\"$VNET_HOSTNAME\"/g" \
	 | sed -e "s/^Domain=VNET/Domain=\"$VNET_DOMAIN_N\"/g" \
	 | sed -e "s/^DNSServers=VNET/DNSServers=\"$VNET_DNSSERVERS\"/g" \
	 | sed -e "s/^DomainOrder=VNET/DomainOrder=\"$VNET_DOMAINORDER\"/g" \
	 | sed -e "s/^Gateways=VNET/Gateways=\"$VNET_GATEWAYS\"/g" \
	> $PDIR/msbatch.inf
	rm -f $TMPFILE
}

Update_msbatch_for_winsock2()
{
	# The function Set_winsock2_defaults has already set these globals
	# for the matching msbatch settings:
	#   WS2_COMPUTERNAME
	#   WS2_WORKGROUP
	#   WS2_DESCRIPTION

	TMPFILE=$HOME/ivnet.$$
	rm -f $TMPFILE
	cp $PDIR/msbatch.inf $TMPFILE
	cat $TMPFILE \
	 | sed -e "s/^ComputerName=WS2/ComputerName=\"$WS2_COMPUTERNAME\"/g" \
	 | sed -e "s/^Workgroup=WS2/Workgroup=\"$WS2_WORKGROUP\"/g" \
	 | sed -e "s/^Description=WS2/Description=\"$WS2_DESCRIPTION\"/g" \
	> $PDIR/msbatch.inf
	rm -f $TMPFILE
}

Update_msbatch_default_printer()
{
	# on Linux, we want to install a default generic PostScript printer
	# on the local: port so printing "just works" out of the box, at least
	# in a way acceptable for most document types
	if [ "$SYSTEMTYPE" = "linux" ]; then
		TMPFILE=$HOME/iprint.$$
		rm -f $TMPFILE
		cp $PDIR/msbatch.inf $TMPFILE
		cat $TMPFILE \
	 	| sed -e "s/^\[Printers\]/;\[Printers\]/g" >$PDIR/msbatch.inf
		cat <<EOF >>$PDIR/msbatch.inf

[Printers]
Default (local) printer=Apple LaserWriter,local
EOF
		rm -f $TMPFILE
	fi
}

Setup_MSBATCH()
{
	if [ "x$PHASE2_ONLY" != "x" ]
	then
		Log skipping Setup_MSBATCH
		return
	fi
	TMPFILE=$HOME/i9x.$$
	Log Setup_MSBATCH

	# ---- Pick which msbatch.inf files for different setup types.
	if [ "$Use_Merge_Winsock" = "ws2" ]
	then
		SOURCE=$STATICMERGE/inst95/msbatch.ws2.inf
	elif [ "$Use_Merge_Winsock" = "ws1" ]
	then
		SOURCE=$STATICMERGE/inst95/msbatch.ws.inf
	else
		SOURCE=$STATICMERGE/inst95/msbatch.vn.inf
	fi

	Log Using $SOURCE

	# CUSTOMIZATION OF msbatch.inf here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	# Adjust the timezone setting if at all possible.
	TZSETTING=`sh $STATICMERGE/inst95/winzone.sh`

	USER_NAME=$LOGNAME
	USER_ORG=
	Log Setting TimeZone to $TZSETTING
	Log Setting InstallDIR to c:\\$WINDIR
	Log Setting Name to $USER_NAME
	Log Setting Org to $USER_ORG

	ProdKey=
	if [ "$CAB_DIR" -a -f $CAB_DIR/msbatch.inf ]
	then
		ProdKey="`grep 'ProductKey=' $CAB_DIR/msbatch.inf`"
	fi
	if [ -n "$PRODUCT_ID" ]
	then
		Log "Setting ProductID to $PRODUCT_ID"
		cat $SOURCE \
			| sed -e "s/^ProductID=XYZ/ProductID=$PRODUCT_ID/g" \
			> $TMPFILE
	elif [ -n "$ProdKey" -a "$USE_MSBATCH_PRODUCT_KEY" = "TRUE" ]
	then
		eval "$ProdKey"
		PRODUCT_KEY="`echo $ProductKey | tr -d '\r'`"
		Log "Got ProductKey from msbatch.inf: $PRODUCT_KEY"
		Log "Setting ProductKey to $PRODUCT_KEY"
		cat $SOURCE \
			| sed -e "s/^ProductID=XYZ/ProductKey=$PRODUCT_KEY/g" \
			> $TMPFILE
	else
		Log "No ProductID"
		cat $SOURCE \
			| sed -e "s/^ProductID=XYZ/;ProductID=/g" \
			> $TMPFILE
	fi
	if [ "$INTERACTIVE_CONFIG" = yes ]
	then
		EXPRESS_VAL=0
	else
		EXPRESS_VAL=1
	fi

	cat $TMPFILE \
		| sed -e "s/^TimeZone=XYZ/TimeZone=\"${TZSETTING}\"/g" \
		| sed -e "s/^InstallDir=XYZ/InstallDir=c:\\\\$WINDIR/g" \
		| sed -e "s/^Name=XYZ/Name=$USER_NAME/g" \
		| sed -e "s/^Org=XYZ/Org=$USER_ORG/g" \
		| sed -e "s/^Express=.*/Express=$EXPRESS_VAL/g" \
		> $PDIR/msbatch.inf
	rm -f $TMPFILE

	# If we are installing anything newer than OSR1
	# then we need to uncomment the entry in msbatch.inf to add NDIS
	# as a static VXD to the registry.  It is marked with an OSR2 tag.
	if [ "$WIN_VERSION" != "win95" -o "$WIN95_VERSION" != "OSR1" ]
	then
	    	ed - $PDIR/msbatch.inf >/dev/null 2>&1 <<-\EOF
		/^;OSR2-/
		s/;OSR2-//
		w
		q
EOF
	fi
	# When we are using the vnet stuff, we also have to fill in
	# the network info into the file.
	if [ "$Use_Merge_Winsock" = "not" ]
	then
		Update_msbatch_for_vnet
	elif [ "$Use_Merge_Winsock" = "ws2" ]
	then
		Update_msbatch_for_winsock2
	fi
	Update_msbatch_default_printer
	chmod +r $PDIR/msbatch.inf
}

# "Phase 1" of the installation is the part that does the initial load of
# the files from the CD-ROM, and then reboots.
# Requires the following globals to be already set:
#    PDIR, WINDIR, SOURCE_DRIVE_LETTER
Phase_1()
{
	if [ "x$PHASE1_OP" = "xuse" ]
	then
		Echo Using saved Phase 1 archive
		if [ -d $PDIR ]
		then
			rm -rf $PDIR
		fi
		mkdir $PDIR
		cd $PDIR
		cpio -icvdum < $SAVEFILE
		return
	fi
	if [ "x$PHASE2_ONLY" != "x" ]
	then
		Log skipping Phase_1
		return
	fi
	Log Phase_1 start
	Log `date`

	# Make sure that the "$WIN_CONFIG" configuration has session type "win3x"
	SESS=`winconfig $WIN_CONFIG list session | cut -d= -f2`
	if [ "x$SESS" != "xwin3x" ]
	then
		winconfig $WIN_CONFIG set session=win3x
	fi

	#
	# Since we have resizable windows, don't auto resize on each
	# session startup
	#
	winconfig $WIN_CONFIG set windowresizewin=false

	cd $PDIR
	if [ "$INST_FROM_CD" = "yes" ]
	then
		if [ "x$TRACE" = "xtrace" -a "$DO_SILENT" = "notsilent" ]
		then
			Echo Insert the Windows CD.
			Echo Press return when ready.
			read foo
		fi
	fi

	BAT_FILE=$PDIR/iwin_p1.bat
	rm -f $BAT_FILE
	DOSText "$SOURCE_DRIVE_LETTER:" > $BAT_FILE
	if [ "z$SETUP_DIR" != "z" ]
	then
		DOSText "cd $SETUP_DIR" | sed -e 's!/!\\!g' >> $BAT_FILE
	fi

	# If this is a Win3x to Win95 upgrade then invoke windows to run
	# the setup command.
	if [ "$WIN3x_TO_WIN95_UPGRADE" = "TRUE" ]
	then
		# This is the command run from the batch file
		# in a DOS session, so "win" must be on the
		# DOS search path for this to work.
		SETUPCMD="win $SETUPCMD"
	fi
	if [ "$ZOOM_OPTION" = "-z" -a "$WIN3x_TO_WIN95_UPGRADE" != "TRUE" ]
	then
		# When not zooming (and when not doing an upgrade from win31)
		# then we need to enable the trick to make Phase1 dosexec load
		# our windows31 vw display driver instead of the vga display
		# driver.  This must be done because the vga display driver
		# requires a zoom.
		MERGE_REDIRECT_VGADRV=on
		export MERGE_REDIRECT_VGADRV

		ORIG_SIZEAUTO="`winconfig $WIN_CONFIG list windowsizeauto`"
		ORIG_WIDTH="`winconfig $WIN_CONFIG list windowswidth`"
		ORIG_HEIGHT="`winconfig $WIN_CONFIG list windowsheight`"
		Log "ORIG_SIZEAUTO = $ORIG_SIZEAUTO"
		Log "ORIG_WIDTH = $ORIG_WIDTH"
		Log "ORIG_HEIGHT = $ORIG_HEIGHT"
		winconfig $WIN_CONFIG set windowswidth=736
		winconfig $WIN_CONFIG set windowsheight=552
		winconfig $WIN_CONFIG set windowsizeauto=false
	fi

	DOSText "@echo." >> $BAT_FILE

	#
	# For the command, convert forward slashes to back slashes
	# and convert option indicators " -" to " /".
	#
	# NOTE: The -p s=Adapter,Net options to setup speed up the HW
	# detection phase by skipping CD and Netcard detect.
	#
	if [ "$WIN_VERSION" = "winME" ]
	then
	    # Default to PS/2 mouse for winME
	    winconfig $WIN_CONFIG set mouse=ps2mouse
	    SUOPTS="-is -ih -iq"
	    if [ "$Use_Merge_Winsock" = "not" ]
	    then
	        DOSText "Z $SETUPCMD c:/msbatch.inf -p s=Adapter $SUOPTS" \
		    | sed -e 's!^Z !!' | sed -e 's!/!\\!g' \
		    | sed -e 's! -! /!g' >> $BAT_FILE
	    else
	        DOSText "Z $SETUPCMD c:/msbatch.inf -p s=Adapter,Net $SUOPTS" \
		    | sed -e 's!^Z !!' | sed -e 's!/!\\!g' \
		    | sed -e 's! -! /!g' >> $BAT_FILE
	    fi
	elif [ "$WIN_VERSION" = "win98" ]
	then
	    # Default to PS/2 mouse for win98
	    winconfig $WIN_CONFIG set mouse=ps2mouse
	    if [ "$Use_Merge_Winsock" = "not" ]
	    then
	        DOSText "Z $SETUPCMD c:/msbatch.inf -is -p s=Adapter -ih" \
		    | sed -e 's!^Z !!' | sed -e 's!/!\\!g' \
		    | sed -e 's! -! /!g' >> $BAT_FILE
	    else
	        DOSText "Z $SETUPCMD c:/msbatch.inf -is -p s=Adapter,Net -ih" \
		    | sed -e 's!^Z !!' | sed -e 's!/!\\!g' \
		    | sed -e 's! -! /!g' >> $BAT_FILE
	    fi
	else
	    DOSText "Z $SETUPCMD c:/msbatch.inf -is -p s=Adapter,Net" \
		| sed -e 's!^Z !!' | sed -e 's!/!\\!g' \
		| sed -e 's! -! /!g' >> $BAT_FILE
	fi

	#
	# Note that if phase 1 of install works properly, we won't get
	# to the pause we are adding to the batch file below, because
	# setup will do a "reboot" of the virtual machine.  Thus, we
	# only get to this pause if there was an error, so that pause
	# lets the user read it.
	#
	DOSText "@echo." >> $BAT_FILE
	DOSText "@echo -------------------------------------------------" \
						>> $BAT_FILE
	DOSText "@echo." >> $BAT_FILE
	DOSText "@echo For $MERGENAME support:" >> $BAT_FILE
	DOSText "@echo 	Please make a note of ALL messages listed above" \
						>> $BAT_FILE
	DOSText "@echo." >> $BAT_FILE
	DOSText "@pause" >> $BAT_FILE

	Echo Starting first phase of installation.

	# Turn on autozoom so if the startzoomed option (+z) is not set
	# we will zoom when needed.
	AUTOZOOM_SAVE=`winconfig $WIN_CONFIG list autozoom | cut -d= -f2`
	winconfig $WIN_CONFIG set autozoom=true

	Dos_Cmd $ARG_X +k$WIN_CONFIG -b $ZOOM_OPTION $AFLOP $BFLOP +r iwin_p1.bat

	winconfig $WIN_CONFIG set autozoom=$AUTOZOOM_SAVE

	Echo Finished first phase of installation.
	unset MERGE_REDIRECT_VGADRV

	[ "x$TRACE" != "xtrace" ] && rm -f $BAT_FILE

	if [ ! -d $WINDIR -o ! -f $WINDIR/system/kernel32.dll ]
	then
		Echo "Error 24: Windows failed the initial install."
		Echo Cannot continue.  Exiting.
		Exit 24
	fi
	if [ "x$PHASE1_OP" = "xsave" ]
	then
		cd $PDIR
		if [ -d $SAVEFILE ]
		then
			Echo "Error 25: $SAVEFILE is a directory!"
			Clean_Up
			Exit 25
		fi
		Echo Saving the state of phase 1 in $SAVEFILE
		find . -print | cpio -oc > $SAVEFILE
	fi
	if [ "x$TRACE" = "xtrace" ]
	then
		Echo date
		Echo 
		Echo End of first phase of installation.
		if [ "$DO_SILENT" = "notsilent" ]
		then
			Echo Press return to continue.
			Echo 
			read foo
		fi
	fi
}

# Add the MRGBLK entries to the Registry by using the DOS version
#   of regedit
Add_Mrgblk_To_Registry()
{
	# Fire up dos, so it can add needed entries to the registry.
	Log Add_Mrgblk_To_Registry

	BAT1_FILE="$PDIR/iwin_mb1.bat"
	BAT2_FILE="$PDIR/iwin_mb2.bat"
	rm -f $BAT1_FILE
	DOSText "c: " > $BAT1_FILE
	DOSText "cd \\ "  >> $BAT1_FILE
	DOSText "regedit /E c:\\\\regget " >> $BAT1_FILE

	# Export the registry contents from the .dat files
	Dos_Cmd +k$WIN_CONFIG +b +r iwin_mb1.bat >> $LOGFILE 2>&1
	grep -i mrgblkinstall $PDIR/regget > $NULL 2> $NULL
	if [ $? -ne 0 ]
	then
		Log "Adding mrgblk values to registry"
		DOSText "c: " > $BAT2_FILE
		DOSText "cd \\ "  >> $BAT2_FILE
		DOSText "regedit c:\\\\$WINDIR\\\\mrgblk.reg " \
				>> $BAT2_FILE
		Dos_Cmd +k$WIN_CONFIG +b +r iwin_mb2.bat >> $LOGFILE 2>&1
	fi
	if [ "x$TRACE" != "xtrace" ]
	then
		rm -f $PDIR/regget
		rm -f $PDIR/regput
		rm -f $BAT1_FILE
		rm -f $BAT2_FILE
	fi
}

# Add welcome entry to the Registry by using the DOS version
#   of regedit.  We only need to do this for Win98 Orig.  Win95
#   classic and OSR2 as well as win98 SE already don't need this
#   to cause welcome.exe to run.
Add_Welcome_To_Registry()
{
	if [ "$WINDOWS_VERSION" != "98orig" ]
	then
		Log "Don't need to add welcome to registry"
		Log "Winversion = $WINDOWS_VERSION"
		return
	fi

	# Fire up dos, so it can add needed entries to the registry.
	Log Add_Welcome_To_Registry
	WELCOME_REG="$STATICMERGE/inst95/welcome.reg"
	if [ ! -f $WELCOME_REG ]
	then
		Log "Missing file $WELCOME_REG"
		return
	fi

	BAT1_FILE="$PDIR/iwin_mb1.bat"
	rm -f $BAT1_FILE
	cp $WELCOME_REG $PDIR

	Log "Adding welcome values to registry"
	DOSText "c: " > $BAT1_FILE
	DOSText "cd \\ "  >> $BAT1_FILE
	DOSText "regedit welcome.reg " >> $BAT1_FILE
	Dos_Cmd +k$WIN_CONFIG +b +r iwin_mb1.bat >> $LOGFILE 2>&1

	if [ "x$TRACE" != "xtrace" ]
	then
		rm -f $BAT1_FILE
		rm -f $PDIR/welcome.reg
	fi
}

# Add wshutdn entry to the Registry by using the DOS version
#   of regedit.  We only need to do this for WinME.
Add_Wshutdn_To_Registry()
{
	# Fire up dos, so it can add needed entries to the registry.
	Log Add_Wshutdn_To_Registry
	WSHUTDN_REG="$STATICMERGE/inst95/wshutdn.reg"
	if [ ! -f $WSHUTDN_REG ]
	then
		Log "Missing file $WSHUTDN_REG"
		return
	fi
	CURDIR=`pwd`
	cd $PDIR

	BAT1_FILE="$PDIR/iwin_mb1.bat"
	rm -f $BAT1_FILE
	cp $WSHUTDN_REG $PDIR

	Log "Adding wshutdn values to registry"
	DOSText "c: " > $BAT1_FILE
	DOSText "cd \\ "  >> $BAT1_FILE
	DOSText "regedit wshutdn.reg " >> $BAT1_FILE
	Dos_Cmd +k$WIN_CONFIG +b +r iwin_mb1.bat >> $LOGFILE 2>&1

	if [ "x$TRACE" != "xtrace" ]
	then
		rm -f $BAT1_FILE
		rm -f $PDIR/wshutdn.reg
	fi
	cd $CURDIR
}

Remove_IntroVideo_From_Registry()
{
	# Fire up dos, so it can add needed entries to the registry.
	Log Remove_IntroVideo_From_Registry

	CURDIR=`pwd`
	cd $PDIR
	BAT1_FILE="$PDIR/iwin_mb1.bat"
	BAT2_FILE="$PDIR/iwin_mb2.bat"
	rm -f $BAT1_FILE
	DOSText "c: " > $BAT1_FILE
	DOSText "cd \\ "  >> $BAT1_FILE
	DOSText "regedit /E c:\\\\regget " >> $BAT1_FILE

	# Export the registry contents from the .dat files
	Dos_Cmd +k$WIN_CONFIG +b +r iwin_mb1.bat >> $LOGFILE 2>&1
	sed -e '/Intro Video.*=.*content.hta/d' \
	    $PDIR/regget > $PDIR/regput 2>$NULL

	Log "Reconstructing regsitry without intro video"
	DOSText "c: " > $BAT2_FILE
	DOSText "cd \\ "  >> $BAT2_FILE
	DOSText "regedit /C c:\\\\regput " >> $BAT2_FILE
	Dos_Cmd +k$WIN_CONFIG +b +r iwin_mb2.bat >> $LOGFILE 2>&1
	if [ "x$TRACE" != "xtrace" ]
	then
		rm -f $PDIR/regget
		rm -f $PDIR/regput
		rm -f $BAT1_FILE
		rm -f $BAT2_FILE
	fi
	if [ "x$TRACE" = "xtrace" -a "$DO_SILENT" = "notsilent" ]
	then
		Echo 
		Echo "Remove_IntroVideo_From_Registry done"
		Echo Press return to continue.
		Echo 
		read foo
	fi
	cd $CURDIR
}

# Add the Generic Merge Monitor entries to the Registry by using the
# DOS version of regedit
Add_MrgMonitor_To_Registry()
{
	# Fire up dos, so it can add needed entries to the registry.
	Log Add_MrgMonitor_To_Registry

	BAT1_FILE="$PDIR/iwin_mb1.bat"
	BAT2_FILE="$PDIR/iwin_mb2.bat"
	rm -f $BAT1_FILE
	DOSText "c: " > $BAT1_FILE
	DOSText "cd \\ "  >> $BAT1_FILE
	DOSText "regedit /E c:\\\\regget " >> $BAT1_FILE

	# Export the registry contents from the .dat files
	Dos_Cmd +k$WIN_CONFIG +b +r iwin_mb1.bat >> $LOGFILE 2>&1
	grep -i Mrgmon_Install $PDIR/regget > $NULL 2> $NULL
	if [ $? -ne 0 ]
	then
		Log "Adding Merge Monitor values to registry"
		DOSText "c: " > $BAT2_FILE
		DOSText "cd \\ "  >> $BAT2_FILE
		if [ "$WIN_VERSION" = "win98" -o "$WIN_VERSION" = "winME" ]
		then
		      DOSText "regedit c:\\\\$WINDIR\\\\mrgmon98.reg " \
				>> $BAT2_FILE
		else
		      DOSText "regedit c:\\\\$WINDIR\\\\mrgmon95.reg " \
				>> $BAT2_FILE
		fi
		Dos_Cmd +k$WIN_CONFIG +b +r iwin_mb2.bat >> \
			 $LOGFILE 2>&1
	fi
	if [ "x$TRACE" != "xtrace" ]
	then
		rm -f $PDIR/regget
		rm -f $PDIR/regput
		rm -f $BAT1_FILE
		rm -f $BAT2_FILE
	fi
}

# If this is OSR2.5 we must remove Internet explorer from the registry
#   so Windows will not try to install it!
Del_Ie4_From_Registry()
{
	# Fire up dos, so it can del needed entries from the registry.
	Log Del_Ie4_To_Registry

	BAT1_FILE="$PDIR/iwin_ie1.bat"
	BAT2_FILE="$PDIR/iwin_ie2.bat"
	rm -f $BAT1_FILE
	DOSText "c: " > $BAT1_FILE
	DOSText "cd \\ "  >> $BAT1_FILE
	DOSText "regedit /E c:\\\\regget " >> $BAT1_FILE

	# Export the registry contents from the .dat files
	Dos_Cmd +k$WIN_CONFIG +b +r iwin_ie1.bat >> $LOGFILE 2>&1
	grep -i ie4setup $PDIR/regget > $NULL 2> $NULL
	if [ $? -eq 0 ]
	then
		ISOSR25=yes
		Log "Found ie4setup this is OSR2.5 or newer"
		Log "Removing ie4 values from registry"
		rm -f $PDIR/$WINDIR/system.dat
		rm -f $PDIR/$WINDIR/user.dat
		rm -f $BAT2_FILE
		grep -v ie4setup $PDIR/regget |grep -v jdbgmgr >$PDIR/regput
		DOSText "c: " > $BAT2_FILE
		DOSText "cd \\ "  >> $BAT2_FILE
		DOSText "regedit /C c:\\\\regput " >> $BAT2_FILE
		Dos_Cmd +k$WIN_CONFIG +b +r iwin_ie2.bat >> \
			 $LOGFILE 2>&1
		rm -f $PDIR/regput
	fi
	if [ "x$TRACE" != "xtrace" ]
	then
		rm -f $PDIR/regget
		rm -f $PDIR/regput
		rm -f $BAT1_FILE
		rm -f $BAT2_FILE
	fi
	if [ -f $PDIR/$WINDIR/inf/ohare.inf ]
	then
		grep -i ie4setup $PDIR/$WINDIR/inf/ohare.inf | \
			grep -v jdbgmgr > $NULL 2> $NULL
		if [ $? -eq 0 ]
		then
			ISOSR25=yes
			Log "Found ie4setup in inf file"
			Log "removing ie4 from inf file"
			grep -v ie4setup $PDIR/$WINDIR/inf/ohare.inf \
				> $PDIR/$WINDIR/inf/ohare.new
			mv $PDIR/$WINDIR/inf/ohare.inf \
				$PDIR/$WINDIR/inf/ohare.old
			mv $PDIR/$WINDIR/inf/ohare.new \
				$PDIR/$WINDIR/inf/ohare.inf
		fi
	fi

	if [ "$WIN_VERSION" = "winME" ]
	then
		Echo "Found Windows ME (Millennium Edition)"
	elif [ "$WIN_VERSION" = "win98" ]
	then
		if [ "$WIN98_VERSION" = "2ndEdition" ]
		then
			Echo "Found Windows98 2nd Edition"
		else
			Echo "Found Windows98 Original version"
		fi
	elif [ "$WIN_VERSION" = "win95" ]
	then
		if [ "x$ISOSR25" = "xyes" ]
		then
			Echo "Found Windows95 OSR2.5"
		elif [ "$WIN95_VERSION" = "OSR2" ]
		then
			Echo "Found Windows95 OSR2.0"
		else
			Echo "Found Classic Windows95"
		fi
	fi
}

# On Windows ME the NIC fails to get added from msbatch.inf
# so we add it to the registry during magic step
Add_Nic_To_Registry()
{
	case "$WINDOWS_VERSION" in
	ME*)
	    # When we are using the vnet stuff, then we have to fix
	    # NIC network info for ME.
	    if [ "$Use_Merge_Winsock" = "not" ]
	    then

		# Fire up dos, so it can add needed entries to the registry.
		Log Add_NIC_To_Registry
		NIC_REG="$STATICMERGE/inst95/menic.reg"
		if [ ! -f $NIC_REG ]
		then
		    Log "Missing file $NIC_REG"
		    return
		fi

		BAT1_FILE="$PDIR/iwin_mb1.bat"
		rm -f $BAT1_FILE
		cp $NIC_REG $PDIR

		Log "Adding NIC values to registry"
		DOSText "c: " > $BAT1_FILE
		DOSText "cd \\ "  >> $BAT1_FILE
		DOSText "regedit menic.reg " >> $BAT1_FILE
		Dos_Cmd +k$WIN_CONFIG +b +r iwin_mb1.bat >> $LOGFILE 2>&1

		if [ "x$TRACE" != "xtrace" ]
		then
			rm -f $BAT1_FILE
			rm -f $PDIR/menic.reg
		fi
		
	    fi
	    ;;
	*)
	    Log "Don't need to add NIC to registry"
	    Log "Winversion = $WINDOWS_VERSION"
	    return
	    ;;
	esac


}

Munge_Registry_for_VW_95()
{
	# Run the DOS batch script that updates the registry.
	# This is only used for Windows 95 version.
	Log "Preparing VW"

	BAT1_FILE="$PDIR/iwin_vw1.bat"
	BAT2_FILE="$PDIR/iwin_vw2.bat"
	rm -f $BAT1_FILE
	DOSText "c: " > $BAT1_FILE
	DOSText "cd \\ "  >> $BAT1_FILE
	DOSText "regedit /E c:\\\\regget " >> $BAT1_FILE

	# Export the registry contents from the .dat files
	Dos_Cmd +k$WIN_CONFIG +b +r iwin_vw1.bat >> $LOGFILE 2>&1
	# Now made the needed changes to the text registry.
	# Change these lines:
# [HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Class\Display\0000]
# "Ver"="4.0"
# "DevLoader"="*vdd"
# "InfPath"="msdisp.inf"
# "InfSection"="VGA"
# "DriverDesc"="VGA"
	# To:
# [HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Class\Display\0000]
# "InfPath"="MSDISP.INF"
# "InfSection"="VWDisp_Install"
# "DriverDesc"="Merge Windows/X (Win 95 version)"
# "Ver"="4.0"
# "MergeVer"=hex:00,00,00,04
# "DevLoader"="*vdd"
	#
 	#
	# And change these lines:
# [HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Class\Display\0000\DEFAULT]
# "Mode"="4,640,480"
# "drv"="vga.drv"
# "vdd"="*vdd"
	# To:
# [HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Class\Display\0000\DEFAULT]
# "Mode"="4,640,480"
# "drv"="vw95.drv"
# "minivdd"="vwvdd.vxd"
# "RefreshRate"="-1"
# "ExtModeSwitch"="0"
# "CHIPID"=hex:57,56,00,00,00,00
# "vdd"="*vdd"
	#
	#

	cat $PDIR/regget \
	| sed -e 's%"InfSection"="VGA"%"InfSection"="VWDisp_Install"'% \
	| sed -e 's%DriverDesc"="VGA"%DriverDesc"="Merge Windows/X (Win 95 version)"\
"Ver"="4.0"\
"MergeVer"=hex:00,00,00,04\
%' \
	| sed -e 's%"drv"="vga.drv"%"drv"="vw95.drv"\
"minivdd"="vwvdd.vxd"\
"RefreshRate"="-1"\
"ExtModeSwitch"="0"\
"CHIPID"=hex:57,56,00,00,00,00\
%' \
	| $STATICMERGE/charconv -p -x > $PDIR/regput

	# Replace the real registry from the updated file.
	rm -f $PDIR/$WINDIR/system.dat
	rm -f $PDIR/$WINDIR/user.dat
	rm -f $BAT2_FILE
	DOSText "c: " > $BAT2_FILE
	DOSText "cd \\ "  >> $BAT2_FILE
	DOSText "regedit /C c:\\\\regput " >> $BAT2_FILE
	Dos_Cmd +k$WIN_CONFIG +b +r iwin_vw2.bat >> $LOGFILE 2>&1
	if [ "x$TRACE" != "xtrace" ]
	then
		rm -f $PDIR/regget
		rm -f $PDIR/regput
		rm -f $BAT1_FILE
		rm -f $BAT2_FILE
	fi
	if [ "x$TRACE" = "xtrace" ]
	then
		cp $PDIR/$WINDIR/user.dat   $HOME/user.dat.vw
		cp $PDIR/$WINDIR/system.dat $HOME/system.dat.vw
		chmod +w $HOME/user.dat.vw
		chmod +w $HOME/system.dat.vw
	fi
}

Setup_InternetExplorer()
{
	# Don't allow keyboard interrupts during this very
	# sensitive stage or the registry can become corrupt
	trap '' 2

	Echo "Updating registry for Internet Explorer."

	# Run the DOS batch script that updates the registry.
	cd "$PDIR"
	BAT1_FILE=iwin_ie1.bat
	BAT2_FILE=iwin_ie2.bat
	FULL_BAT1="$PDIR"/$BAT1_FILE
	FULL_BAT2="$PDIR"/$BAT2_FILE
	GET_FILE=iwin_ie1.reg
	PUT_FILE=iwin_ie2.reg
	FULL_GET="$PDIR"/$GET_FILE
	FULL_PUT="$PDIR"/$PUT_FILE
	rm -f "$FULL_BAT1" "$FULL_BAT2"
	rm -f "$FULL_GET"  "$FULL_PUT"

	# Now made the needed changes to the text registry.

	if [ "$WIN_VERSION" = "win98" -o "$WIN_VERSION" = "winME" ]
	then
		Log Doing win98
		# For 98 we change this:

		#[HKEY_USERS\.DEFAULT\Software\Microsoft\Internet Connection Wizard]
		#"DesktopChanged"=dword:00000001

		# To this:

		#[HKEY_USERS\.DEFAULT\Software\Microsoft\Internet Connection Wizard]
		#"DesktopChanged"=dword:00000001
		#"Completed"=hex:01,00,00,00

		# So first extract the setting.
		DOSText "c: " > "$BAT1_FILE"
		DOSText "cd \\ "  >> "$BAT1_FILE"
		DOSText "regedit /E $GET_FILE \
\"HKEY_USERS\\.DEFAULT\\Software\\Microsoft\\Internet Connection Wizard\"" \
			>> "$BAT1_FILE"
		Dos_Cmd +k$WIN_CONFIG +b +r $BAT1_FILE >> "$LOGFILE" 2>&1
		RET_VAL=$?
		# Make sure the batch file ran and produced a file.
		if [ ! -f "$FULL_GET" ]
		then
			Log "Failed to extract the info"
			Log "dosexec returned $RET_VAL"
			Log "Add required info and continue install"
			DOSText "REGEDIT4" > "$FULL_PUT"
			DOSText "" >> "$FULL_PUT"
			DOSText "[HKEY_USERS\\.DEFAULT\\Software\\Microsoft\\Internet Connection Wizard]" >> "$FULL_PUT"
			DOSText "\"DesktopChanged\"=dword:00000001" >> "$FULL_PUT"
			DOSText "\"Completed\"=hex:01,00,00,00" >> "$FULL_PUT"
		else
			cat "$FULL_GET" \
			| sed -e 's%"DesktopChanged".*$%&\
"Completed"=hex:01,00,00,00'% \
			| $STATICMERGE/charconv -p -x > "$FULL_PUT"
		fi
	else 
		Log Doing win95
		# It's Win95 so we change this:

		#[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{FBF23B42-E3F0-101B-8488-00AA003E56F8}\Shell\Open\Command]
		#@="\"C:\\Program Files\\ICW-Internet Connection Wizard\\icwconn1.exe\""

		# To this:

		#[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{FBF23B42-E3F0-101B-8488-00AA003E56F8}\Shell\Open\Command]
		#@="C:\.Progra~1\\Intern~1\\iexplore.exe"

		# So first extract the setting.
		DOSText "c: " > "$BAT1_FILE"
		DOSText "cd \\ "  >> "$BAT1_FILE"
		DOSText "regedit /E $GET_FILE \
\"HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes\\CLSID\\{FBF23B42-E3F0-101B-8488-00AA003E56F8}\\Shell\\Open\\Command\"" \
			>> "$BAT1_FILE"
		Dos_Cmd +k$WIN_CONFIG +b +r $BAT1_FILE >> "$LOGFILE" 2>&1
		RET_VAL=$?
		# Make sure the batch file ran and produced a file.
		if [ ! -f "$FULL_GET" ]
		then
			Log "Failed to extract the info"
			Log "dosexec returned $RET_VAL"
			Log "Add required info and continue install"
			DOSText "REGEDIT4" > "$FULL_PUT"
			DOSText "" >> "$FULL_PUT"
			DOSText "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes\\CLSID\\{FBF23B42-E3F0-101B-8488-00AA003E56F8}\\Shell\\Open\\Command]" >> "$FULL_PUT"
			DOSText "@=\"\\\"C:\\\\Progra~1\\\\Intern~1\\\\iexplore.exe\\\"\"" >> "$FULL_PUT"
		else
			cat "$FULL_GET" \
			| sed -e 's%Program Files\\\\ICW-Internet Connection Wizard\\\\icwconn1.exe%Progra~1\\\\Intern~1\\\\iexplore.exe'% \
			| $STATICMERGE/charconv -p -x > "$FULL_PUT"
		fi
	fi

	DOSText "c: " > "$FULL_BAT2"
	DOSText "cd \\ "  >> "$FULL_BAT2"
	DOSText "regedit $PUT_FILE" >> "$FULL_BAT2"
	Dos_Cmd +k$WIN_CONFIG +b +r $BAT2_FILE >> $LOGFILE 2>&1
	if [ "x$TRACE" != "xtrace" ]
	then
		rm -f $FULL_GET
		rm -f $FULL_PUT
		rm -f $FULL_BAT1
		rm -f $FULL_BAT2
	fi
	trap interruptProc 2
} 

Fix_Printer()
{
	Echo "Fixing Default Printer."

	# Run the DOS batch script that updates the registry.
	cd "$PDIR"
	PFIX_BAT="$PDIR"/pfix.bat
	DOSText "c: " > "$PFIX_BAT"
	DOSText "cd \\ "  >> "$PFIX_BAT"
	DOSText "regedit j:\mwin95\printfix.reg" >> "$PFIX_BAT"
	Dos_Cmd +k$WIN_CONFIG +b +r pfix.bat  >> $LOGFILE 2>&1
	if [ "x$TRACE" != "xtrace" ]
	then
		rm -f $PFIX_BAT
	fi
} 

Add_Sound_To_Registry()
{
	if [ -f "$STATICMERGE/inst95/mrgsnd.reg" ]
	then
		cp $STATICMERGE/inst95/mrgsnd.reg $WINDIR
		chmod +r $WINDIR/mrgsnd.reg

		# Run the DOS batch script that updates the registry.

		BAT_FILE="$PDIR/iwin_snd.bat"
		rm -f $BAT_FILE
		DOSText "c: " > $BAT_FILE
		DOSText "cd \\ "  >> $BAT_FILE
		DOSText "regedit c:\\\\$WINDIR\\\\mrgsnd.reg " \
			>> $BAT_FILE
		
		# Export the registry contents from the .dat files
		Dos_Cmd +k$WIN_CONFIG +b +r iwin_snd.bat \
			>> $LOGFILE 2>&1

		if [ "x$TRACE" != "xtrace" ]
		then
			rm -f $BAT_FILE
		fi
		if [ "x$TRACE" = "xtrace" ]
		then
			cp $PDIR/$WINDIR/user.dat $HOME/user.dat.snd
			cp $PDIR/$WINDIR/system.dat $HOME/system.dat.snd
			chmod +w $HOME/user.dat.snd
			chmod +w $HOME/system.dat.snd
		fi
	else
	    Echo "Skipping Sound setup. No registry files found"
	fi

} 

Smaller_x()
{
	if [ $1 -ge 1920 ] ; then
		NEW_X=1600
		return
	fi
	if [ $1 -ge 1600 ] ; then
		NEW_X=1280
		return
	fi
	if [ $1 -ge 1280 ] ; then
		NEW_X=1024
		return
	fi
	if [ $1 -ge 1024 ] ; then
		NEW_X=800
		return
	fi
	if [ $1 -ge 800 ] ; then
		NEW_X=640
		return
	fi
	NEW_X=600
	return
}

Smaller_y()
{
	if [ $1 -ge 1440 ] ; then
		NEW_Y=1200
		return
	fi
	if [ $1 -ge 1200 ] ; then
		NEW_Y=1024
		return
	fi
	if [ $1 -ge 1024 ] ; then
		NEW_Y=768
		return
	fi
	if [ $1 -ge 768 ] ; then
		NEW_Y=600
		return
	fi
	if [ $1 -ge 600 ] ; then
		NEW_Y=480
		return
	fi
	NEW_Y=400
	return
}

Set_Default_Window_Size()
{

	DISPLAY_XY=`xdpyinfo 2>/dev/null | grep "dimensions:"  \
		| while read A B C ; do echo $B ; break ; done`

	[ -z "$DISPLAY_XY" ] && return
 
	Smaller_x `echo $DISPLAY_XY | cut -dx -f1`
	XX=$NEW_X
	Smaller_y `echo $DISPLAY_XY | cut -dx -f2`
	YY=$NEW_Y
 
	FORCE_XY="${XX}x${YY}"
	[ -z "$FORCE_XY" ] && return

	Log "$STATICMERGE"/bin/setres -g $FORCE_XY $WIN_CONFIG
	"$STATICMERGE"/bin/setres -g $FORCE_XY $WIN_CONFIG

	MSDISP_FILE="$1"
	[ -z "$MSDISP_FILE" ] && return

	MSD_TMPFILE=$HOME/.merge/.wmsdisp.tmp
	cp $MSDISP_FILE $MSD_TMPFILE
	chmod +w $MSDISP_FILE
	sed -e "s/8,800,600/8,${XX},${YY}/" $MSD_TMPFILE > $MSDISP_FILE
	rm -f $MSD_TMPFILE
	chmod -w $MSDISP_FILE
	winconfig $WIN_CONFIG set windowsizeauto=false
	winconfig $WIN_CONFIG set "windowswidth=${XX}"
	winconfig $WIN_CONFIG set "windowsheight=${YY}"

	# Make this the new default
	ORIG_WIDTH="`winconfig $WIN_CONFIG list windowswidth`"
	ORIG_HEIGHT="`winconfig $WIN_CONFIG list windowsheight`"
}

# The "Magic_Step" is run between Phase 1 and Phase 2.
Magic_Step()
{
	Echo "Preparing for second phase of installation"

	Log Magic_Step

	if [ "x$TRACE" = "xtrace" -a "$DO_SILENT" = "notsilent" ]
	then
		Echo 
		Echo "WIN_VERSION = $WIN_VERSION"
		Echo Press return to continue.
		Echo 
		read foo
	fi

	[ -n "$ORIG_SIZEAUTO" ] && winconfig $WIN_CONFIG set "$ORIG_SIZEAUTO"
	[ -n "$ORIG_WIDTH" ] && winconfig $WIN_CONFIG set "$ORIG_WIDTH"
	[ -n "$ORIG_HEIGHT" ] && winconfig $WIN_CONFIG set "$ORIG_HEIGHT"

	if [ -f $HOME/win/config.sys ];
	then
		# Test for Japanese Win98se installation
		grep jdisp $HOME/win/config.sys > $NULL 2> $NULL
		if [ $? -eq 0 ]
		then
			cat $HOME/win/config.sys | \
                	sed -e 's/\(.*biling\.sys\)/rem \1/' | \
                	sed -e 's/\(.*jfont\.sys\)/rem \1/' | \
                	sed -e 's/\(.*jdisp\.sys\)/rem \1/' | \
                	sed -e 's/\(.*jkeyb\.sys\)/rem \1/' | \
                	sed -e 's/\(.*kkcfunc\.sys\)/rem \1/' | \
                	sed -e 's/\(.*himem\.sys\)/rem \1/' | \
                	sed -e 's/\(device.*EMM386\.EXE RAM\)/rem \1/' \
                	>> $HOME/win/config.sys.jp

			mv config.sys .config.sys.orig
			mv config.sys.jp config.sys
		fi
	fi
	
	if [ "x$TRACE" = "xtrace" ]
	then 
		cp $PDIR/$WINDIR/user.dat $HOME/user.dat.orig
		cp $PDIR/$WINDIR/system.dat $HOME/system.dat.orig
	fi

	# Make sure in the personal drive directory.
	cd "$PDIR"

	if [ "$Use_Merge_Winsock" = "not" ]
	then
		echo "NETWORK_TYPE=vnet" >> .boot/info.txt
	elif [ "$Use_Merge_Winsock" = "ws1" ]
	then
		echo "NETWORK_TYPE=winsock" >> .boot/info.txt
	else
		echo "NETWORK_TYPE=winsock2" >> .boot/info.txt
	fi

	#
	# Check autoexec.bat and config.sys for drivers and tsr's that
	# non-english windows9x need. If these are present we need to
	# put an entry in system.ini to reserve space so Windows 9x
	# does not wipe these out.
	#
	grep -i keyboard.sys autoexec.bat > $NULL 2> $NULL
	if [ $? -eq 0 ]
	then
		WINDOWMEMSIZE=yes
	fi
	grep -i country.sys config.sys > $NULL 2> $NULL
	if [ $? -eq 0 ]
	then
		WINDOWMEMSIZE=yes
	fi
	grep -i display.sys config.sys > $NULL 2> $NULL
	if [ $? -eq 0 ]
	then
		WINDOWMEMSIZE=yes
	fi
		
	# Now make sure that autoexec.bat and config.sys have required lines!

	grep -i \^path= autoexec.bat > $NULL 2> $NULL
	if [ $? -ne 0 ]
	then
		# There is no PATH statement (or no autoexec.bat file)
		# So add the Windows path.
		DOSText "PATH=c:\\\\$WINDIR" >> autoexec.bat
	else
		# There is a PATH line.  See if it has the Windows directory.
		grep -i \^path= autoexec.bat | grep -i c:.$WINDIR \
			> $NULL 2> $NULL
		if [ $? -ne 0 ]
		then
			# Windows is not on the path so add it.
			# UNDER CONSTRUCTION
			TF=$$.aebat
			[ ! -w autoexec.bat ] && chmod +w autoexec.bat
			cp autoexec.bat $TF
			cat $TF \
			    | sed -e "s/^PATH=/PATH=c:\\\\$WINDIR;/g" \
			    > autoexec.bat
			rm -f $TF
		fi
	fi

	#
	# Check for TMP and TEMP settings in autoexec.bat.  If they are not
	# there, then add them and make a temp directory too if needed.
	# The Win9x bootstrap always makes sure that TMP and TEMP are set,
	# even if they are not in the autoexec.bat.  Having installwindows
	# set TMP and TEMP in autoexec.bat is our approximation.
	#
	NEED_WINTMP_DIR=FALSE
	grep -i '^[ @	]*set[ 	][ 	]*tmp=' autoexec.bat > $NULL 2> $NULL
	if [ $? -ne 0 ]
	then
		#
		# Add the TMP setting.  Note that an extra \ is needed 
		# for \temp because DOSText interprets \t as a tab
		#
		DOSText "@set TMP=c:\\\\$WINDIR\\\\temp" >> autoexec.bat
		NEED_WINTMP_DIR=TRUE
	fi
	grep -i '^[ @	]*set[ 	][ 	]*temp=' autoexec.bat > $NULL 2> $NULL
	if [ $? -ne 0 ]
	then
		DOSText "@set TEMP=c:\\\\$WINDIR\\\\temp" >> autoexec.bat
		NEED_WINTMP_DIR=TRUE
	fi
	if [ "$NEED_WINTMP_DIR" = TRUE ] && [ ! -d "$WINDIR/temp" ]
	then
		mkdir "$WINDIR/temp" > $NULL 2> $NULL
	fi

	[ ! -f config.sys ] && touch config.sys
	grep -i \^device config.sys | grep -i ifshlp.sys > $NULL 2> $NULL
	if [ $? -ne 0 ]
	then
		# Add ifshlp.sys driver.
		DOSText "devicehigh=c:\\\\$WINDIR\\\\ifshlp.sys" >> config.sys
	fi

	# Patch the kernel32.dll and krnl386.exe so merge will work.

	PATTARG=$WINDIR/system/kernel32.dll
	if [ "$WIN_VERSION" = "winME" ]
	then
		PATFILE=$STATICMERGE/patch/wMEkernel32.pat
	elif [ "$WIN_VERSION" = "win98" ]
	then
		if [ "$WIN98_VERSION" = "Original" ]
		then
			PATFILE=$STATICMERGE/patch/w98kernel32.pat
		else
			# For win98 2ndEdition
                        if [ "$WINDOWS_VERSION" = "98sezhcn" ]
                        then
                              PATFILE=$STATICMERGE/patch/w98SEzhkernel32.pat
                        elif [ "$WINDOWS_VERSION" = "98sezhtw" ]
			then
                              PATFILE=$STATICMERGE/patch/w98SEztkernel32.pat
                        elif [ "$WINDOWS_VERSION" = "98sejp" ]
			then
                              PATFILE=$STATICMERGE/patch/w98SEJkernel32.pat
                        elif [ "$WINDOWS_VERSION" = "98sekr" ]
			then
                              PATFILE=$STATICMERGE/patch/w98SEkrkernel32.pat
			else
                              PATFILE=$STATICMERGE/patch/w98SEkernel32.pat
                        fi
                fi
	elif [ "$WIN95_VERSION" = "OSR2" ]
	then
		PATFILE=$STATICMERGE/patch/w95osr2kernel32.pat
	else
		PATFILE=$STATICMERGE/patch/w95kernel32.pat
	fi
	cp $PATTARG $WINDIR/system/.kernel32.dll
	$STATICMERGE/patch/patchit -u -p $PATFILE $PATTARG
	if [ $? -ne 0 ]
	then
		Log "patchit -u -p $PATFILE $PATTARG"
		Echo "Error 26: Failed to patch \"$PATTARG\"!"
		Echo "Cannot continue.  Exiting."
		Exit 26
	fi

	# Handle the "upgrade from Win3x case" where phase1 of win95
	# setup leaves the krnl386.exe to be the win3x version and
	# installs the win95 version as krnl386.001
	if [ -f $WINDIR/system/krnl386.001 ]
	then
		PATTARG=$WINDIR/system/krnl386.001
	else
		PATTARG=$WINDIR/system/krnl386.exe
	fi
	if [ "$WIN_VERSION" = "winME" ]
	then
		PATFILE=$STATICMERGE/patch/wMEk386.pat
	elif [ "$WIN_VERSION" = "win98" ]
	then
		# Both Orignal and SE have the same krnl386.exe file.
                if [ "$WINDOWS_VERSION" = "98sezhcn" ]
                then
                     PATFILE=$STATICMERGE/patch/w98zhk386.pat
                elif [ "$WINDOWS_VERSION" = "98sezhtw" ]
                then
                     PATFILE=$STATICMERGE/patch/w98ztk386.pat
	        elif [ "$WINDOWS_VERSION" = "98sejp" ]
		then
                     PATFILE=$STATICMERGE/patch/w98jk386.pat
	        elif [ "$WINDOWS_VERSION" = "98sekr" ]
		then
                     PATFILE=$STATICMERGE/patch/w98krk386.pat
                else
                     PATFILE=$STATICMERGE/patch/w98k386.pat
                fi
	elif [ "$WIN95_VERSION" = "OSR2" ]
	then
		PATFILE=$STATICMERGE/patch/osr2k386.pat
	else
		PATFILE=$STATICMERGE/patch/krnl386.pat
	fi
	cp $PATTARG $WINDIR/system/.krnl386.dll
	$STATICMERGE/patch/patchit -u -p $PATFILE $PATTARG
	if [ $? -ne 0 ]
	then
		Log "patchit -u -p $PATFILE $PATTARG"
		Echo "Error 27: Failed to patch \"$PATTARG\"!"
		Echo "Cannot continue.  Exiting."
		Exit 27
	fi

	# Install the parts of the Merge block device driver that
	# have to be copied in.  The actual mrgblk.pdr is installed
	# by the Fix_links function (from fix_windows.sh).
	cp $STATICMERGE/inst95/mrgblk.inf $WINDIR/inf/mrgblk.inf
	chmod +r $WINDIR/inf/mrgblk.inf
	cp $STATICMERGE/inst95/mrgblk.reg $WINDIR
	chmod +r $WINDIR/mrgblk.reg
	if [ "$WIN_VERSION" = "winME" ]
	then
		cp $STATICMERGE/inst95/mrgmon98.reg $WINDIR
		chmod +r $WINDIR/mrgmon98.reg
	elif [ "$WIN_VERSION" = "win98" ]
	then
		cp $STATICMERGE/inst95/mrgmon98.reg $WINDIR
		chmod +r $WINDIR/mrgmon98.reg
	else
		cp $STATICMERGE/inst95/mrgmon95.reg $WINDIR
		chmod +r $WINDIR/mrgmon95.reg
	fi

	# The Fix_* requires that WIN_CONFIG be set to
	# the current configuration we are using, and C_DIR be set
	# to the full path Unix directory where the C drive is rooted.
	# They also call Log and use LOGFILE.
	# This function changes directories, so must cd back.
	# Dot in fix_windows.sh to give us the Fix_* functions
	. $STATICMERGE/bin/fix_windows.sh
	C_DIR="$PDIR"
	Fix_setup					>> "$LOGFILE" 2>&1
	if [ $? -ne 0 ]
	then
		# Unexpected error!
		Echo "Error 57: Internal error, mysterious failure."
		Echo "Cannot continue.  Exiting."
		Exit 57
	fi
	cd "$PDIR"

	# Fix_links() Installs the various drivers that we need by creating
	# symbolic links to a central copy.  This both saves disk space
	# but more importantly, if a new version of Merge or an update
	# of Merge changes our of our drivers, then all the installation
	# of Windows are instantly automatically upgraded.
	# This function changes directories, so must cd back.
	Fix_links					>> "$LOGFILE" 2>&1
	# Call Fix_files with debug output about patch state
	Fix_files -d 				        >> "$LOGFILE" 2>&1
	cd "$PDIR"

	# Add an entry in the setup load_inf section so windows automagicly
	# knows about this device in the control panel.
	SOURCE=$WINDIR/inf/setup.inf
	ed $SOURCE <<-MRGINSTINF >/dev/null 2>/dev/null
		g/monitor[2-9c].inf/d
		g/monitor1[1-9c].inf/d
		/scsi.inf
		i
		mrgblk.inf
		.
		w
		q
	MRGINSTINF
	
	SOURCE=$WINDIR/inf/setupc.inf
	ed $SOURCE <<-MRGINSTINF >/dev/null 2>/dev/null
		/scsi.inf
		i
		mrgblk.inf
		.
		w
		q
	MRGINSTINF

	SOURCE=$WINDIR/inf/setupc.inf
	if [ "$WIN_VERSION" = "winME" ]
	then
		ed $SOURCE <<-MRGINSTINF >/dev/null 2>/dev/null
			g/KEY_RUN.*Intro Video.*/d
			w
			q
		MRGINSTINF
	fi

	# Go add the MRGBLK entries to the registry
	Add_Mrgblk_To_Registry

	# Go add the Merge Monitor entries to the registry
	Add_MrgMonitor_To_Registry

	# Go add the Merge Sound entries to the registry
	Add_Sound_To_Registry

	# Go remove ie4 from registry so phase 2 will not install it
	Del_Ie4_From_Registry

	if [ "$Use_Merge_Winsock" = "not" ]
	then
		Echo "Preparing Networking"
		if [ "$WIN_VERSION" = "winME" ]
		then
		    # For Windows ME we need to add the NIC to the register
		    # since msbatch.inf fails to do it
		    Add_Nic_To_Registry
		fi
		# Go and put our special ne2000 driver in place.
		Fix_vnet_registry	>> "$LOGFILE" 2>&1
		cd "$PDIR"
	fi

	if [ "$WIN_VERSION" = "win98" -o "$WIN_VERSION" = "winME" ]
	then
		if [ "$ZOOM_OPTION" = "-z" -a \
		     "$WIN3x_TO_WIN95_UPGRADE" != "TRUE" ]
		then
			SYSDIR=$PDIR/$WINDIR/system
			mv $SYSDIR/vga.drv $SYSDIR/vga.drv.orig
			ln -s $STATICMERGE/windrv/f/vw.drv \
				$SYSDIR/vga.drv
			cat $STATICMERGE/windrv/f/vw.ver \
				>> $PDIR/$WINDIR/system.ini
		fi
	else
		if [ "$ZOOM_OPTION" = "-z" -a \
		     "$WIN3x_TO_WIN95_UPGRADE" != "TRUE" ]
		then
			Munge_Registry_for_VW_95
		fi
	fi
	# Put back the normal vmm32.vxd. 
	# The only use for the new vmm32.vxd is for IE4's use.
	if [ "x$ISOSR25" = "xyes" ]
	then
	    cd $PDIR
	    if [ ! -d $WINDIR/system/.hide ]
	    then
		    mkdir $WINDIR/system/.hide
	    else
		   rm -f $WINDIR/system/.hide/vmm32.vxd
	    fi
	    mv $WINDIR/system/vmm32.vxd $WINDIR/system/.hide
	    EXTR_BAT="$PDIR/iwin_ex.bat"
	    rm -f $EXTR_BAT
	    DOSText "c: " > $EXTR_BAT
	    DOSText "cd \\\\$WINDIR\\\\system "  >> $EXTR_BAT
	    if [ "z$SETUP_DIR" != "z" ]
	    then
		DOSText "$SOURCE_DRIVE_LETTER:\\\\$SETUP_DIR\\\\extract -A $SOURCE_DRIVE_LETTER:\\\\$SETUP_DIR\\\\win95_02.cab vmm32.vxd " \
		   | sed -e 's!^Z !!' | sed -e 's!/!\\!g' | sed -e 's! -! /!g' \
		   >> $EXTR_BAT
	    else
		DOSText "$SOURCE_DRIVE_LETTER:extract -A $SOURCE_DRIVE_LETTER:\\\\win95_02.cab vmm32.vxd " \
		   | sed -e 's!^Z !!' | sed -e 's!/!\\!g' | sed -e 's! -! /!g' \
		   >> $EXTR_BAT
	    fi


	    # Get the orinial vmm32.vxd which has the working vmm.vxd 
	    Dos_Cmd +k$WIN_CONFIG +b +r iwin_ex.bat >> $LOGFILE 2>&1
	    [ "x$TRACE" != "xtrace" ] && rm -f $EXTR_BAT
	fi

	#
	# Get rid of the "display=*vddvga" line in the [386enh] section
	# of system.ini since Windows doesn't install vddvga if it isn't
	# installing VGA support.  Also change the device description line
	# for the Win31 version of vw driver to say "Win31" so the user is
	# more aware of what is installed.
	# 
	if [ "$WIN_VERSION" = "win95" -a -f $WINDIR/system.ini ]
	then
		SOURCE=$WINDIR/system.ini
		ex $SOURCE <<-MRGINSTINF >/dev/null
		set ic
		g,DOS Merge Windows/X.*,s,,Merge Windows/X (Win 3.1 version),
		g/^[ 	]*display[ 	]*=.*\\vddvga/s/^/; Commented OUT by installwindows - /
		wq
		MRGINSTINF
		#
		# Setup so Windows does not steel all memory away from 
		# TSR's for display and keyboard of other char sets.
		# If this is not done Windows will exit just after
		# going into protected mode.
		#
		if [ "x$WINDOWMEMSIZE" = "xyes" ]
		then
			SOURCE=$WINDIR/system.ini
			ex $SOURCE <<-SYSTEMINI >/dev/null
			set ic
			/\[386Enh\]
			a
			WindowMemSize=600
			.
			wq
			SYSTEMINI
		fi
	fi

	# Hide logos.sys and logow.sys.
	# These are the graphics that are shown when windows is shutting down.
	# We might want to in the future replace these with our own files.

	HWIN=$PDIR/$WINDIR
	if [ ! -d $HWIN ]
	then
		Echo "Error 28: \"$HWIN\" does not exist!"
		Echo "Cannot continue.  Exiting."
		Exit 28
	fi
	cd $HWIN

	[ ! -d .hide ] && mkdir .hide
	if [ ! -d .hide ]
	then
		Echo "Error 29: $HWIN/.hide does not exist and cannot be created!"
		Echo "Cannot continue.  Exiting."
		Exit 29
	fi
	[ -f logos.sys ] && mv logos.sys .hide
	[ -f logow.sys ] && mv logow.sys .hide

	if [ "$WIN_VERSION" != "winME" ]
	then
		#
		# Change "welcome.exe" to our own version "wshutdn.exe" that
		# causes windows to shutdown after installation has completed.
		# This way we can do the final fixup of the registry before
		# the user has a chance to try installing other software
		# and messing things up.
		#
		Log Installing fake welcome.exe
		mv [Ww][Ee][Ll][Cc][Oo][Mm][Ee].[Ee][Xx][Ee] welcome.sav
		cp $WSHUTDN_PROG welcome.exe
	fi

	# Adjust the device info files ($WINDIR/inf/*.inf), so as to
	# reduce the "Setting Up Hardware" part of the installaton.
	INF=$PDIR/$WINDIR/inf
	if [ ! -d $INF ]
	then
		Echo "Error 30: \"$PDIR\" does not exist!"
		Echo "Cannot continue.  Exiting."
		Exit 30
	fi
	cd $INF

	[ ! -d .hide ] && mkdir .hide
	if [ ! -d .hide ]
	then
		Echo "Error 31: $INF/.hide does not exist and cannot be created!"
		Echo "Cannot continue.  Exiting."
		Exit 31
	fi

	# Hide certain INF files. ##########################################

	# 1: Hide INF files that are never needed under Merge.  These are:
	#	SCCS and drive adapter related.
	#	BUS related.
	#	MS-Mouse (but MSMOUSE is needed for Win98)
	mkdir .hide/notneeded

	mv adapter.inf cheyenne.inf .hide/notneeded 2> $NULL
	mv msfdc.inf    mshdc.inf   .hide/notneeded 2> $NULL
	mv mtd.inf pcmcia.inf		  	.hide/notneeded 2> $NULL
	mv scsi.inf                             .hide/notneeded 2> $NULL
	if [ "$WIN_VERSION" = "win95" ]
	then
		mv msmouse.inf			.hide/notneeded 2> $NULL
	fi

	# 2: Hide printer, modem, network and multimedia INF files
	#    during installation.  These are put back after installation.
	mkdir .hide/tmp
	mv msprint.inf  msprint2.inf  prtupd.inf     .hide/tmp 2> $NULL
	mv decpsmw4.inf hpnetprn.inf                 .hide/tmp 2> $NULL
	mv modems.inf mdm*.inf                       .hide/tmp 2> $NULL
	if [ "$Use_Merge_Winsock" = "ws1" ]
	then
		mv net*.inf ndscli.inf               .hide/tmp 2> $NULL
	fi
	mv midi.inf serwave.inf wave.inf tamwrap.inf .hide/tmp 2> $NULL

	# 3: Hide the display adapter and monitor INF files, and
	#    replace with hand crafted ones that only have VGA support.
	mkdir .hide/monitor
	if [ -f $STATICMERGE/inst95/monitor.inf ]
	then
		mv monitor.inf .hide/monitor
		cp $STATICMERGE/inst95/monitor.inf .
	fi
	mv monitor[0-9]*.inf .hide/monitor
	if [ "$WIN_VERSION" = "winME" ]
	then
		mv dx*.inf .hide/monitor
		if [ -f $STATICMERGE/inst95/milldisp.inf ]
		then
			mv milldisp.inf .hide/monitor
			cp $STATICMERGE/inst95/milldisp.inf milldisp.inf

			# For WinME this is a bit of a hack but msdet.inf
			# wants an msdisp.inf file around too, so we just
			# make a copy of our milldisp.inf.  With this,
			# ME setup installs the vw95 driver just fine.
			if [ -f msdisp.inf ]
			then
				mv msdisp.inf .hide/monitor
			fi
			ln milldisp.inf msdisp.inf
		fi
	elif [ "$WIN_VERSION" = "win98" ]
	then
		mv dx*.inf .hide/monitor
		if [ -f $STATICMERGE/inst95/msdisp98.inf ]
		then
			mv msdisp.inf .hide/monitor
			cp $STATICMERGE/inst95/msdisp98.inf msdisp.inf
		fi
	elif [ -f $STATICMERGE/inst95/msdisp.inf ]
	then
		mv msdisp.inf .hide/monitor
		cp $STATICMERGE/inst95/msdisp.inf .
	fi

	Set_Default_Window_Size msdisp.inf

	# 4: Put in special printer INF file for use during installation.
	if [ "$PRINTER_HACK" = "yes" ]
	then
		# The "hack" is to provide a very minimal msprint.inf
		# file for use during installation, and to put back the
		# real one after installation is over.
		if [ -f $STATICMERGE/inst95/msprint.inf ]
		then
			cp $STATICMERGE/inst95/msprint.inf .
		else
			PRINTER_HACK=no
		fi
	fi

	# 5: Unnecessary Windows ME INF files (USB, IR, etc)
	if [ "$WIN_VERSION" = "winME" ]
	then
		mkdir .hide/winME
		mv ati* .hide/winME 2>/dev/null
		mv ir* .hide/winME 2>/dev/null
		mv usb* .hide/winME 2>/dev/null
		mv wdma_* .hide/winME 2>/dev/null
	fi


	# 6: Edit certain files.
	# UNDER CONSTRUCTION - - - - - - - - - - - - - - - - - - - - - - - -
	# - - -
	# Might want to edit setup.inf, setupc.inf, or machine.inf here.
	# - - -

	# 7: Set the session to be Windows 9x, win95.
	#    CFGLIST is setup in the Fixup_Configs step by calling Get_Configs
	#
	for NAME in $CFGLIST
	do
		SESS=`winconfig $NAME list session | cut -d= -f2`
		MSG="in configuration $NAME"
		if [ "x$SESS" != "xwin95" ]
		then
			winconfig $NAME set session=win95
		fi
		# Force drive D to point to the mydata directory
		# if it exists, otherwise make it unattached.
		#
		if [ -d "$USEHOME"/mydata ]
		then
			Log "Attach drive D to $USEHOME/mydata $MSG"
			winconfig $NAME set drive=d,unix,HOME/mydata/,map_win95 
		else
			Log "Unattach drive D $MSG"
			winconfig $NAME set drive=d,none
		fi
	done

	# Done with the Magic!
}

Phase_2()
{
	Log Phase_2 start
	Log `date`

	# Now fire up Windows, so it can complete the installation.
	Echo Restarting Windows to complete installation.

	if [ "x$TRACE" = "xtrace" -a "$DO_SILENT" = "notsilent" ]
	then
		Echo 
		Echo Press return to continue.
		Echo 
		read foo
	fi

	# Turn on autozoom so if the startzoomed option (+z) is not set
	# we will zoom when needed.
	AUTOZOOM_SAVE=`winconfig $WIN_CONFIG list autozoom | cut -d= -f2`
	winconfig $WIN_CONFIG set autozoom=true

	Echo "Starting Windows session for second phase of installation."
	if [ "$WIN_VERSION" = "win98" -o "$WIN_VERSION" = "winME" ]
	then
		# Win98 reboots an extra time after it "detects" the
		# display type and networking
		Log "Phase 2, extra first session for win98"
		Win_Cmd $ARG_X +k$WIN_CONFIG -b $ZOOM_OPTION $AFLOP $BFLOP +r
		# Some versions of Win98 do one more reboot, some do two more.
		# TBD : For now re-enable the automatic restart feature to deal
		#       with this situation.
		# TBD   Still need to investigate if we can call "Clean_Up" here
		#	(or do some it here instead of in "Clean_Up").

		# Slurp doesn't need to restart again.
		if [ "x$DOING_SLURP" = "x" ]
		then
			MERGE_DISABLE_RESTART=off
		else
			MERGE_DISABLE_RESTART=on
		fi
	fi

	# If we are dealing with windows 98 or winME then we need to patch
	# msafd.dll. This file has address information in it that
	# bases a check of valid sockets for tcp/udp winsock oper-
	# ations on a windows kernel address. Without this patch,
	# ftp, telnet... get error code 10038.
	MSATARG=$PDIR/$WINDIR/system/msafd.dll
	if [ -f "$MSATARG" ]
	then
		if [ "$WIN_VERSION" = "winME" ]
		then
			MPATFILE=$STATICMERGE/patch/wMEmsafd.pat
		else
			MPATFILE=$STATICMERGE/patch/w98msafd.pat
		fi
		Log "patchit -i -u -p $MPATFILE $MSATARG"
		Patchit_tome -i -u -p $MPATFILE $MSATARG
	fi
	if [ "$WIN_VERSION" = "winME" ]
	then
		# For WinME add wshutdn and remove the intro video
		cp $WSHUTDN_PROG $PDIR/$WINDIR/wshutdn.exe
		Add_Wshutdn_To_Registry
		Remove_IntroVideo_From_Registry

		# We have to do this again for winME
		Set_Default_Window_Size
	fi
	if [ "$WIN_VERSION" = "win98" -o "$WIN_VERSION" = "winME" ]
	then
		Echo "Starting Windows session for third phase of installation."
	fi
	Win_Cmd $ARG_X +k$WIN_CONFIG -b $ZOOM_OPTION $AFLOP $BFLOP 

	winconfig $WIN_CONFIG set autozoom=$AUTOZOOM_SAVE

	# Now check to see if Windows really got installed.
	Check_Win9x
	if [ $? -ne 1 ]
	then
		Log "Windows did not installed successfully."
		Log "Restore configurations to win3x session type."

		# Windows didn't installed successfully. 
		# Restore the changes made to the configurations in the
		# Magic_Step, namely set the session type back to win3x.
		for NAME in $CFGLIST
		do
			winconfig $NAME set session=win3x
		done
	fi

	Log `date`
	Log Phase_2 done
}


Clean_Up()
{
	Log Clean_Up

	# Move back INF files we hid during installation.
	if [ -d $INF ]
	then
		cd $INF
		[ "$PRINTER_HACK" = "yes" ] && rm -f msprint.inf
		mv .hide/tmp/*.inf .
		rmdir $INF/.hide/tmp > $NULL 2> $NULL
	fi

	cd $PDIR

	if [ "x$TRACE" = "xtrace" ]
	then
		# Don't move msbatch.inf if doing a trace, just copy it.
		cp msbatch.inf $DOTMERGE
	else
		# Move the msbatchfile to the $HOME/.merge directory
		# so if there are problems it will be saved there
		# next to the log file.
		mv msbatch.inf $DOTMERGE
	fi

	# Remove the autoexec.dos and config.dos files that setup created.
	# (They are not used for any purpose.)
	rm -f autoexec.dos config.dos
}

Touch_Up()
{

	# Put in the "workaround" for the problem of the FONT directory
	# not permanently having the "system" attribute.
	ADD_IT=yes
	if [ -f autoexec.bat ]
	then
		BS="\\\\"
		grep -i 'attrib' autoexec.bat | grep -i fonts > $NULL 2> $NULL
		if [ $? -eq 0 ]
		then
			ADD_IT=no
		fi
	fi
	if [ "$ADD_IT" = "yes" ]
	then
		# Add the line to autoexec.bat
		DOSText "@attrib +s c:${BS}$WINDIR${BS}fonts"  \
			>> autoexec.bat
	fi

	Log Restoring original welcome.exe
	HWIN=$PDIR/$WINDIR
	if [ -f $HWIN/welcome.sav ]
	then
		rm -f $HWIN/welcome.exe
		mv $HWIN/welcome.sav $HWIN/welcome.exe
		Add_Welcome_To_Registry
	fi
}

# Change the desttop "My Documents" directory to point to D:/MyDocuments.
# (The D drive is targeted to $USEHOME/mydata)
# 
Change_MyDoc1()
{
	Log "Change_MyDoc1"
	if [ ! -d "$USEHOME"/mydata/MyDocuments ]
	then
		return
	fi
	Log "Change to $PDIR"
	cd "$PDIR"
	BAT1_FILE=iwin_md1.bat
	BAT2_FILE=iwin_md2.bat
	FULL_BAT1="$PDIR"/$BAT1_FILE
	FULL_BAT2="$PDIR"/$BAT2_FILE
	GET_FILE=iwin_md1.reg
	PUT_FILE=iwin_md2.reg
	FULL_GET="$PDIR"/$GET_FILE
	FULL_PUT="$PDIR"/$PUT_FILE

	rm -f "$FULL_BAT1" "$FULL_BAT2"
	rm -f "$FULL_GET"  "$FULL_PUT"

	Log "Update - User Shell Folders"
	DOSText "c: " > "$BAT1_FILE"
	DOSText "cd \\ "  >> "$BAT1_FILE"
	DOSText "regedit /E $GET_FILE \
\"HKEY_USERS+.DEFAULT+Software+Microsoft+Windows+CurrentVersion+Explorer+User Shell Folders\"" \
		| sed -e 's%+%\\%g' \
		>> "$BAT1_FILE"
	Dos_Cmd +k$WIN_CONFIG +b +r $BAT1_FILE >> "$LOGFILE" 2>&1
	RET_VAL=$?
	# Make sure the batch file ran and produced a file.
	if [ ! -f "$FULL_GET" ]
	then
		Log "Failed to extract the info"
		if [ "x$TRACE" != "xtrace" ]
		then
			rm -f "$FULL_BAT1" "$FULL_BAT2"
			rm -f "$FULL_GET"  "$FULL_PUT"
		fi
		return
	fi
	cat "$FULL_GET" \
		| sed -e 's%C:\\\\My Documents%D:\\\\MyDocuments%' \
		| $STATICMERGE/charconv -p -x > "$FULL_PUT"
	cmp -s "$FULL_GET" "$FULL_PUT"
	if [ $? -ne 0 ]
	then
		DOSText "c: " > "$FULL_BAT2"
		DOSText "cd \\ "  >> "$FULL_BAT2"
		DOSText "regedit $PUT_FILE" >> "$FULL_BAT2"
		Dos_Cmd +k$WIN_CONFIG +b +r $BAT2_FILE >> $LOGFILE 2>&1
	else
		Log "No change to registry"
	fi
	if [ "x$TRACE" != "xtrace" ]
	then
		rm -f "$FULL_BAT1" "$FULL_BAT2"
		rm -f "$FULL_GET"  "$FULL_PUT"
	fi
}
	
Change_MyDoc2()
{
	Log "Change_MyDoc2"
	if [ ! -d "$USEHOME"/mydata/MyDocuments ]
	then
		return
	fi
	Log "Change to $PDIR"
	cd "$PDIR"
	BAT1_FILE=iwin_md3.bat
	BAT2_FILE=iwin_md4.bat
	FULL_BAT1="$PDIR"/$BAT1_FILE
	FULL_BAT2="$PDIR"/$BAT2_FILE
	GET_FILE=iwin_md3.reg
	PUT_FILE=iwin_md4.reg
	FULL_GET="$PDIR"/$GET_FILE
	FULL_PUT="$PDIR"/$PUT_FILE

	rm -f "$FULL_BAT1" "$FULL_BAT2"
	rm -f "$FULL_GET"  "$FULL_PUT"

	Log "Update - Shell Folders"
	DOSText "c: " > "$BAT1_FILE"
	DOSText "cd \\ "  >> "$BAT1_FILE"
	DOSText "regedit /E $GET_FILE \
\"HKEY_USERS+.DEFAULT+Software+Microsoft+Windows+CurrentVersion+Explorer+Shell Folders\"" \
		| sed -e 's%+%\\%g' \
		>> "$BAT1_FILE"
	Dos_Cmd +k$WIN_CONFIG +b +r $BAT1_FILE >> "$LOGFILE" 2>&1
	RET_VAL=$?
	# Make sure the batch file ran and produced a file.
	if [ ! -f "$FULL_GET" ]
	then
		Log "Failed to extract the info"
		if [ "x$TRACE" != "xtrace" ]
		then
			rm -f "$FULL_BAT1" "$FULL_BAT2"
			rm -f "$FULL_GET"  "$FULL_PUT"
		fi
		return
	fi
	cat "$FULL_GET" \
		| sed -e 's%C:\\\\My Documents%D:\\\\MyDocuments%' \
		| $STATICMERGE/charconv -p -x > "$FULL_PUT"

	cmp -s "$FULL_GET" "$FULL_PUT"
	if [ $? -ne 0 ]
	then
		DOSText "c: " > "$FULL_BAT2"
		DOSText "cd \\ "  >> "$FULL_BAT2"
		DOSText "regedit $PUT_FILE" >> "$FULL_BAT2"
		Dos_Cmd +k$WIN_CONFIG +b +r $BAT2_FILE >> $LOGFILE 2>&1
	else
		Log "No change to registry"
	fi
	if [ "x$TRACE" != "xtrace" ]
	then
		rm -f "$FULL_BAT1" "$FULL_BAT2"
		rm -f "$FULL_GET"  "$FULL_PUT"
	fi
}

Slurp_NativePartition()
{
	if [ "$SYSTEMTYPE" != "linux" ]
	then
		Echo "slurp is only supported on linux."
		Echo "Cannot continue, Exiting."
		Exit 52
	fi
	# This script copies ALL the files from the real DOS C
	# drive into the indicated directory, and sets up the
	# the special filenamemapping subdirs, but otherwise
	# does not make changes.
	"$STATICMERGE"/bin/slurp.sh "$PDIR"
	RETCODE=$?
	if [ $RETCODE -ne 0 -o ! -f $PDIR/command.com ]
	then
		echo "Failed to get Windows files from the"
		echo "DOS drive C volume." 
		Exit 52
	fi
}

Get_Native_Win_Version()
{
        # We look fot text in the "slurped" command.com
        # 4.00.1111 is OSR2
        # 4.10.1998 is Win98 original
        # 4.10.2222 is Win98 second edition

        strings $PDIR/command.com | grep '4.00.'
        if [ $? -ne 0 ]
        then
                WIN_VERSION=win98
                WIN95_VERSION=not95
                strings $PDIR/command.com | grep '4.10.1998'
                if [ $? -ne 0 ]
                then
                        WIN98_VERSION=2ndEdition
                        WINDOWS_VERSION=98se
                else
                        WIN98_VERSION=Original
                        WINDOWS_VERSION=98orig
                fi                                                                      else
                WIN_VERSION=win95
                WIN_98VERSION=not98
                strings $PDIR/command.com | grep '4.00.1111'
                if [ $? -ne 0 ]
                then
                        WIN95_VERSION=OSR1
                        WINDOWS_VERSION=95classic
                else
                        WIN95_VERSION=OSR2
                        WINDOWS_VERSION=95osr2
                fi
        fi
}
 
xxx_Make_Native_Boot_Floppy()
{
        # If we're slurping, we boot off a virtual floppy that
        # we copy the "slurped" command.com and io.sys onto.
	Log Make_Native_Boot_Floppy

        # First make the floppy
        NAT_VFLOPPY=$PDIR/.boot/boot.dsk
        cp $STATICMERGE/bs/dos7.dbr $NAT_VFLOPPY
        chmod 644 $NAT_VFLOPPY
        dd if=$STATICMERGE/diskimages/f.dsk of=$NAT_VFLOPPY seek=1 skip=1 bs=512

        # Copy the necesary windows files onto it.
        DOPT="-s"
        cd $PDIR
        echo copyto command.com | $STATICMERGE/dosdisk $DOPT $NAT_VFLOPPY
        echo copyto io.sys | $STATICMERGE/dosdisk $DOPT $NAT_VFLOPPY
        echo copyto msdos.sys | $STATICMERGE/dosdisk $DOPT $NAT_VFLOPPY
        echo copyto autoexec.bat | $STATICMERGE/dosdisk $DOPT $NAT_VFLOPPY
        export DOS_BOOTVDISK=$NAT_VFLOPPY
	Log About to boot from the private drive boot floppy.
        "$BOOT_SH"
	Log Finished Make_Native_Boot_Floppy.
}   
 
Fix_NativePartition()
{
	#
	# This function modifies the nativewindows registry with the
	# necessary merge entries
	#
	Log Fix_NativePartition.

        Get_Native_Win_Version
        # Make_Native_Boot_Floppy  
        NATIVEREG=$PDIR/regget
        NEWREG=$PDIR/script.reg
        cd $PDIR
	chmod +w windows/*.dat
        BAT1_FILE="$PDIR/iwin_mb1.bat"
        BAT2_FILE="$PDIR/iwin_mb2.bat"
        rm -f $BAT1_FILE
        DOSText "PATH=c:\windows" > $BAT1_FILE
        DOSText "c: " >> $BAT1_FILE
        DOSText "cd \\ "  >> $BAT1_FILE
        DOSText "regedit /E c:\\\\regget" >> $BAT1_FILE

        # Export the registry contents from the .dat files
        Dos_Cmd +k$WIN_CONFIG +b +r iwin_mb1.bat >> $LOGFILE 2>&1

        # Remove all the native windows hardware entries from the registry
        cat $NATIVEREG | sed -e "s/HKEY_LOCAL_MACHINE[\\]Enum.*[\]/JUNK/g" \
        | sed -e "s/HKEY_LOCAL_MACHINE[\\]Hardware.*[\]/JUNK/g" \
        | sed -e "s/HKEY_LOCAL_MACHINE[\\]Network.*[\]/JUNK/g"  \
        | sed -e "s/HKEY_LOCAL_MACHINE[\\]System[\\]CurrentControlSet.* [\]/JUNK/g" > TMP_$$

        if [ $WIN_VERSION = win98 ]
        then
                if [ $WIN98_VERSION = Original ]

                then
                        VV=98
                else
                        VV=98SE
                fi

                # Remove the SetupX stuff from the Windows98 versions.
                cat TMP_$$ | sed -e "s/HKEY_LOCAL_MACHINE[\\]Sofware[\\]Microsoft[\\]Windows[\\]CurrentVersion[\\]Setup[\\]SetupX.*[\]/JUNK/g" > TMP2_$$
                cp TMP2_$$ $NEWREG
                SETUPX=setupx$VV.reg
        else
                VV=95
                cp TMP_$$ $NEWREG
                SETUPX=
        fi
        rm -f TMP_$$ TMP2_$$

        # Add merge hardware entries to the registry
        for II in hardware$VV.reg enum$VV.reg currentcontrolset$VV.reg $SETUPX
        do
                cat $STATICMERGE/inst95/$II >> $NEWREG
                if [ "x$TRACE" = "xtrace" ]
                then
                        echo "adding $II to the registry"
                fi
        done

        # Import our new "mergified" registry.
        cp $NEWREG $NATIVEREG
        rm -f $BAT2_FILE
        DOSText "PATH=c:\windows" > $BAT2_FILE
        DOSText "c: " >> $BAT2_FILE
        DOSText "cd \\ "  >> $BAT2_FILE
        DOSText "regedit /C c:\\\\regget " >> $BAT2_FILE
        Dos_Cmd +k$WIN_CONFIG +b +r iwin_mb2.bat >> \
                         $LOGFILE 2>&1

	if [ "x$TRACE" = "x" ]
	then
		mv $BAT1_FILE $BAT2_FILE $DOTMERGE
	fi
	Log Fix_NativePartition.

}         

Check_winconfig()
{
	SESS=`winconfig $WIN_CONFIG list session | cut -d= -f2`
	if [ "x$SESS" != "xwin95" -a "x$SESS" != "xwin3x" ]
	then
		# Error, not a windows type configuration.
		return 1
	fi
	return 0
}

# Verify that the 1st parameter is a valid IP address.
# Return 0 when it is ok.
# Set the global VERIFIED to the valid IP address form.
# When not ok, return 1 and set the global to empty.
Verify_ip_addr()
{ 
	VERIFIED=
	# First split it up into four numbers, using a dot as the delimiter.
	INPUT="${1}.EndMarker"
	if [ "$INPUT" = ".EndMarker" ]
	then
		# It is ok for the input to be blank.
		# This just means the value is being defered.
		return 0
	fi
	N1=`echo $INPUT | cut -d. -f1`
	N1=`expr $N1 + 0 2> /dev/null`
	[ -z "$N1" ] && return 1

	N2=`echo $INPUT | cut -d. -f2`
	N2=`expr $N2 + 0 2> /dev/null`
	[ -z "$N2" ] && return 1

	N3=`echo $INPUT | cut -d. -f3`
	N3=`expr $N3 + 0 2> /dev/null`
	[ -z "$N3" ] && return 1

	N4=`echo $INPUT | cut -d. -f4`
	N4=`expr $N4 + 0 2> /dev/null`
	[ -z "$N4" ] && return 1

	M4=`echo $INPUT | cut -d. -f5`
	[ "x$M4" != "xEndMarker" ] && return 1

	# Each of these must be a number between 0 and 255.
	for i in $N1  $N2 $N3 $N4
	do
		[ $i -ge 0 -a $i -le 255 ] && continue
		return 1
	done
	VERIFIED="$N1.$N2.$N3.$N4"
	return 0
}

Get_vnet_defaults()
{
	# Get some standard system info.
	if [ "$SYSTEMTYPE" = "linux" ]
	then
		UNAME_N=`hostname -s`
	else
		UNAME_N=`uname -n`
	fi

	# note: LOGNAME holds the user's login name.

	DEF_VNET_HOSTNAME="$UNAME_N-$LOGNAME"
	DEF_VNET_COMPUTERNAME="$DEF_VNET_HOSTNAME"
	DEF_VNET_WORKGROUP="WORKGROUP"
	DEF_VNET_DESCRIPTION="Windows session of $LOGNAME on $UNAME_N"
	DEF_VNET_LOGONDOMAIN="WORKGROUP"
	DEF_VNET_DHCP=1
	# Default to empty IPADDRESS
	DEF_VNET_IPADDRESS=
	DEF_VNET_IPMASK=255.255.255.0
	if [ "$SYSTEMTYPE" = "sco32" ]
	then
		# Add code here to run our program that prints
		# out the network stuff so we can use the right settings.
		# Until then, detect some standard masks,
		# so it is a good chance that the "default" will be right.
		for i in net0 net1 net2 net3 net4 net5 net6 net7
		do
			[ ! -c /dev/$i ] && continue
			NM=`/etc/ifconfig -p $i 2>/dev/null | grep netmask`
			case "$NM" in
			*netmask?ffffff00*)
				# Class C
				DEF_VNET_IPMASK=255.255.255.0
				;;
			*netmask?fffff800*)
				# special
				DEF_VNET_IPMASK=255.255.248.0
				;;
			*netmask?ffff0000*)
				# Class B
				DEF_VNET_IPMASK=255.255.0.0
				;;
			esac
		done
	elif [ "$SYSTEMTYPE" = "uw7" ]
	then
		# Add code here to run our program that prints
		# out the network stuff so we can use the right settings.
		# Until then, detect some standard masks,
		# so it is a good chance that the "default" will be right.
		for i in net0 net1 net2 net3 net4 net5 net6 net7
		do
			[ ! -c /dev/$i ] && continue
			NM=`/usr/sbin/ifconfig -p $i 2>/dev/null | grep netmask`
			case "$NM" in
			*netmask?ffffff00*)
				# Class C
				DEF_VNET_IPMASK=255.255.255.0
				;;
			*netmask?fffff800*)
				# special
				DEF_VNET_IPMASK=255.255.248.0
				;;
			*netmask?ffff0000*)
				# Class B
				DEF_VNET_IPMASK=255.255.0.0
				;;
			esac
		done
	elif [ "$SYSTEMTYPE" = "linux" ]
	then
		# Don't use /proc/sys/net since the GRKERNSEC patch does
		# not permit mortal users to do that.
		eval `grep "^MERGE_VNET_IFNAME=" $DEFAULTMERGE`
		if [ "$MERGE_VNET_IFNAME" = auto ]
		then
			NETIFS="`LANG=en_US /sbin/ifconfig -a 2>/dev/null |
			    grep 'eth.*encap:Ethernet' | sed -e 's/[ 	].*//'`"
		else
			NETIFS="$MERGE_VNET_IFNAME"
		fi
		for i in $NETIFS
		do
			INET_LINE="`LANG=en_US /sbin/ifconfig $i 2>/dev/null |
				grep 'inet addr'`"
			if [ "$INET_LINE" != "" ]
			then
				DEF_VNET_IPMASK=`echo $INET_LINE | sed -e 's/^.*Mask:\(.*\)$/\1/'`
				break;
			fi
		done
	fi
	DEF_VNET_DOMAIN_N=
	if [ "$SYSTEMTYPE" = "linux" -a -x /bin/dnsdomainname ]
	then
		DEF_VNET_DOMAIN_N=`dnsdomainname`
	else
		if [ -f /etc/resolv.conf ]
		then
			DEF_VNET_DOMAIN_N=`grep "^domain" /etc/resolv.conf | \
				while read A B ; do echo $B ; break; done`
		fi
	fi
	DEF_VNET_DNSSERVERS=
	if [ -f /etc/resolv.conf ]
	then
		DEF_VNET_DNSSERVERS=`grep "^nameserver" /etc/resolv.conf \
			| while read A B; do echo $B ; break; done \
			| sed -e "s/ /,/g" `
	fi
	DEF_VNET_DOMAINORDER=DEF_VNET_DOMAIN_N
	if [ -f /etc/resolv.conf ]
	then
		DEF_VNET_DOMAINORDER=`grep "^search" /etc/resolv.conf \
			| while read A B; do echo $B ; break; done \
			| sed -e "s/ /,/g" `
	fi
	DEF_VNET_GATEWAYS=
	if [ "$SYSTEMTYPE" = "sco32" -o "$SYSTEMTYPE" = "uw7" ]
	then
		DEF_VNET_GATEWAYS=`netstat -nr | grep default \
			| while read A B C; do echo $B ; break; done`
	elif [ "$SYSTEMTYPE" = "linux" ]
	then
		DEF_VNET_GATEWAYS=`netstat -nr | grep " UG " \
			| while read A B C; do echo $B ; break; done`
	fi
}

Set_vnet_defaults()
{
	VNET_COMPUTERNAME="$DEF_VNET_COMPUTERNAME"
	VNET_WORKGROUP="$DEF_VNET_WORKGROUP"
	VNET_DESCRIPTION="$DEF_VNET_DESCRIPTION"
	VNET_LOGONDOMAIN="$DEF_VNET_LOGONDOMAIN"
	VNET_DHCP="$DEF_VNET_DHCP"
	VNET_IPADDRESS="$DEF_VNET_IPADDRESS"
	VNET_IPMASK="$DEF_VNET_IPMASK"
	VNET_HOSTNAME="$DEF_VNET_HOSTNAME"
	VNET_DOMAIN_N="$DEF_VNET_DOMAIN_N"
	VNET_DNSSERVERS="$DEF_VNET_DNSSERVERS"
	VNET_DOMAINORDER="$DEF_VNET_DOMAINORDER"
	VNET_GATEWAYS="$DEF_VNET_GATEWAYS"
}

Set_winsock2_defaults()
{
	# Get some standard system info.
	if [ "$SYSTEMTYPE" = "linux" ]
	then
		UNAME_N=`hostname -s`
	else
		UNAME_N=`uname -n`
	fi

	# note: LOGNAME holds the user's login name.
	WS2_COMPUTERNAME="$UNAME_N-$LOGNAME"
	WS2_WORKGROUP="WORKGROUP"
	WS2_DESCRIPTION="Windows session of $LOGNAME on $UNAME_N"
	WSHUTDN_PROG=$STATICMERGE/mwin95/ws2shutdn.exe
}

#SRI
SRI_report_vnet_defaults()
{
	SRI_output "VNET_COMPUTERNAME=$DEF_VNET_COMPUTERNAME"
	SRI_output "VNET_WORKGROUP=$DEF_VNET_WORKGROUP"
	SRI_output "VNET_DESCRIPTION=$DEF_VNET_DESCRIPTION"
	SRI_output "VNET_LOGONDOMAIN=$DEF_VNET_LOGONDOMAIN"
	SRI_output "VNET_DHCP=$DEF_VNET_DHCP"
	SRI_output "VNET_IPADDRESS=$DEF_VNET_IPADDRESS"
	SRI_output "VNET_IPMASK=$DEF_VNET_IPMASK"
	SRI_output "VNET_HOSTNAME=$DEF_VNET_HOSTNAME"
	SRI_output "VNET_DOMAIN_N=$DEF_VNET_DOMAIN_N"
	SRI_output "VNET_DNSSERVERS=$DEF_VNET_DNSSERVERS"
	SRI_output "VNET_DOMAINORDER=$DEF_VNET_DOMAINORDER"
	SRI_output "VNET_GATEWAYS=$DEF_VNET_GATEWAYS"
}
Log_vnet_settings()
{
	Log
	if [ $VNET_DHCP -eq 0 ]
	then
		Log "   IP Address : $VNET_IPADDRESS"
		Log "   IP Mask    : $VNET_IPMASK"
		Log "   Internet name : $VNET_HOSTNAME"
		Log "   DNS domain  : $VNET_DOMAIN_N"
		Log "   DNS servers : $VNET_DNSSERVERS"
		Log "   DNS domain search order : $VNET_DOMAINORDER"
		Log "   Gateway address : $VNET_GATEWAYS"
	else
		Log "   IP Address : assigned automatically"
	fi
	Log "Network Neighborhood settings"
	Log "   Workgroup : $VNET_WORKGROUP"
	Log "   Name : $VNET_COMPUTERNAME"
	Log "   Description: $VNET_DESCRIPTION"
	Log "   Logon domain : $VNET_LOGONDOMAIN"
}
#ENDSRI

# Ask the user the IP network info neede for VNET networking.
Query_vnet_info()
{
	# Set these variables for the network settings needed in msbatch.
	# VNET_COMPUTERNAME	ComputerName
	# VNET_WORKGROUP	Workgroup	(default=WORKGROUP)
	# VNET_DESCRIPTION	Description
	# VNET_LOGONDOMAIN	LogonDomain	(default=WORKGROUP)
	# VNET_DHCP		DHCP
	# VNET_IPADDRESS	IPAddress
	# VNET_IPMASK		IPMASK
	# VNET_HOSTNAME		Hostname
	# VNET_DOMAIN_N		Domain		DNS domain (e.g. bar.com)
	# VNET_DNSSERVERS	DNSServers
	# VNET_DOMAINORDER	DomainOrder  e.g. foo.bar.com,baz.x.com
	# VNET_GATEWAYS		Gateways 

	# First set the defaults:
	Get_vnet_defaults
	Set_vnet_defaults

	# The above sets the defaults as best we know how.
	# Now ask the user if changes are needed.
	while true
	do
		Text ""
		Text "========================================================"
		Text ""
		Text "Please enter the network settings to be used for this"
		Text "installation of Windows."
		Text "To use the setting in the brackets just press return."
		Text "If you you are not sure what the setting should be then"
		Text "use the default or leave it blank. You can change any"
		Text "of these settings later after Windows is installed."
		Text ""
	
		while true
		do
			if [ $VNET_DHCP -eq 1 ]
			then
				YORN=y
			else
				YORN=n
			fi

			Text "Use DHCP to assign an IP address automatically? [$YORN] \c"
			read ANS XXX
			[ "x$ANS" = "x" ] && ANS="$YORN"
			case $ANS in
			y*|Y*)
				VNET_DHCP=1
				break;
				;;
			n*|N*)
				VNET_DHCP=0
				break
				;;
			*)
				Text Please answer y for yes, and n for no.
				;;
			esac
		done

		if [ $VNET_DHCP -eq 0 ]
		then
			while true
			do
				Text "IP Mask? [$VNET_IPMASK] \c"
				read ANS XXX
				[ "x$ANS" != "x" ] && VNET_IPMASK="$ANS"
				Verify_ip_addr $VNET_IPMASK
				[ $? -eq 0 ] && break
				Echo "That is not a valid IP address type."
				VNET_IPMASK="$DEF_VNET_IPMASK"
			done

			while true
			do
				Text "IP Address? [$VNET_IPADDRESS] \c"
				read ANS XXX
				[ "x$ANS" != "x" ] && VNET_IPADDRESS="$ANS"
				Verify_ip_addr $VNET_IPADDRESS
				[ $? -eq 0 ] && break
				Echo "That is not a valid IP address type."
				VNET_IPADDRESS="$DEF_VNET_IPADDRESS"
			done

			Text "Internet name? [$VNET_HOSTNAME] \c"
			read ANS XXX
			[ "x$ANS" != "x" ] && VNET_HOSTNAME="$ANS"
	
			Text "DNS domain? [$VNET_DOMAIN_N] \c"
			read ANS XXX
			[ "x$ANS" != "x" ] && VNET_DOMAIN_N="$ANS"

			while true
			do
				Text "DNS servers? [$VNET_DNSSERVERS] \c"
				read ANS XXX
				[ "x$ANS" != "x" ] && VNET_DNSSERVERS="$ANS"
				Verify_ip_addr $VNET_DNSSERVERS
				[ $? -eq 0 ] && break
				Echo "That is not a valid IP address type."
				VNET_DNSSERVERS="$DEF_VNET_DNSSERVERS"
			done

			Text "DNS domain search order? [$VNET_DOMAINORDER] \c"
			read ANS XXX
			[ "x$ANS" != "x" ] && VNET_DOMAINORDER="$ANS"
	
			while true
			do
				Text "Gateway address? [$VNET_GATEWAYS] \c"
				read ANS XXX
				[ "x$ANS" != "x" ] && VNET_GATEWAYS="$ANS"
				Verify_ip_addr $VNET_GATEWAYS
				[ $? -eq 0 ] && break
				Echo "That is not a valid IP address type."
				VNET_GATEWAYS="$DEF_VNET_GATEWAYS"
			done
		fi

		Text "Network Neighborhood workgroup? [$VNET_WORKGROUP] \c"
		read ANS XXX
		[ "x$ANS" != "x" ] && VNET_WORKGROUP="$ANS"

		if [ "x$VNET_COMPUTERNAME" = "x" ]
		then
			# When not yet specified, use VNET_HOSTNAME
			# for the default.
			VNET_COMPUTERNAME="$VNET_HOSTNAME"
		fi
		Text "Network Neighborhood name? [$VNET_COMPUTERNAME] \c"
		read ANS XXX
		[ "x$ANS" != "x" ] && VNET_COMPUTERNAME="$ANS"

		Text "Network Neighborhood description? [$VNET_DESCRIPTION] \c"
		read ANS
		[ "x$ANS" != "x" ] && VNET_DESCRIPTION="$ANS"

		Text "Network Neighborhood logon domain? [$VNET_LOGONDOMAIN] \c"
		read ANS XXX
		[ "x$ANS" != "x" ] && VNET_LOGONDOMAIN="$ANS"

		Text ""
		Text ""
		Text "========================================================"
		Text These are the settings:
		if [ $VNET_DHCP -eq 0 ]
		then
			Text "   IP Address : $VNET_IPADDRESS"
			Text "   IP Mask    : $VNET_IPMASK"
			Text "   Internet name : $VNET_HOSTNAME"
			Text "   DNS domain  : $VNET_DOMAIN_N"
			Text "   DNS servers : $VNET_DNSSERVERS"
			Text "   DNS domain search order : $VNET_DOMAINORDER"
			Text "   Gateway address : $VNET_GATEWAYS"
		else
			Text "   IP Address : assigned automatically"
		fi
		Text "Network Neighborhood settings"
		Text "   Workgroup : $VNET_WORKGROUP"
		Text "   Name : $VNET_COMPUTERNAME"
		Text "   Description: $VNET_DESCRIPTION"
		Text "   Logon domain : $VNET_LOGONDOMAIN"
		Text ""
		while true
		do
			Text "Is the above OK? [y/n] \c"
			read ANS
			case $ANS in
			y*|Y*)
				# Log the settings
				#SRI
				Log_vnet_settings
				#Log
				#Log "   IP Address : $VNET_IPADDRESS"
				#Log "   IP Mask    : $VNET_IPMASK"
				#Log "   Internet name : $VNET_HOSTNAME"
				#Log "   DNS domain  : $VNET_DOMAIN_N"
				#Log "   DNS servers : $VNET_DNSSERVERS"
				#Log "   DNS domain search order : $VNET_DOMAINORDER"
				#Log "   Gateway address : $VNET_GATEWAYS"
				#Log "Network Neighborhood settings"
				#Log "   Workgroup : $VNET_WORKGROUP"
				#Log "   Name : $VNET_COMPUTERNAME"
				#Log "   Description: $VNET_DESCRIPTION"
				#Log "   Logon domain : $VNET_LOGONDOMAIN"
				#ENDSRI
				return
				;;
			n*|N*)
				break
				;;
			*)
				Text Please answer y for yes, and n for no.
				;;
			esac
			Text ""
		done
		while true
		do
		  Text
		  Text "======================================================="
		  Text "You now have 3 options:"
		  Text "1: Repeat the process of entering the network settings"
		  Text "   and correct any values previously entered."
		  Text "2: Repeat the process of entering the network settings"
		  Text "   using the original default values."
		  Text "3: Quit and abort the installation of Windows".
		  Text ""
		  Text "Important note: You are permitted to defer setting some"
		  Text "or all of the items.  Defered items can be set later"
		  Text "inside your Windows session using the Network option in"
		  Text "the Control Panel."
		  Text ""
		  Text "Enter 1, 2 or 3 for the desired action"
		  Text "and press return \c"
			read ANS
			case $ANS in
			1)
				break
				;;
			2)
				Set_vnet_defaults
				break
				;;
			3)
				Text ""
				interruptProc
				;;
			*)
				Text ""
				Text "Please enter 1, 2 or 3."
				Text ""
				;;
			esac
		  Text ""
			
		done
	done
}


# This function sets Use_Merge_Winsock to "ws1", "ws2", or "not".
Ask_network()
{
	if [ "$WIN_VERSION" = "win95" ]
	then
		WS_NAME="Winsock 1.1"
		WS_USE=ws1
	else
		WS_NAME="Winsock 2"
		WS_USE=ws2
	fi
		
	#SRI
	#################
	# short circuit #
	#################
	# don't run this if you are being run from the new gui
	if [ -n "$SRI_FROM_GUI" ]
	then

		# we're replacing this because it was done
		# at the end of the function
		Log Use_Merge_Winsock=$Use_Merge_Winsock

		return
	fi
	#ENDSRI
	grep "^MERGE_VNETWORK=off" /etc/default/merge > /dev/null
	if [ $? -eq 0 ]
	then
		# Merge vnet is disabled, so use the standard winsock setup.
		Use_Merge_Winsock=$WS_USE
		return
	fi
	if [ "$SYSTEMTYPE" = "linux" ]
	then
		# Make sure the system has networking, because that must be
		# installed if VNET is to be used.  Don't use /proc/sys/net
		# since the GRKERNSEC patch does not permit mortal users
		# to do that.
		"$STATICMERGE"/bin/ishome
		ISHOME=$?
		EIFS="`LANG=en_US /sbin/ifconfig -a 2>/dev/null |
			grep Ethernet`"
		if [ -z "$EIFS" -o $ISHOME -ne 0 ]
		then
			# No networking so use the standard winsock setup
			# instead of VNET.
			Use_Merge_Winsock=$WS_USE
			return
		fi
	elif [ "$SYSTEMTYPE" = "sco32" ]
	then
		if [ ! -f /etc/strcf.d/05dlpi -a ! -h /etc/strcf.d/05dlpi ]
		then
			# No networking so use the standard winsock setup
			# instead of VNET.
			Use_Merge_Winsock=$WS_USE
			return
		fi
	else
		if [ ! -f /etc/inst/nd/filter ]
		then
			# No networking so use the standard winsock setup
			# instead of VNET.
			Use_Merge_Winsock=$WS_USE
			return
		fi
	fi
    # Start interactive questions about network stuff.
    while true
    do
	Text "========================================="
	Text "Right now you have to choose between using simple"
	Text "'Winsock' networking or setting up full Windows networking using"
	Text "VNET networking."
	Text
	Text "VNET networking allows the use of 'Network Neighborhood', but"
	Text "requires a unique IP address to identify your Windows session."
	Text "The IP address can be obtained automatically if you have a"
	Text "DHCP server on your network. Otherwise, you are required to"
	Text "provide a unique IP address, network mask, DNS configuration"
	Text "and gateway address."
	Text
	Text "Winsock networking does not require you to specify any of"
	Text "these, but provides only $WS_NAME functionality, which is"
	Text "sufficient for internet access and most Windows network"
	Text "functionality, but not Network Neigborhood browsing."
	Text 
	Text "If you don't know what to do, choose Winsock networking."
	Text 
	Text "Which type of networking is to be installed now? "
	Text "   (Type 'w' for $WS_NAME networking, 'v' for VNET networking,"
	Text "   or 'q' to Quit the installation of Windows, and then press"
	Text "   return or enter.)"
	Text "[w/v/q] \c"
	read ANS
	case "$ANS" in
	w)
		Use_Merge_Winsock=$WS_USE
		break
		;;
	v)
		Use_Merge_Winsock=not
		Query_vnet_info
		break
		;;
	q)
		exit 6
		;;
	*)
		Text "Please type in w, v, or q"
		;;
	esac
    done
    Log Use_Merge_Winsock=$Use_Merge_Winsock
}

# End of function declarations.  Start of MAIN code.

# Do a sync and sleep here to give enough time so that later checks
# for filesystem space are more likely to usefully accurate.
sync
sleep 1

MYNAME="$0"

Get_System_Variables

# By default attach empty read-only floppy images for the A and B drive,
# to prevent hangs if Windows Setup probes the floppy drives.
AFLOP="+aa:=$STATICMERGE/diskimages/f.dsk,r"
BFLOP="+ab:=$STATICMERGE/diskimages/f.dsk,r"

# Setting global varaibles that depend on the system variables
GLOBAL_AUTOEXEC=$VARMERGE/dosroot/autoexec.bat

WSHUTDN_PROG=$STATICMERGE/mwin95/wshutdn.exe

SLURP_SH="$STATICMERGE/bin/slurp.sh"
# BOOT_SH=/opt/win4lin/bt.sh

# if [ ! -x $BOOT_SH ]
# then
# 	echo $BOOT_SH does not exist or is not executable!
# 	exit 41
# fi

Log "Log of $0"
Log "args: $*"
Log `date`

MERGENAME=Merge
eval `grep "^MERGENAME=" "$STATICMERGE"/Version`

# Temporary undocumented way to sepecify an alternate win configuration
# for use when using winsetup, since in that situation you cannot
# use the new (and undocumented) "config" option.  Once winsetup
# is updated to use the "config" option, then the use of
# this environment variable should be removed.
if [ "x$MERGE_INSTALL_WIN_CONFIG" != "x" ]
then
	WIN_CONFIG="$MERGE_INSTALL_WIN_CONFIG"
	Check_winconfig
	if [ $? -ne 0 ]
	then
		# Note that because this is undocumented, there
		# is not a great need to NLSize this error message.
		Echo "Cannot use configuration $WIN_CONFIG because"
		Echo "it either does not exist or is not a Windows"
		Echo "type configuration."
		Echo "(The configuration name was specified via the"
		Echo "MERGE_INSTALL_WIN_CONFIG environment variable.)"
		Exit 38
	fi
fi

Check_win4lin_license()
{
	return
	# If there is a problem the chk_lic_file script
	# will print an error message and return an error code.
	# Error code  17 means that the license is OK, but cannot
	# start up a Windows session now, usually because too
	# many Windows sessions are already running.
	M3=`"$STATICMERGE"/bin/chk_lic_file -s`
	ERR_CODE=$?
	[ $ERR_CODE -eq 0 ] && return
	if [ $ERR_CODE -eq 17 ]
	then
		if [ "_$GUI_OPT" = "_" -o "_$SRI_FROM_GUI" = "_" ]
		then
			# Need to NLSize this message!
			M1="Cannot start up another Windows session now."
			M2="To many Windows sessions already running."
			Echo $M1
			Echo $M2
			$STATICMERGE/x_msg -e "${M1} ${M2}" 
		fi
		exit 51
	fi
	if [ "_$GUI_OPT" = "_" -o "_$SRI_FROM_GUI" = "_" ]
	then
		# Need to NLSize this message!
		M1="There is a problem with the Win4Lin license. "
		M2="Use the Win4Lin installer to add or fix the license. "
		Echo $M1
		Echo $M2
		Echo $M3
		$STATICMERGE/x_msg -e "${M1} ${M2} (${M3})" 
	fi
	[ $ERR_CODE -eq 13 ] && exit 55
	exit 50
}
interruptProc()
{
	Echo "Installation cancelled by the user."

	Exit 6
}
trap interruptProc 1 2 3 15

Printopts()
{
  [ "$DO_SILENT" = "silent" -o "x$GUI_OPT" = "xgui" ] && Exit 14
  # The following needs to be NLSized! ! !
  Echo ""
  Echo "This command installs Windows for a user's personal use."
  Echo ""
  Echo "The normal usage is to use no parameters and have Windows be installed"
  Echo "from the Windows files that were loaded by the 'loadwindowsCD' command."
  Echo "If for some reason you do not want to do this you can install Windows"
  Echo "from another location using some of the following options."
  Echo "Usage:"
  Echo "$MYNAME [fromdir S | cd | cddevice S ]"
  Echo "      [subdir S] [windir S] [cdletter S] [cmd S]"
  Echo "      [floppyok | floppyoka | floppyokb ]"
  Echo "      [floppydeva S | floppyfilea S]"
  Echo "      [floppydevb S | floppyfileb S]"
  Echo ""
  Echo "Of the first three options, at most one may be used:"
  Echo "  fromdir S  - Specify the UNIX directory S where a the contents of"
  Echo "               the Windows CD was copied or is currently mounted."
  Echo "  cd         - Install from a CD from a default CD drive."
  Echo "  cddevice S - Install from a CD using the specified device name 'S'"
  Echo "               the CD drive."
  Echo "When none of the above is specified then the Windows CD must have"
  Echo "already been 'loaded' into the system using the 'loadwindowsCD'"
  Echo "command or the GUI equivalent."
  Echo ""
  Echo "  subdir S   - Specify the sub-directory where the SETUP program and"
  Echo "               the other Windows installation files are located."
  Echo "  windir S   - Specify the subdirectory of the personal drive where"
  Echo "               to install Windows."
  Echo "  cdletter S - Specify the CD-ROM drive letter to use for for Windows"
  Echo "               to access the CD.  This is only for when the 'cd' or"
  Echo "               'cddevice' options are also used."
  Echo "  cmd S      - Specify an alternate to the command SETUP."
  Echo "               If this option is used it must be the last option."
  Echo "To provide access to the floppy drives use one of:"
  Echo "  floppyoka  - Allow access to drive A"
  Echo "  floppyokb  - Allow access to drive B"
  Echo "  floppyok   - Allow access to drives A and B"
  Echo "Normally the automatic floppy device selection should be used."
  Echo "If for some reason different device names must be used, or you"
  Echo "must use a virtual floppy file, use these options:"
  Echo "To specify something special for drive A, use one of:"
  Echo "  floppydeva S  - Use device name S for the A drive."
  Echo "  floppyfilea S - Use virtual floppy name S for the A drive."
  Echo "To specify something special for drive B, use one of:"
  Echo "  floppydevb S  - Use device name S for the B drive."
  Echo "  floppyfileb S - Use virtual floppy name S for the B drive."
  Echo ""
  Exit 14
}

Unknown_opt()
{
  [ "$DO_SILENT" = "silent" -o "x$GUI_OPT" = "xgui" ] && Exit 14
  # The following needs to be NLSized! ! !
  Echo ""
  Echo "Error 14: Unknown parameter $1"
  Printopts
}

Set_Final_Configuration_Options()
{
	Log "Setting X Cut/Paste on"
	winconfig $WIN_CONFIG set xcutandpaste=true
}

#SRI

#    We are being called from a gui.  Do appropriate setup
SRI_startup()
{
	SRI_FROM_GUI=TRUE

	# If we're not tracing, chuck the non-gui progress info
	[ -z "$TRACE" ] && exec 1>/dev/null
}

SRI_output()
{
	[ -n "$SRI_FROM_GUI" ] && eval "echo $* >&$SRI_OUTPUT_FD"
}

SRI_FROM_GUI=""
SRI_JUST_VNET="" 
SRI_OUTPUT_FD=3
if [ ! -z "$SRI_PROGRESS_FILE" ]
then
	SRI_OUTPUT_FILE=$SRI_PROGRESS_FILE
	export SRI_OUTPUT_FILE
fi

#ENDSRI

while true
do
	# Note, a number of options are undocumented.  This means
	# that they are for internal use only, and may change or
	# be removed at any time.  So don't use them except for
	# Internal Merge/Win4Lin stuff.
	[ "x$1" = "x" ] && break
	case $1 in
	trace)
		shift
		TRACE=trace
		;;
	floppyok)
		shift
		AFLOP=
		BFLOP=
		;;
	floppyoka)
		shift
		AFLOP=
		;;
	floppyokb)
		shift
		BFLOP=
		;;
	floppydeva)
		# Specifies the floppy device to use for drive A.
		shift
		if [ "x$1" = "x" ]
		then
			Echo "Error 38: Invalid parameters."
			Exit 38
		fi
		MERGE_ADRIVE_FORMATTABLE="$1"
		export MERGE_ADRIVE_FORMATTABLE
		MERGE_ADRIVE_AUTOSENSING="$1"
		export MERGE_ADRIVE_AUTOSENSING
		AFLOP=
		shift
		;;
	floppydevb)
		# Specifies the floppy device to use for drive B.
		shift
		if [ "x$1" = "x" ]
		then
			Echo "Error 38: Invalid parameters."
			Exit 38
		fi
		MERGE_BDRIVE_FORMATTABLE="$1"
		export MERGE_BDRIVE_FORMATTABLE
		MERGE_BDRIVE_AUTOSENSING="$1"
		export MERGE_BDRIVE_AUTOSENSING
		BFLOP=
		shift
		;;
	floppyfilea)
		# Specifies a file to use a floppy drive A.
		shift
		if [ "x$1" = "x" ]
		then
			Echo "Error 38: Invalid parameters."
			Exit 38
		fi
		AFLOP="$1"
		shift
		;;
	floppyfileb)
		# Specifies a file to use a floppy drive B.
		shift
		if [ "x$1" = "x" ]
		then
			Echo "Error 38: Invalid parameters."
			Exit 38
		fi
		BFLOP="$1"
		shift
		;;
	1)
		shift
		PHASE1_ONLY=phase1_only
		Log PHASE1_ONLY = $PHASE1_ONLY
		;;
	2)
		shift
		PHASE2_ONLY=phase2_only
		Log PHASE2_ONLY = $PHASE2_ONLY
		;;
	silent)
		shift
		DO_SILENT="silent"
		;;
	gui)
		# We don't document this.  It is for use only when
		# this script is being run from the Merge GUI.
		shift
		GUI_OPT="gui"
		ARG_X="+x"
		;;
	slurp)
		# Used for slurp install.
		shift
		DOING_SLURP="slurp"
		;;
	x|+x)
		# Allow "x or +x" even though we don't document it,
		# because other Merge commands use the "+x" option
		# to indicate that we are using X.
		shift
		ARG_X="+x"
		;;
	checklicense)
		# Exit with non-zero there is no license installed
		# or if there is already a Windows session running.
		if [ "$SYSTEMTYPE" = "linux" ]
		then
			# This function does not return if there is a
			# problem with the license or an existing session.
			Check_win4lin_license
		fi
		exit 0
		;;
	checkwin)
		Determine_Personal
		Determine_Windir
		# Exit with non-zero if Windows9x is already installed.
		Check_Win9x
		if [ $? -eq 1 ]
		then
			exit 1
		fi
		exit 0
		;;
	checkdos)
		Check_DOS7
		if [ $? -ne 0 ]
		then
			exit 1
		fi
		exit 0
		;;
	checkcabs)
		Check_Wincabs
		if [ $? -ne 0 ]
		then
			exit 1
		fi
		exit 0
		;;
	fromdir)
		shift
		if [ "x$1" = "x" ]
		then
			Echo "Error 18: fromdir value incomplete"
			Exit 18
		fi
		FROMDIRECTORY=$1
		shift
		;;
	windir)
		shift
		if [ "x$1" = "x" ]
		then
			Echo "Error 38: Invalid parameters."
			Exit 38
		fi
		WINDIR=$1
		shift
		;;
	cd)
		INST_FROM_CD=yes
		;;
	cdletter)
		shift
		if [ "x$1" = "x" ]
		then
			Echo "Error 11: cdletter value incomplete"
			Exit 11
		fi
		SOURCE_DRIVE_LETTER=$1

		shift
		;;
	cddevice)
		shift
		if [ "x$1" = "x" ]
		then
			Echo "Error 22: cddevice value incomplete"
			Exit 22
		fi
		CDDEVICE=$1
		shift
		if [ ! -b $CDDEVICE ]
		then
			Echo "Error 33: Invalid CD-ROM device name specified:"
			Echo "        $CDDEVICE"
			Echo "Cannot continue.\n"
			Exit 33
		fi
		INST_FROM_CD=yes
		;;
	productid)
		# This option is not normally used by end users.
		shift
		if [ "x$1" = "x" ]
		then
			Echo "Error 38: Invalid parameters."
			Exit 38
		fi
		PRODUCT_ID=$1
		shift
		;;
	nomsbprodkey)
		# This option is not normally used by end users.
		shift
		USE_MSBATCH_PRODUCT_KEY=FALSE
		;;
	subdir)
		shift
		if [ "x$1" = "x" ]
		then
			Echo "Error 38: Invalid parameters."
			Exit 38
		fi
		SETUP_DIR=$1
		shift
		;;
	cmd)
		# The rest of the command line is the command to run
		# to install Windows.  It can be a full path name.
		# To make it easy to enter, it can have "/" instead of "\"
		# as path separators.  We convert it for the user.
		shift
		if [ "x$1" = "x" ]
		then
			Echo "Error 38: Invalid parameters."
			Exit 38
		fi
		SETUPCMD="$*"
		break
		;;
	config)
		# This option is not documented yet.
		# Perhaps someday (and the GUI will allow a user to choose).
		# When it is documented there should be a specific
		# error messages here.
		shift
		if [ "x$1" = "x" ]
		then
			Echo "Error 38: Invalid parameters."
			Exit 38
		fi
		WIN_CONFIG=$1
		Check_winconfig
		if [ $? -ne 0 ]
		then
			Echo "Error 38: Invalid parameters."
			Exit 38
		fi
		shift
		;;
	savephase1)
		# Note: This is not a supported option, so don't complain if
		# it does not work!  Also note that it is not documented.
		shift
		if [ -d $SAVEFILE ]
		then
			Echo "Error 12: $SAVEFILE is a directory!"
			Exit 12
		fi
		PHASE1_OP=save
		;;
	usesavedphase1)
		# Note: This is not a supported option, so don't complain if
		# it does not work!  Also note that it is not documented.
		shift
		if [ ! -f $SAVEFILE -a ! -h $SAVEFILE ]
		then
		   Echo "Error 13: saved phase1 file $SAVEFILE does not exist."
			Exit 13
		fi
		PHASE1_OP=use
		PHASE2_ONLY=phase2_only
		;;
	h|-h|-\?|help)
		Printopts
		;;
	vnet)
		shift
		Use_Merge_Winsock=not
		Get_vnet_defaults
		Set_vnet_defaults
		;;
	winsock)
		shift
		Use_Merge_Winsock=ws_default
		;;
	winsock1)
		shift
		Use_Merge_Winsock=ws1
		;;
	winsock2)
		shift
		Use_Merge_Winsock=ws2
		;;
	interactive_configuration)
		shift
		INTERACTIVE_CONFIG=yes
		;;
	#SRI
	sri_from_gui)
		shift
		SRI_startup
		;;
	sri_just_vnet)
		shift
		SRI_JUST_VNET=TRUE
		;;
	#ENDSRI
	*)
		Unknown_opt
		;;
	esac
done
if [ "$SYSTEMTYPE" = "linux" ]
then
	Check_win4lin_license
fi


#SRI
if [ -n "$SRI_FROM_GUI" ]
then
	if [ -n "$SRI_JUST_VNET" ]
	then
		# check that they told us the name of the named pipe
		[ -z "$SRI_OUTPUT_FILE" ] && Exit 56
		echo "piping through $SRI_OUTPUT_FILE"
		# redirect channel $SRI_OUTPUT_FD to $SRI_OUTPUT_FILE
		#FIXME sh/could we check this for success?
		eval "exec ${SRI_OUTPUT_FD}> $SRI_OUTPUT_FILE"

		Get_vnet_defaults
		#I dont think this line would serve any purpose here:
		#Set_vnet_defaults
		SRI_report_vnet_defaults
		Exit 0
	else
		if [ -n "$SRI_PROGRESS_FILE" ]
		then
			eval "exec ${SRI_OUTPUT_FD}> $SRI_PROGRESS_FILE"
		fi
	fi
	# the SRI will set this in the environment if appropriate
	if [ $Use_Merge_Winsock="not" ]
	then
		# the SRI will have put these settings in the environment
		Log_vnet_settings
	fi
fi
#ENDSRI

PATH=/usr/X11R6/bin:/usr/bin/X11:$PATH
if [ -n "$DISPLAY" ]
then
	ARG_X="+x"
fi
if [ "x$DOING_SLURP" = "xslurp" -a ! -x $SLURP_SH ]
then
	echo $SLURP_SH does not exist or is not executable!
	exit 38
fi

# Don't allow dynamic resize during install since it won't work
# (because mrgwin32.exe doesn't run during install)
MERGE_XCRT_DISABLE_DYNAMIC_RESIZE=TRUE
export MERGE_XCRT_DISABLE_DYNAMIC_RESIZE

# When no parameters are given that say where to install from,
# then see if can install from the automatic location.
# If not then report a failure.
if [ "x$FROMDIRECTORY" = "x" -a "$INST_FROM_CD" = "no" ]
then
	Check_Wincabs
	if [ $? -eq 0 -a "x$DOING_SLURP" = "x" ]
	then
		# Tell user that Windows files have not been loaded.
		Echo ""
		Echo "Error 54: Cannot install Windows because the"
		Echo "Windows files have not been loaded."
		Echo "The command "loadwindowsCD" or the GUI equivalent"
		Echo "is used to load the Windows files."
		Echo ""
		Exit 54
	fi
	FROMDIRECTORY="$DEFAULT_CAB_DIR"
fi

if [ ! -x /bin/ed -a ! -x /usr/bin/ed ]
then
	Echo ""
	Echo "Error, There is no 'ed' program installed in /bin or /usr/bin."
	Echo "This program must be installed before trying to install Windows."
	Echo ""
	Exit 24
fi

if [ ! -f /etc/lindowsos-version ]; then
	# If we're not running under Lindows, prevent root from installing
	# since running Windows as root is a BAD idea!
	Must_Not_Be_Root
fi
if [ "x$FROMDIRECTORY" = "x" ]
then
	Echo Install Windows9x from CD.
elif [ "x$DOING_SLURP" = "xslurp" ]
then
        Echo Install Windows9x from the Native Windows Partition.
else 
	Echo Install Windows9x from ${FROMDIRECTORY}.
fi

Determine_Personal
Source_Setup
   # The following function Determine_CD_Setup validates and/or sets
   # SETUP_DIR, WIN_VERSION, WIN95_VERSION and WIN98_VERSION.
if [ "x$DOING_SLURP" = "x" ]
then  
	Determine_CD_Setup
	Check_WinME_Fromdir_Override
fi

if [ "x$Use_Merge_Winsock" = "x" ]
then
	Ask_network
elif [ "$Use_Merge_Winsock" = "ws_default" ]
then
	if [ "$WIN_VERSION" = "win95" ]
	then
		Use_Merge_Winsock=ws1
	else
		Use_Merge_Winsock=ws2
	fi
elif [ "$Use_Merge_Winsock" = "ws2" -a "$WIN_VERSION" = "win95" ]
then
	Echo ""
	Echo "Error: Cannot install with Winsock 2 on Windows 95."
	Exit 58
fi

Log "Use_Merge_Winsock = $Use_Merge_Winsock"
if [ "$Use_Merge_Winsock" = "ws2" ]
then
	Set_winsock2_defaults
fi

mkdir $PDIR/.boot 2> /dev/null
if [ "x$FROMDIRECTORY" != "x" -a "$INST_FROM_CD" = "no" ]
then
	if [ -f "$FROMDIRECTORY/$SETUP_DIR/b.dsk" ]
	then
		cp "$FROMDIRECTORY/$SETUP_DIR/b.dsk" "$PDIR/.boot/b.dsk"
		chmod 644 "$PDIR/.boot/b.dsk"
	elif [ -f "$FROMDIRECTORY/b.dsk" ]
	then
		cp "$FROMDIRECTORY/b.dsk" "$PDIR/.boot/b.dsk"
		chmod 644 "$PDIR/.boot/b.dsk"
	fi
	
	if [ -f "$FROMDIRECTORY/$SETUP_DIR/info.txt" ]
	then
		cp "$FROMDIRECTORY/$SETUP_DIR/info.txt" "$PDIR/.boot/info.txt"
		chmod 644 "$PDIR/.boot/info.txt"
		CAB_DIR="$FROMDIRECTORY/$SETUP_DIR"
		echo "CAB_DIR=$FROMDIRECTORY/$SETUP_DIR" >> "$PDIR/.boot/info.txt"
	elif [ -f "$FROMDIRECTORY/info.txt" ]
	then
		cp "$FROMDIRECTORY/info.txt" "$PDIR/.boot/info.txt"
		chmod 644 "$PDIR/.boot/info.txt"
		CAB_DIR="$FROMDIRECTORY"
		echo "CAB_DIR=$FROMDIRECTORY" >> "$PDIR/.boot/info.txt"
	fi
fi

if [ ! -f "$PDIR/.boot/info.txt" ]
then
	echo "WINDOWS_VERSION=$WINDOWS_VERSION" > "$PDIR/.boot/info.txt"
else
	# replace the WINDOWS_VERSION entry which must come from loadwindows.sh
	# The reason is loadwindows.sh can not exactly tell windows version
	# at least when it comes to European windows, for example, it always
	# miss-recognize French Win98se to win98original
	cat $PDIR/.boot/info.txt | \
            sed -e '/^WINDOWS_VERSION=.*/d' \
	    >> $PDIR/.boot/info.txt.new
	eval 'mv $PDIR/.boot/info.txt.new $PDIR/.boot/info.txt'
	echo "WINDOWS_VERSION=$WINDOWS_VERSION" >> "$PDIR/.boot/info.txt"
fi

echo "COUNTRY=$COUNTRY" >> "$PDIR/.boot/info.txt"
echo "CODEPAGE=$CODEPAGE" >> "$PDIR/.boot/info.txt"
echo "WIN_CODESET=$WIN_CODESET" >> "$PDIR/.boot/info.txt"

# Add the HOST_CODESET entry indicating the Unix/Linux codeset
# being used to install Windows

Dos_Cmd +Codeset >> "$PDIR/.boot/info.txt"

Check_Win_Upgrade
Determine_Zoom

if [ "x$DOING_SLURP" = "x" ]
then
	Determine_DOS7
	Determine_Windir
	echo "WINDIR=$WINDIR" >> "$PDIR/.boot/info.txt"
	Determine_Command
	Get_Product_ID
	Check_Space
	Fixup_Configs
	Setup_MSBATCH
	SRI_output "progress:5"
	Phase_1
	SRI_output "progress:30"
else  
	# We're slurping
	Determine_Windir
	echo "WINDIR=$WINDIR" >> "$PDIR/.boot/info.txt"
	Fixup_Configs
	Setup_MSBATCH
	Slurp_NativePartition 
	Fix_NativePartition 
fi

Magic_Step
SRI_output "progress:35"
Phase_2
SRI_output "progress:75"
Touch_Up
SRI_output "progress:80"
Change_MyDoc1
Change_MyDoc2
SRI_output "progress:85"
Setup_InternetExplorer
SRI_output "progress:90"
Fix_Printer
Set_Final_Configuration_Options
SRI_output "progress:95"
Clean_Up
SRI_output "progress:100"

Echo Windows installed.
Exit 0
