#!/bin/sh
# $Id: win_network.sh,v 1.7 2003/06/08 23:33:00 lreiter Exp $ 
# SCCSID(@(#)win_network.sh	1.7) Modified 00:15:43 8/23/01
# A utility for switching network types between winsock and vnet types.

# + + + + VNET setup functions + + + + + + + + + + + + + + + + + + + + + +

Fixup_Network_Regsave()
{
	Log Fixup_Network_Regsave

	# First go put everything back into the registry!

	BAT_FILE="${C_DIR}winreg.bat"
	rm -f "$BAT_FILE"

	cd "${C_DIR}"
	mv .boot/regsave .

	DOSText "c: " > "$BAT_FILE"
	DOSText "cd \\\\regsave "  >> "$BAT_FILE"
	DOSText "$REGEDIT 1.reg " >> "$BAT_FILE"
	DOSText "$REGEDIT 2.reg " >> "$BAT_FILE"
	DOSText "$REGEDIT 3.reg " >> "$BAT_FILE"
	DOSText "$REGEDIT 4.reg " >> "$BAT_FILE"
	DOSText "$REGEDIT 5.reg " >> "$BAT_FILE"
	DOSText "$REGEDIT 6.reg " >> "$BAT_FILE"
	DOSText "$REGEDIT 7.reg " >> "$BAT_FILE"
	
	$DOSWINBINDIR/dos +k$WIN_CONFIG +b +r winreg.bat >> "$LOGFILE" 2>&1

	if [ "x$TRACE" != "xtrace" ]
	then
		rm -rf "regsave"
		rm -f "$BAT_FILE"
	fi

	# Now if the current status is that VNET is not supposed to
	# be installed, remove the MERGE NIC entry
	if [ "$CURRENT_NETTYPE" != "vnet" ]
	then
		Remove_Vnet_Registry
	fi
}

Set_WORKGROUP_SET()
{
	#
	Log Set_Dialup_Installed
	WORKGROUP_SET="no"

	BAT_FILE="${C_DIR}winreg.bat"
	rm -f "$BAT_FILE"

	cd "${C_DIR}"
	rm -rf mrgntmp
	mkdir -p mrgntmp

	DOSText "c: " > "$BAT_FILE"
	DOSText "cd \\\\mrgntmp "  >> "$BAT_FILE"
	DOSText "$REGEDIT /E wrkgroup.reg HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Services\\VxD\\VNETSUP" >> "$BAT_FILE"

	$DOSWINBINDIR/dos +k$WIN_CONFIG +b +r winreg.bat >> "$LOGFILE" 2>&1

	# Find the NET entry associated with the MS PPP adapter
	grep -i 'Workgroup.*=' mrgntmp/wrkgroup.reg | \
	    sed -e '{
		s/"//g
		s/.*=//
	    }' | $STATICMERGE/charconv -d -x > mrgntmp/workgroup_name

	WORKGROUP_NAME="`cat mrgntmp/workgroup_name`"
	if [ -z "$WORKGROUP_NAME" ]
	then
		Log "Workgroup Entry not found"
		WORKGROUP_SET="no"
	else
		Log "Workgroup value found: $WORKGROUP_NAME"
		WORKGROUP_SET="yes"
	fi
	cd "${C_DIR}"
	[ "$TRACE" = "trace" ] || rm -rf mrgntmp
}

Set_DIALUP_ADAPTER_INSTALLED()
{
	#
	Log Set_Dialup_Installed
	DIALUP_ADAPTER_INSTALLED="no"

	BAT_FILE="${C_DIR}winreg.bat"
	rm -f "$BAT_FILE"

	cd "${C_DIR}"
	rm -rf mrgntmp
	mkdir -p mrgntmp

	DOSText "c: " > "$BAT_FILE"
	DOSText "cd \\\\mrgntmp "  >> "$BAT_FILE"
	DOSText "$REGEDIT /E allnet.reg HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Services\\Class\\Net" >> "$BAT_FILE"

	$DOSWINBINDIR/dos +k$WIN_CONFIG +b +r winreg.bat >> "$LOGFILE" 2>&1

	# Find the NET entry associated with the MS PPP adapter
	sed -n -e '
	    /MS$PPP/{
		x
		s/\[//g
		s/\]//g
		s/\\/\\\\/g
		p
		q
	    }
	    /Class\\Net\\0...\]/h
	' mrgntmp/allnet.reg > mrgntmp/dialup_entry

	DIALUP_ADAPTER_KEY="`$STATICMERGE/charconv -d -x mrgntmp/dialup_entry`"
	if [ -z "$DIALUP_ADAPTER_KEY" ]
	then
		Log "Dialup Entry not found"
		DIALUP_ADAPTER_INSTALLED=no
	else
		Log "Dialup Entry found"
		DIALUP_ADAPTER_INSTALLED=yes
	fi
	cd "${C_DIR}"
	[ "$TRACE" = "trace" ] || rm -rf mrgntmp
}

Set_MS_PROVIDER_DISABLED()
{
	#
	Log Set_MS_PROVIDER_DISABLED

	#
	# Assume MS provider is NOT disabled.  This takes care of the
	# Win95 case where the winsock2 update is not installed.
	#
	MS_PROVIDER_DISABLED="no"

	BAT_FILE="${C_DIR}winreg.bat"
	rm -f "$BAT_FILE"

	cd "${C_DIR}"
	rm -rf mrgntmp
	mkdir -p mrgntmp

	DOSText "c: " > "$BAT_FILE"
	DOSText "cd \\\\mrgntmp "  >> "$BAT_FILE"
	DOSText "$REGEDIT /E allws2.reg HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Services\\Winsock2\\Parameters" >> "$BAT_FILE"

	$DOSWINBINDIR/dos +k$WIN_CONFIG +b +r winreg.bat >> "$LOGFILE" 2>&1

	if [ ! -s mrgntmp/allws2.reg ]
	then
		Log "Winsock2 Parameters not found in registry"
		cd "${C_DIR}"
		[ "$TRACE" = "trace" ] || rm -rf mrgntmp
		return
    	fi

	# Find the Enabled= entry associated with the MS provider
	sed -n -e '
	  :top
	    /LibraryPath.*rnr20.dll/benabled
	    n
	    btop
	  :enabled
	    /[Ee]nabled.*/{
	    	p
		q
	    }
	    n
	    benabled
	' mrgntmp/allws2.reg > mrgntmp/msdns_enabled

	if [ ! -s mrgntmp/msdns_enabled ]
	then
		Log "MS DNS Entry not found"
		cd "${C_DIR}"
		[ "$TRACE" = "trace" ] || rm -rf mrgntmp
		return
    	fi

	if grep -q -i 'enabled.*=.*1' mrgntmp/msdns_enabled >/dev/null 2>&1
	then
		Log "MS_PROVIDER_DISABLED no"
		MS_PROVIDER_DISABLED="no"
	else
		Log "MS_PROVIDER_DISABLED yes"
		MS_PROVIDER_DISABLED="yes"
	fi
	cd "${C_DIR}"
	[ "$TRACE" = "trace" ] || rm -rf mrgntmp
}


Subtract_reg()
{
	SOURCE_REG=$1
	SUBOUT_REG=$2
	DEST_REG=$3

	rm -f $DEST_REG
	sed -n -e '
	    3 {
		p
		q
	    }
	' $SUBOUT_REG > start_line 2>/dev/null
	if [ ! -s start_line ]
	then
		Log "Subract_reg empty start_line"
		return
	fi
	fgrep -n "`cat start_line`" $SOURCE_REG | \
		sed -e 's/:.*//' > start_line_num 2>/dev/null
	if [ ! -s start_line_num ]
	then
		Log "Subract_reg didn't find start_line"
		return
	fi

	START_LINE_NUM="`cat start_line_num`"
	ALL_MERGE_NIC_LINES=`wc -l $SUBOUT_REG | \
	    sed -e 's/^  *//' -e 's/[ 	].*//'`
	END_LINE_NUM="`expr $START_LINE_NUM + $ALL_MERGE_NIC_LINES - 3`"

	if [ "$START_LINE_NUM" -ge "$END_LINE_NUM" ]
	then
		Log "Subtract_reg: bad start $START_LINE_NUM end $END_LINE_NUM"
		return
	fi
	sed -e "${START_LINE_NUM},${END_LINE_NUM}d" $SOURCE_REG > $DEST_REG
}


Remove_Vnet_Registry()
{
	Log Save_Vnet_Registry

	BAT_FILE="${C_DIR}winreg.bat"
	rm -f "$BAT_FILE"

	cd "${C_DIR}"
	rm -rf mrgntmp
	mkdir -p mrgntmp

	DOSText "c: " > "$BAT_FILE"
	DOSText "cd \\\\mrgntmp "  >> "$BAT_FILE"
	DOSText "$REGEDIT /E allnet.reg HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Services\\Class\\Net" >> "$BAT_FILE"

	$DOSWINBINDIR/dos +k$WIN_CONFIG +b +r winreg.bat >> "$LOGFILE" 2>&1

	# Find the NET entry associated with the Merge NIC
	sed -n -e '
	    /Merge NIC/{
		x
		s/\[//g
		s/\]//g
		s/\\/\\\\/g
		p
		q
	    }
	    /NE2000 Compatible/{
		x
		s/\[//g
		s/\]//g
		s/\\/\\\\/g
		p
		q
	    }
	    /Class\\Net\\0...\]/h
	' mrgntmp/allnet.reg > mrgntmp/merge_nic_entry

	MERGE_NIC_KEY="`$STATICMERGE/charconv -d -x mrgntmp/merge_nic_entry`"
	if [ -z "$MERGE_NIC_KEY" ]
	then
		Log "Vnet Entry not found"
		cd "${C_DIR}"
		[ "$TRACE" = "trace" ] || rm -rf mrgntmp
		return
    	fi

	# Save the Merge NIC entry
	cd "${C_DIR}"
	mkdir -p vnetsave

	DOSText "c: " > "$BAT_FILE"
	DOSText "cd \\\\vnetsave "  >> "$BAT_FILE"
	DOSText "$REGEDIT /E mergenic.reg $MERGE_NIC_KEY" >> "$BAT_FILE"

	$DOSWINBINDIR/dos +k$WIN_CONFIG +b +r winreg.bat >> "$LOGFILE" 2>&1

	# Remove the Merge NIC

	if [ "$WINDOWS_VERSION" = "95classic" ]
	then
		#
		# Note: Windows95classic version of regedit does NOT have
		# the /D option, so we have to do it the "hard" way.
		#
		DOSText "c: " > "$BAT_FILE"
		DOSText "cd \\\\vnetsave "  >> "$BAT_FILE"
		DOSText "$REGEDIT /E regget.reg" >> "$BAT_FILE"

		$DOSWINBINDIR/dos +k$WIN_CONFIG +b +r winreg.bat \
			>> "$LOGFILE" 2>&1

		cd vnetsave
		rm -f regput.reg
		Subtract_reg regget.reg mergenic.reg regput.reg

		cd "${C_DIR}"
		if [ -s vnetsave/regput.reg ]
		then
			DOSText "c: " > "$BAT_FILE"
			DOSText "cd \\\\vnetsave "  >> "$BAT_FILE"
			DOSText "$REGEDIT /C regput.reg" >> "$BAT_FILE"

			$DOSWINBINDIR/dos +k$WIN_CONFIG +b +r winreg.bat \
				>> "$LOGFILE" 2>&1
		fi
		if [ "x$TRACE" != "xtrace" ]
		then
			rm -f vnetsave/regget.reg vnetsave/regput.reg \
				vnetsave/start_line*
		fi
	else
		DOSText "c: " > "$BAT_FILE"
		DOSText "cd \\\\vnetsave "  >> "$BAT_FILE"
		DOSText "$REGEDIT /D $MERGE_NIC_KEY" >> "$BAT_FILE"

		$DOSWINBINDIR/dos +k$WIN_CONFIG +b +r winreg.bat \
			>> "$LOGFILE" 2>&1
	fi

	cd "${C_DIR}"
	rm -rf .boot/vnetsave
	mv vnetsave .boot

	if [ "x$TRACE" != "xtrace" ]
	then
		rm -rf mrgntmp
		rm -f "$BAT_FILE"
	fi
}

Fixup_DNS_Configuration()
{
	#
	# Note: Windows95classic version of regedit does NOT have the
	# /D option, so the following code must not be used for win95classic.
	#
	Log Fixup_DNS_Configuration

	BAT_FILE="${C_DIR}winreg.bat"
	rm -f "$BAT_FILE"

	cd "${C_DIR}"
	rm -rf mrgntmp
	mkdir -p mrgntmp

	DOSText "c: " > "$BAT_FILE"
	DOSText "cd \\\\mrgntmp "  >> "$BAT_FILE"
	DOSText "$REGEDIT /E mstcp.reg HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Services\\VxD\\MSTCP" >> "$BAT_FILE"

	$DOSWINBINDIR/dos +k$WIN_CONFIG +b +r winreg.bat >> "$LOGFILE" 2>&1

	# Find the NET entry associated with the Merge NIC
	sed -n -e '
	    /\\MSTCP\\..*\]/q
	    p
	' mrgntmp/mstcp.reg > mrgntmp/dns.reg

	if grep -q -i 'hostname.*=.*no[nt]used' mrgntmp/dns.reg >/dev/null 2>&1
	then
		# Set a default WORKGROUP
		Log "Resetting DNS setup to default"
		$DOSWINBINDIR/dos +k$WIN_CONFIG +b +r \
			$REGEDIT_UNIX j:/mwin95/resetdns.reg >> "$LOGFILE" 2>&1
	fi
		
	if [ "x$TRACE" != "xtrace" ]
	then
		rm -rf mrgntmp
		rm -f "$BAT_FILE"
	fi
}


Replace_Vnet_Registry()
{
	Log Replace_Vnet_Registry

	BAT_FILE="${C_DIR}winreg.bat"
	rm -f "$BAT_FILE"

	cd "${C_DIR}"
	if [ ! -d .boot/vnetsave ]
	then
		Log "Replace_Vnet_Registry: .boot/vnetsave not found"
		return
	fi
	mv .boot/vnetsave .

	DOSText "c: " > "$BAT_FILE"
	DOSText "cd \\\\vnetsave "  >> "$BAT_FILE"
	DOSText "$REGEDIT mergenic.reg " >> "$BAT_FILE"
	
	$DOSWINBINDIR/dos +k$WIN_CONFIG +b +r winreg.bat >> "$LOGFILE" 2>&1

	if [ "x$TRACE" != "xtrace" ]
	then
		rm -rf "vnetsave"
		rm -f "$BAT_FILE"
	fi
}

get_current_info()
{
	CURRENT_NETTYPE=unknown
	CAB_DIR=unknown
	WINDOWS_VERSION=unknown
	if [ ! -f "$INFO_FILE" ]
	then
		Log "Missing file $INFO_FILE"
		return
	fi
	eval `grep "^CAB_DIR=" "$INFO_FILE"`
	if [ "x$CAB_DIR" = "x" ]
	then
		CAB_DIR=unknown
	fi
	WINDOWS_VERSION=`grep "^WINDOWS_VERSION=" "$INFO_FILE" | cut -d= -f2`
	if [ "x$WINDOWS_VERSION" = "x" ]
	then
		WINDOWS_VERSION=unknown
	fi
	case $WINDOWS_VERSION in
	    *95*)
	    	IS_WIN95=yes
		;;
	    *)
	    	IS_WIN95=no
	esac
	grep "NETWORK_TYPE=vnet" "$INFO_FILE" >> $NULL 2>>$NULL
	if [ $? -eq 0 ]
	then
		CURRENT_NETTYPE=vnet
		return
	fi
	grep "NETWORK_TYPE=winsock2" "$INFO_FILE" >> $NULL 2>>$NULL
	if [ $? -eq 0 ]
	then
		CURRENT_NETTYPE=winsock2
		return
	fi
	grep "NETWORK_TYPE=winsock" "$INFO_FILE" >> $NULL 2>>$NULL
	if [ $? -eq 0 ]
	then
		CURRENT_NETTYPE=winsock
		return
	fi
	CURRENT_NETTYPE=unknown
	return
}

change_info()
{
	INFO_FILE="${C_DIR}.boot/info.txt"
	if [ ! -f "$INFO_FILE" ]
	then
		Log "Creating missing file $INFO_FILE"
		rm -f "$INFO_FILE"
		Text "NETWORK_TYPE=$OP" > "$INFO_FILE"
		return
	fi
	TMPF=${C_DIR}.boot/file$$.tmp
	cat $INFO_FILE | sed -e "s/NETWORK_TYPE=.*/NETWORK_TYPE=$OP/" > $TMPF
	cmp -s $INFO_FILE $TMPF
	if [ $? -ne 0 ]
	then
		chmod u+w $INFO_FILE
		cp $TMPF $INFO_FILE
	fi
	rm -f $TMPF
}


# + + + + Text handling functions + + + + + + + + + + + + + + + + + + + + + +

# 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 $EPT "$*" >> "$LOGFILE"
	[ "x$TRACE" = "xtrace" ]       && echo $EPT "$*"
}

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

# Conditionally does an x_msg with the specified message
EchoX()
{
	if [ "$SRI_FROM_GUI" = "true" ]
	then
		if [ "$1" = "-e" ]
		then
			ERRORMODE="-e"
			shift
		fi
		$STATICMERGE/x_msg -N $ERRORMODE "$*"
	fi
}

# + + + Configuration functions + + + + + + + + + + + + + + + + + + + + + +
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.
	
	# Get the STATICMERGE, VARMERGE, DEFAULTMERGE, DOSWINBINDIR,
	# SYSTEMTYPE, etc by dotting in mrgssv.sh.
	. /etc/mrgssv.sh

	if [ "x$HOME" = "x" -o "x$HOME" = "x/" ]
	then
		eval `grep "^MERGE_ROOT_HOME=" $DEFAULTMERGE`
		USEHOME=$MERGE_ROOT_HOME
	else
		USEHOME=$HOME
	fi
	DOTMERGE=$USEHOME/.merge
	
	LOGFILE="$DOTMERGE/win_net.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
		OK_TO_LOG="no_log"
	fi
	MERGE_AUTOBACKGROUND=off
	export MERGE_AUTOBACKGROUND
	if [ "$SYSTEMTYPE" = "linux" ]
	then
		EPT="-e"
	fi
	DEFAULT_CAB_DIR=$VARMERGE/dosroot/wincabs
}

# Determine the "Personal Drive Letter" and where it is located
# from the configuration info.
# Put this info in the variables PLETTER and C_DIR.
# NOTE: C_DIR has a trailing slash.
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_drive_root c $WIN_CONFIG
	if [ $? -ne 0 ]
	then
		usrMsg STRING WIN_NETWORK_INVALID_C \
"Error: The C drive directory in the $WIN_CONFIG\n\
configuration is not valid.\n\
Cannot continue.  Exiting...\n" \
			"$WIN_CONFIG"
		Echo "$USRMSG_STRING"
		EchoX -e "$USRMSG_STRING"
		exit 1
	fi
	C_DIR=$DRV_ROOT

	Log Drive C rooted at $C_DIR

	if [ ! -d $C_DIR ]
	then
		mkdir $C_DIR
	fi
	if [ ! -d $C_DIR ]
	then
		usrMsg STRING WIN_NETWORK_MISSING_C \
"Error: The C drive directory $C_DIR\n\
does not exist, and could not be created.\n\
Cannot continue.  Exiting...\n" \
			"$C_DIR"
		Echo "$USRMSG_STRING"
		EchoX -e "$USRMSG_STRING"
		exit 19
	fi
}

# + + + MAIN --- start here --- + + + + + + + + + + + + + + + + + + + + + +  +
# (Everything above here must only be functions)

NULL=/dev/null
DO_SILENT=notsilent
TRACE=notrace

Get_System_Variables

# The script "set_nls.sh" uses STATICMERGE to set MFILE.
. $STATICMERGE/set_nls.sh
DM="LCC.MERGE.UNIX.WIN_NETWORK"

# Get command line parameters.
# The first parameter tells us what to do.
# The options are:
#			
#	-winsock1  Switch to Winsock1
#
#	-winsock2  Switch to Winsock2
#			
#	-vnet	Switch to VNET
#
# Added by ler: sri_from_gui is recognized, making certain messages and errors
# popup as x_msg's

OP=""
if [ "$1" = "sri_from_gui" ]
then
	SRI_FROM_GUI=true
	SRI_FD=4
	[ ! -z $SRI_PROGRESS_FILE ] && eval "exec ${SRI_FD}>$SRI_PROGRESS_FILE"
	shift
fi
case "$1" in
-w*1)
	# Option "-winsock1"
	OP="winsock"
	shift
	;;
-w*2)
	# Option "-winsock2"
	OP="winsock2"
	shift
	;;
-v*)
	# Option "-vnet"
	OP="vnet"
	shift
	;;
*)
	MYNAME="`basename $0`"
	usrMsg STRING WIN_NETWORK_SYNTAX  \
"Usage: $MYNAME [ -vnet | -winsock1 | -winsock2 ] [ config-name ]\n" \
		"$MYNAME"
	Echo "$USRMSG_STRING"
	exit 1
	;;
esac

WIN_CONFIG="$1"
[ "x$WIN_CONFIG" = "x" ] && WIN_CONFIG=win
# Make sure the configuration is a windows configuration.
SESS=`winconfig $WIN_CONFIG list session 2>>$NULL | cut -d= -f2`
if [ "x$SESS" = "x" ]
then
	usrMsg STRING WIN_NETWORK_NO_CONFIG  \
"Error, there is no configuration named $WIN_CONFIG\n" \
		"$WIN_CONFIG"
	Echo "$USRMSG_STRING"
	EchoX -e "$USRMSG_STRING"
	exit 1
fi
if [ "$SESS" = "dos" ]
then
	usrMsg STRING WIN_NETWORK_NOT_WIN_CONFIG  \
"Error, the configuration named $WIN_CONFIG \n\
is not a Windows configuration.\n" \
		"$WIN_CONFIG"
	Echo "$USRMSG_STRING"
	EchoX -e "$USRMSG_STRING"
	exit 1
fi

# Determine_Personal uses $WIN_CONFIG and sets C_DIR.
Determine_Personal

if [ -f "$C_DIR"/.merge_win95_lock ]
then
	usrMsg STRING WIN_NETWORK_IN_USE  \
"Error, Windows session currently using the\n\
$C_DIR directory.\n\
Cannot make changes now.\n" \
		"$C_DIR"
	Echo "$USRMSG_STRING"
	EchoX -e "$USRMSG_STRING"
	exit 1
fi

# Make sure there is an info file present
INFO_FILE="${C_DIR}.boot/info.txt"
if [ ! -f "$INFO_FILE" ]
then
	usrMsg STRING WIN_NETWORK_MISSING_FILE_OP \
"Error!\n\
Missing file $INFO_FILE.\n\
Cannot continue $OP operation.  Exiting...\n" \
		"$INFO_FILE" "$OP"
	Echo "$USRMSG_STRING"
	EchoX -e "$USRMSG_STRING"
	exit 1
fi

STATE=1
eval `grep "^WINDIR=" "${C_DIR}.boot/info.txt" 2>/dev/null`
Log "Boot info.txt WINDIR=$WINDIR"

if [ "x$WINDIR" = "x" ]
then
	STATE=2
	WINDIR=`winconfig $WIN_CONFIG list windowsdir | cut -d= -f2`
	if [ "x$WINDIR" = "x" ]
	then
		STATE=3
		# Default to the "windows" subdirectory.
		WINDIR=windows
	fi
fi
Log "Final WINDIR=$WINDIR"

REGEDIT="regedit/L:\\\\$WINDIR\\\\system.dat"
REGEDIT_UNIX="regedit -L:/$WINDIR/system.dat"

FULL_WDIR="${C_DIR}$WINDIR"
FULL_WSYSDIR="$FULL_WDIR/system"
if [ ! -d "$FULL_WDIR" ]
then
	case $STATE in
	1)
		usrMsg STRING WIN_NETWORK_MISSING_WIN_DIR_1 \
"Error, the Windows installation directory $WINDIR\n\
does not exist. ($FULL_WDIR)\n\
(specified in ${C_DIR}.boot/info.txt)\n" \
			"$WINDIR" "$FULL_WDIR" "$C_DIR"
		Echo "$USRMSG_STRING"
		EchoX -e "$USRMSG_STRING"
		;;
	2)
		usrMsg STRING WIN_NETWORK_MISSING_WIN_DIR_2 \
"Error, the Windows installation directory $WINDIR\n\
does not exist. ($FULL_WDIR)\n\
(Specified in configuration $WIN_CONFIG)\n" \
			"$WINDIR" "$FULL_WDIR" "$WIN_CONFIG"
		Echo "$USRMSG_STRING"
		EchoX -e "$USRMSG_STRING"
		;;
	3)
		usrMsg STRING WIN_NETWORK_MISSING_WIN_DIR_3 \
"Error, the Windows installation directory $WINDIR\n\
does not exist. ($FULL_WDIR)\n\
(Default location)\n" \
			"$WINDIR" "$FULL_WDIR"
		Echo "$USRMSG_STRING"
		EchoX -e "$USRMSG_STRING"
	esac
	exit 1
fi
if [ ! -d "$FULL_WSYSDIR" ]
then
	usrMsg STRING WIN_NETWORK_MISSING_WIN_SYSDIR \
"Error, the Windows system directory\n\
$FULL_WSYSDIR does not exist.\n" \
		"$FULL_WSYSDIR"
	Echo "$USRMSG_STRING"
	EchoX -e "$USRMSG_STRING"
	exit 1
fi

get_current_info
Log "CURRENT_NETTYPE=$CURRENT_NETTYPE"
ORIG_NETTYPE="$CURRENT_NETTYPE"

if [ "$CURRENT_NETTYPE" = "unknown" ]
then
	usrMsg STRING WIN_NETWORK_CORRUPT_FILE \
"Error!\n\
Corrupted file $INFO_FILE.\n" \
		"$INFO_FILE"
	Echo "$USRMSG_STRING"
	EchoX -e "$USRMSG_STRING"
	exit 1
fi

if [ "$OP" = "vnet" -a "$CURRENT_NETTYPE" = "vnet" ]
then
	usrMsg STRING WIN_NETWORK_ALREADY_VNET \
		"Current network type is already VNET.\n"
	Echo "$USRMSG_STRING"
	exit 0
fi

if [ "$OP" = "winsock1" -a "$CURRENT_NETTYPE" = "winsock" ]
then
	usrMsg STRING WIN_NETWORK_ALREADY_WINSOCK1 \
		"Current network type is already Winsock 1.\n"
	Echo "$USRMSG_STRING"
	exit 0
fi

if [ "$OP" = "winsock2" -a "$CURRENT_NETTYPE" = "winsock2" ]
then
	usrMsg STRING WIN_NETWORK_ALREADY_WINSOCK2 \
		"Current network type is already Winsock 2.\n"
	Echo "$USRMSG_STRING"
	exit 0
fi


WS2_DLL="$FULL_WSYSDIR/ws2_32.dll"
VTCP_VXD="$FULL_WSYSDIR/vtcp.386"
WSOCK32_DLL="$FULL_WSYSDIR/wsock32.dll"
WINSOCK_DLL="$FULL_WDIR/winsock.dll"
ICMP_DLL="$FULL_WSYSDIR/icmp.dll"

# Win95 Winsock2 update related definitions
W95_WS2_UPDATE="$VARMERGE/dosroot/share/W95ws2setup.exe"
WSOCK32_WS2_UPDATE="$FULL_WSYSDIR/.wsock32.dll.ws2"
WINSOCK_WS2_UPDATE="$FULL_WDIR/.winsock.dll.ws2"
ICMP_WS2_UPDATE="$FULL_WSYSDIR/.icmp.dll.ws2"

if [ "$OP" = "winsock2" -a "$IS_WIN95" = "yes" ]
then
	if [ ! -r "$W95_WS2_UPDATE" -a ! -r "$WS2_DLL" ]
	then
		usrMsg STRING WIN_NETWORK_W95_WINSOCK2 \
"\nIn order to use Winsock 2 with Windows 95, you must\n\
first download W95ws2setup.exe from Microsoft, name it\n\
$W95_WS2_UPDATE \n\
and then rerun win_network.\n" \
		    $W95_WS2_UPDATE
		Echo "$USRMSG_STRING"
		EchoX -e "$USRMSG_STRING"
		exit 1
	fi
fi

if [ "$CAB_DIR" = "unknown" ]
then
	# Use the default cab location.
	CAB_DIR="$DEFAULT_CAB_DIR"
	if [ -d "$CAB_DIR/cablists" ]
	then
		usrMsg STRING WIN_NETWORK_DEFAULT_WINCABS \
"Warning, Windows installation files directory\n\
not specified in $INFO_FILE.\n\
Using default location $CAB_DIR\n" \
			"$INFO_FILE" "$CAB_DIR"
		Echo "$USRMSG_STRING"
	fi
fi
if [ ! -d "$CAB_DIR/cablists" ]
then
	usrMsg STRING WIN_NETWORK_CABS_NOT_LOADED \
"The Window CD installation files are not properly loaded.\n\
Cannot complete operation.\n"
	Echo "$USRMSG_STRING"
	EchoX -e "$USRMSG_STRING"
	exit 13
fi


if [ -d "${C_DIR}.boot/regsave" ]
then
	# Handle the botched regsave from a previous Merge release
	Fixup_Network_Regsave
fi

Set_WORKGROUP_SET
Set_DIALUP_ADAPTER_INSTALLED

FIRST_TIME=no
if [ "$CURRENT_NETTYPE" != "vnet" -a "$DIALUP_ADAPTER_INSTALLED" = "no" ]
then
	Log "Old VNET stuff and DUN not installed so we have to lose it"
	rm -rf "${C_DIR}.boot/vnetsave"
fi

if [ "$CURRENT_NETTYPE" != "vnet" -a ! -d "${C_DIR}.boot/vnetsave" ]
then
	FIRST_TIME=first_time_vnet
fi

if [ "$ORIG_NETTYPE" = "winsock" ]
then
	change_info
fi

if [ "$OP" = "vnet" -o "$OP" = "winsock2" ]
then
	# Get rid of Winsock1 symlinks if they are there
	if [ -L ${FULL_WDIR}/winsock.dll ]
	then
		rm -f ${FULL_WDIR}/winsock.dll
	fi
	if [ -L ${FULL_WSYSDIR}/wsock32.dll ] 
	then
		rm -f ${FULL_WSYSDIR}/wsock32.dll
	fi
	if [ "$WORKGROUP_SET" = "no" ]
	then
		# Set a default WORKGROUP
		Log "Setting a default Workgroup"
		$DOSWINBINDIR/dos +k$WIN_CONFIG +b +r \
			$REGEDIT_UNIX j:/mwin95/wgroup.reg >> "$LOGFILE" 2>&1
	fi
	if [ "$DIALUP_ADAPTER_INSTALLED" = "no" ]
	then
		Log "Installing dialup networking"
		usrMsg STRING WIN_NETWORK_STANDARDIZING_STACK \
"Starting Windows to standardize the networking stack.\n"
		Echo "$USRMSG_STRING"
		if [ "$IS_WIN95" = "yes" ]
		then
			REG_FILE=j:/mwin95/msdial95.reg
		else
			REG_FILE=j:/mwin95/msdialsu.reg
		fi
		$DOSWINBINDIR/dos +k$WIN_CONFIG +b +r \
			$REGEDIT_UNIX $REG_FILE >> "$LOGFILE" 2>&1
		MERGE_DISABLE_RESTART=on
		export MERGE_DISABLE_RESTART
		"$STATICMERGE"/publicbin/win +k$WIN_CONFIG
		MERGE_DISABLE_RESTART=off 
	fi
fi

if [ "$IS_WIN95" = "yes" -a "$OP" != "winsock1" -a ! -r "$VTCP_VXD" ]
then
	Log "Allowing install of MS/TCP"

	usrMsg STRING WIN_NETWORK_TCPIP_CONFIG \
"Starting Windows so you can install the Microsoft TCP/IP protocol.\n
HINT: Select Microsoft under Manufacturers and then choose TCP/IP\n\
under Models.\n"
	Echo "$USRMSG_STRING"
	EchoX "$USRMSG_STRING"
	REG_FILE=j:/mwin95/tcpadd.reg
	$DOSWINBINDIR/dos +k$WIN_CONFIG +b +r \
		$REGEDIT_UNIX $REG_FILE >> "$LOGFILE" 2>&1
	MERGE_DISABLE_RESTART=on
	export MERGE_DISABLE_RESTART
	"$STATICMERGE"/publicbin/win +k$WIN_CONFIG
	MERGE_DISABLE_RESTART=off 
fi

if [ "$IS_WIN95" = "yes" -a "$OP" = "winsock2" -a ! -r "$WS2_DLL" ]
then
	Log "Installing the win95 winsock2 update"

	usrMsg STRING WIN_NETWORK_W95_WINSOCK2_UPDATE \
"Starting Windows to install the Windows 95 Winsock 2 update.\n"
	Echo "$USRMSG_STRING"
	REG_FILE=j:/mwin95/w95ms2ud.reg
	$DOSWINBINDIR/dos +k$WIN_CONFIG +b +r \
		$REGEDIT_UNIX $REG_FILE >> "$LOGFILE" 2>&1
	MERGE_DISABLE_RESTART=on
	export MERGE_DISABLE_RESTART
	"$STATICMERGE"/publicbin/win +k$WIN_CONFIG
	MERGE_DISABLE_RESTART=off 
fi


if [ "$ORIG_NETTYPE" != "winsock" ]
then
	change_info
fi

if [ "$OP" = "vnet" ]
then
	Set_MS_PROVIDER_DISABLED
	if [ "$MS_PROVIDER_DISABLED" = yes ]
	then
		# Restore the MS provider stuff
		Log "Restoring MS provider configuration"
		usrMsg STRING WIN_NETWORK_RESTORING_MS \
"Starting Windows to restore the Microsoft Network provider.\n"
		Echo "$USRMSG_STRING"
		MERGE_DISABLE_RESTART=on
		export MERGE_DISABLE_RESTART
		$DOSWINBINDIR/dos +k$WIN_CONFIG +b +r \
			$REGEDIT_UNIX j:/mwin95/msnrest.reg \
			>> "$LOGFILE" 2>&1
		"$STATICMERGE"/publicbin/win +k$WIN_CONFIG
		MERGE_DISABLE_RESTART=off 
	fi

	if [ "$FIRST_TIME" = "first_time_vnet" ]
	then
		# Run a session for the user to install networking.
		# (Can we somehow force the networking to get installed?)
		# Note:
		#   Run the session with restart disabled so that when it
		#   tries to reboot it will return here so the automatic
		#   fixups can be done by starting a new session.
		case "$WINDOWS_VERSION" in
		    *ME*)
    			REG_FILE=j:/mwin95/netadd.reg
			usrMsg STRING WIN_MERGENIC_HW_WIN_START \
"Starting Windows so you can install the Merge NIC\n\
network adapter driver and adjust any TCP/IP parameters.\n\n\
HINT: Following a possible login screen, the Add Network Device control\n\
panel will display automatically.  Select Merge/NIC under Manufacturers,\n\
then click OK.  Click Next when the resource settings are displayed.  If\n\
you do not need to adjust the TCP/IP properties, click Finish and then\n\
Yes to shut down.\n\n\
If you do need to change any of the TCP/IP->Merge/NIC properties, do NOT\n\
click Finish at this time.  Instead Right-Click My Network Places and choose\n\
Properties.\n"
			;;
		    *)
    			REG_FILE=j:/mwin95/netctpl.reg
			usrMsg STRING WIN_MERGENIC_NET_WIN_START \
"Starting Windows so you can install the Merge NIC\n\
network adapter driver and adjust any TCP/IP parameters.\n\n\
HINT: Following a possible login screen, the network control panel will\n\
display automatically.  Select Add, then Adapter, then click Add.  Select\n\
Merge/NIC under Manufacturers, then click on OK.  If you need to, you may\n\
change any TCP/IP->Merge/NIC properties at this time.  Click on OK when\n\
finished and Yes to shut down.\n"
			;;
		esac
		Echo "$USRMSG_STRING"
		EchoX "$USRMSG_STRING"
		MERGE_DISABLE_RESTART=on
		export MERGE_DISABLE_RESTART
		$DOSWINBINDIR/dos +k$WIN_CONFIG +b +r \
			$REGEDIT_UNIX $REG_FILE >> "$LOGFILE" 2>&1
		"$STATICMERGE"/publicbin/win +k$WIN_CONFIG
		MERGE_DISABLE_RESTART=off 
	else
		Replace_Vnet_Registry
		# It has been seen that in some circumstances, that after
		# going from VNET to WINSOCK and then back to VNET that
		# "ping" # wants to find icmp.dll in the windows directory
		# rather than in windows/system.  So just make a link there.
		# (One such situation was seen with win98se.)
		if [ ! -f "$FULL_WDIR/icmp.dll" ]
		then
			ln "$FULL_WSYSDIR/icmp.dll" "$FULL_WDIR/icmp.dll"
		fi
	fi

	Fixup_DNS_Configuration

	# Do final fixups including patching msafd.dll

	SAVE_PWD=`pwd`
	Log fix_windows.sh
	. "$STATICMERGE"/bin/fix_windows.sh 
	Fix_setup  >> "$LOGFILE" 2>&1
	SETUP_RETVAL=$?
	if [ $SETUP_RETVAL -eq 0 ]
	then
		Fix_links			>> "$LOGFILE" 2>&1
		# Call Fix_files with silent operation selected
		Fix_files -s			>> "$LOGFILE" 2>&1
		Fix_registry			>> "$LOGFILE" 2>&1
	fi
	cd "$SAVE_PWD"

else
	# Winsock 1 or Winsock 2
	if [ "$ORIG_NETTYPE" = vnet ]
	then
		Remove_Vnet_Registry
	fi

	if [ "$OP" = "winsock" ]
	then
		# For Winsock1 we are done.  Fix_links called by the next
		# invocation of "win" will do the fixing of the
		# winsock.dll and wsock32.dll links 
		exit 0
	fi

	# Winsock 2 case - go configure the Merge Winsock2 provider
	usrMsg STRING WIN_NETWORK_MERGE_WINSOCK2 \
"Starting Windows to configure the Merge Winsock 2 Socket provider.\n"
	Echo "$USRMSG_STRING"

	MERGE_DISABLE_RESTART=on
	export MERGE_DISABLE_RESTART
	$DOSWINBINDIR/dos +k$WIN_CONFIG +b +r $REGEDIT_UNIX \
		j:/mwin95/mws2conf.reg >> "$LOGFILE" 2>&1
	"$STATICMERGE"/publicbin/win +k$WIN_CONFIG
	MERGE_DISABLE_RESTART=off 
fi

#
# Save copies of the wsock32.dll and winsock.dll that came with
# the Win95 Winsock2 update.  We will need them if the user switches
# to winsock1 and then back to vnet or winsock2
#
if [ "$IS_WIN95" = "yes" -a -r "$WS2_DLL" ]
then
	if [ "$OP" = "vnet" -o "$OP" = "winsock2" ]
	then
		if [ ! -f $WSOCK32_WS2_UPDATE ]
		then
			Log "cp $WSOCK32_DLL $WSOCK32_WS2_UPDATE"
			cp $WSOCK32_DLL $WSOCK32_WS2_UPDATE
		fi
		if [ ! -f $WINSOCK_WS2_UPDATE ]
		then
			Log "cp $WINSOCK_DLL $WINSOCK_WS2_UPDATE"
			cp $WINSOCK_DLL $WINSOCK_WS2_UPDATE
		fi
		if [ ! -f $ICMP_WS2_UPDATE ]
		then
			Log "cp $ICMP_DLL $ICMP_WS2_UPDATE"
			cp $ICMP_DLL $ICMP_WS2_UPDATE
		fi
	fi
fi
