#! /bin/sh
# $Id: win.sh,v 1.10 2003/02/05 21:05:41 lreiter Exp $ 
# SCCSID(@(#)win.sh	1.20) Modified 18:17:06 7/9/01
# This script is used to start up a Merge "Windows" process.

# + + + + 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 "$*"
}

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

# Get the STATICMERGE, SYSTEMTYPE, DEFAULTMERGE and HOME setting.
. /etc/mrgssv.sh
[ "$SYSTEMTYPE" = "linux" ] && EPT="-e"
OK_TO_LOG="no_log"
DO_SILENT=notsilent
PATH=$PATH:/usr/X11R6/bin:/usr/bin/X11

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

NULL=/dev/null
> $NULL
export NULL

MY_UID=`"$STATICMERGE"/bin/print_uid`
if [ "$MY_UID" = "0" -a ! -f /etc/lindowsos-version ]
then
	# Root is not allowed to run Windows.
	# Error message is in the standard domain ("JANUS"),
	# [the only exception is if the user is running Lindows]
	DM="LCC.PCI.UNIX.JANUS"
	usrMsg STRING NO_ROOT_WIN "The root user is not allowed to use Windows."
	"$STATICMERGE"/x_msg -e "$USRMSG_STRING"
	exit 1
fi
if [ ! -d "$HOME"/.merge ]
then
	mkdir "$HOME"/.merge
fi

LOGFILE="$HOME"/.merge/win.sh.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 


version_print()
{
	eval `grep "^VERSION=" $VARMERGE/Version`
	eval `grep "^WIN4LIN=" $VARMERGE/Version`
	eval `grep "^SERVER="  $VARMERGE/Version`
	eval `grep "^PRODUCT_STYLE="   $VARMERGE/Version`
	eval `grep "^CYCLE="   $VARMERGE/Version`
	eval `grep "^REL_DAY=" $VARMERGE/Version`
	if [ "_$PRODUCT_STYLE" = "_server" ]
	then
		NUMBER="$SERVER"
	elif [ "_$PRODUCT_STYLE" = "_desktop" ]
	then
		NUMBER="$WIN4LIN"
	else
		NUMBER=
	fi
	Echo $MERGENAME $NUMBER '('${VERSION}${CYCLE}')' $REL_DAY
	if [ "$SYSTEMTYPE" = "linux" ]
	then
        	XFREE86_VER=not
		XINFO=`xdpyinfo | grep "vendor string"`
		Text $XINFO | grep  XFree86 > /dev/null
		if [ $? -eq 0 ]
		then
        		XINFO=`xdpyinfo | grep "vendor release number: "`
        		XFREE86_VER=`Text $XINFO \
			 	| while read A B C D E ; do Text $D;break;done`
		fi                                       
		case $XFREE86_VER in
		33*)
			Echo "XFree86 3.3.x"
			;;
		3*)
			Echo "XFree86 3.x"
			;;
		4001*)
			Echo "XFree86 4.0.1"
			;;
		4002*)
			Echo "XFree86 4.0.2"
			;;
		4*)
			Echo "XFree86 4.x"
			;;
		*)
			DM="LCC.MERGE.UNIX.WIN"
			usrMsg STRING WIN_NOT_2 "Not XFree86 3.x or 4.x" "XFree86 3.x" 4.x
			Echo $USRMSG_STRING
			;;
		esac
	fi
}

syntax_print()
{
	DM="LCC.MERGE.UNIX.WIN"
	usrMsg ECHO HELP_WIN_EXEC "Usage: win [flags]\n\
-g XxY Make the Window area be sized X by Y (e.g. '-g 1000x750').\n\
+kc    Use configuration 'c'. +ku:c means use user u's configuration c.\n\
-v     Print out the $MERGENAME version.\n" win
}

if [ "_$1" = "_--fwin" ]
then
	shift
	USING_FWIN=using_fwin
else
	USING_FWIN=not_fwin
fi
if [ "_$1" = "_--nofix" ]
then
	shift
	FIX_OK=no_fix
else
	FIX_OK=fix_ok
fi

# Scan the command line to see if a specific configuration
# was specified with the "+k" option.
USE_CONFIG=win
ERR_CODE=0
for i in $*
do
	case "$i" in
	+k*)	;;
	-help|--help|--h|-h|+h)
		syntax_print
		exit 0
		;;
	-v)
		version_print
		exit 0
		;;
	*)
		continue
		;;
	esac
	if [ "$i" = "+k" ]
	then
		# Bad use of the k option.
		# Error message is in the standard domain ("JANUS"),
		DM="LCC.PCI.UNIX.JANUS"
		usrMsg STRING BAD_OPT_USAGE \
	"BAD_OPT_USAGE: win: ERROR: The '+k' option is not used correctly." \
			win +k
		"$STATICMERGE"/x_msg -e "$USRMSG_STRING"
		exit 1
	fi
	USE_CONFIG=`Text $i | cut -c  3-`
	continue
done
Log USE_CONFIG=$USE_CONFIG
OPT_1=$1

check_config()
{
	# Note:
	#   If the "win" configuration does already exist,
	#   AND the "dos" configuration does already exist
	#   then the winconfig command will silently and
	#   automatically create the default dos and win configurations.
	winconfig $USE_CONFIG list version > /dev/null 2>&1
	STATE=$?
	if [ $STATE -eq 3 ]
	then
		# Configuration does not exist.
		Log "Configuration $USE_CONFIG does not exist"
		# No such specified config.
		DM="LCC.PCI.UNIX.JANUS"
		usrMsg STRING CONFIG_NE \
	"CONFIG_NE: win: ERROR: The configuration $USE_CONFIG does not exist." \
			win "$USE_CONFIG"
		"$STATICMERGE"/x_msg -e "$USRMSG_STRING"
		exit 1
	fi
	if [ $STATE -eq 4 ]
	then
		# Unreadable configuration.
		Log "Configuration $USE_CONFIG is not readable"
		DM="LCC.PCI.UNIX.JANUS"
		usrMsg STRING CONFIG_NR \
	"CONFIG_NR: win: ERROR: The configuration $USE_CONFIG is not readable."\
			win "$USE_CONFIG"
		"$STATICMERGE"/x_msg -e "$USRMSG_STRING"
		exit 1
	fi
}
# check_config only returns when the configuration is readable.
check_config

get_config_info()
{
	C_DIR=
	# The fuction get_drive_root is in the file unixdrive.sh.
	. $STATICMERGE/bin/unixdrives.sh

	get_drive_root c $USE_CONFIG
	C_DIR=$DRV_ROOT
	if [ ! -d "$C_DIR" ]
	then
		Log $C_DIR does not exist
		C_DIR=
		return
	fi
	WINDIR=`winconfig $USE_CONFIG list windowsdir | cut -d= -f2`
	Log "WINDIR=$WINDIR"
	if [ "x$WINDIR" = "x" ]
	then
		# Default to the "windows" subdirectory.
		WINDIR=windows
	fi
	FULL_WDIR="${C_DIR}$WINDIR"
	FULL_WSYSDIR="$FULL_WDIR/system"
	if [ ! -d "$FULL_WSYSDIR" ]
	then
		# Windows not installed, so don't do any fixups.
		C_DIR=
	fi
}
get_config_info
Log C_DIR=$C_DIR

ERR_CODE=0
if [ "$SYSTEMTYPE" = "linux" ]
then
	# Do this test first because it give a finer grain
	# error code for different kinds of invalid licenses.
	# This returns 0 for ok, 1 thru 14 for failure.
	# Can also return 99 if very unlikely problem with temp file.
	
	"$STATICMERGE"/bin/chk_lic_file >> "$LOGFILE"
	ERR_CODE=$?

	# on Lindows, we have to set XAUTHORITY here otherwise we can't start
	# X11 apps from scripts (such as what we are attempting to do)
	if [ -f /etc/lindowsos-version ]; then
		MY_UID=`"$STATICMERGE"/bin/print_uid`
		if [ "$MY_UID" = "0" ]; then
			XAUTHORITY=/root/.Xauthority
		else
			XAUTHORITY=$HOME/.Xauthority
		fi
		export XAUTHORITY
	fi
fi
# Make sure things are ok OK for running Windows!
# On Linux, this will check the license,
# and on all systems it will make sure a Windows session
# will be allowed to run.  
# For SCO systems win_probes returns 0, 11, 18-22.
if [ $ERR_CODE -eq 0 ]
then
	"$STATICMERGE"/bin/win_probe -x $USE_CONFIG >> "$LOGFILE"
	ERR_CODE=$?
fi
case $ERR_CODE in
0)
	# Ok.
	;;
1|2|3|4|5|6|7|8|9|14|15)
	# Linux only.
	# The license info is invalid.
	DM="LCC.PCI.UNIX.JANUS"
	if [ "_$PRODUCT_STYLE" = "_server" ]
	then
		ERR_MSG="LICENSE_INVALID_S"
		BUYURL=
	else
		ERR_MSG="LICENSE_INVALID"
		BUYURL=`$STATICMERGE/bin/getbuyurl`
	fi
	usrMsg STRING $ERR_MSG "The license is invalid." $BUYURL
	"$STATICMERGE"/x_msg -e "$USRMSG_STRING"
	exit 1
	;;
11)
	# $VARMERGE is invalid.  This should never happen.
	DM="LCC.MERGE.UNIX.WIN"
	usrMsg STRING WIN_UNEXPECTED_ERROR "Unexpected error."
	USRMSG_STRING="$USRMSG_STRING 11"
	"$STATICMERGE"/x_msg -e "$USRMSG_STRING"
	exit 1
	;;
12)
	# Linux only.
	# There is no license file.
	DM="LCC.PCI.UNIX.JANUS"
	if [ "_$PRODUCT_STYLE" = "_server" ]
	then
		ERR_MSG="LICENSE_FILE_MISSING_S"
		BUYURL=
	else
		ERR_MSG="LICENSE_FILE_MISSING"
		BUYURL=`$STATICMERGE/bin/getbuyurl`
	fi
	usrMsg STRING $ERR_MSG "License is not installed" $BUYURL
	"$STATICMERGE"/x_msg -e "$USRMSG_STRING"
	exit 1
	;;
13)
	# Linux only.
	DM="LCC.PCI.UNIX.JANUS"
	usrMsg STRING LICENSE_FILE_NR \
		"The license is not readable - $VARMERGE/install/license.lic" \
		"$VARMEGE"/install/license.lic
	"$STATICMERGE"/x_msg -e "$USRMSG_STRING"
	exit 1
	;;
16)
	# Linux only.
	DM="LCC.PCI.UNIX.JANUS"
	if [ "_$PRODUCT_STYLE" = "_server" ]
	then
		ERR_MSG="LICENSE_EXPIRED_S"
		BUYURL=
	else
		ERR_MSG="LICENSE_EXPIRED"
		BUYURL=`$STATICMERGE/bin/getbuyurl`
	fi
	usrMsg STRING $ERR_MSG "The license is expired." $BUYURL
	"$STATICMERGE"/x_msg -e "$USRMSG_STRING"
	exit 1
	;;
17)
	# Linux only.
	DM="LCC.MERGE.UNIX.WIN"
	usrMsg STRING SESSION_TOOMANY "Licensed session count exceeded."
		"$STATICMERGE"/x_msg -e "$USRMSG_STRING"
	"$STATICMERGE"/x_msg -e "$USRMSG_STRING"
	exit 1
	;;
18)
	DM="LCC.MERGE.UNIX.WIN"
	usrMsg STRING SESSION_ALREADY "Cannot start another session from the same location."
	"$STATICMERGE"/x_msg -e "$USRMSG_STRING"
	exit 1
	;;
19)
	DM="LCC.MERGE.UNIX.WIN"
	usrMsg STRING SESSION_LOCK_FAILS "Session lock check failure. C drive located on remote filesystem?"
	"$STATICMERGE"/x_msg -e "$USRMSG_STRING"
	exit 1
	;;
20)
	DM="LCC.MERGE.UNIX.WIN"
	usrMsg STRING SESSION_LOCK_PROBLEM "Session lock check unexpected failure."
	"$STATICMERGE"/x_msg -e "$USRMSG_STRING"
	exit 1
	;;
21)	
	# Merge driver not loaded or open limit exceeded.
	DM="LCC.PCI.UNIX.JANUS"
	if [ "$SYSTEMTYPE" = "sco32" ]
	then
		usrMsg STRING MERGE_NA "$MERGENAME driver not installed." win
	else
		usrMsg STRING MERGE_NL \
			"$MERGENAME driver not loaded or open limit exceeded." \
			win
	fi
	"$STATICMERGE"/x_msg -e "$USRMSG_STRING"
	exit 1
	;;
22)
	# Something failed.  Probably a messed up configuration
	# or our of resources.  So just run dosexec so it can
	# print out a more intelligent error message.
	exec $STATICMERGE/publicbin/dosexec -W $*
	exit $?
	;;
23)
	# Invalid bump license
	# Linux only.
	DM="LCC.MERGE.UNIX.WIN"
	usrMsg STRING LICENSE_BUMP_INVALID_S \
		"Invalid additional session license." \
		"$STATICMERGE"/x_msg -e "$USRMSG_STRING"
	"$STATICMERGE"/x_msg -e "$USRMSG_STRING"
	exit 1
	;;
24)
	# Invalid bump license
	# Linux only.
	DM="LCC.MERGE.UNIX.WIN"
	usrMsg STRING LICENSE_BUMP_DUP_S \
		"Duplicate additional session license." \
		"$STATICMERGE"/x_msg -e "$USRMSG_STRING"
	"$STATICMERGE"/x_msg -e "$USRMSG_STRING"
	exit 1
	;;
*)
	# Don't know how this can happen.
	DM="LCC.MERGE.UNIX.WIN"
	usrMsg STRING WIN_UNEXPECTED_ERROR "Unexpected error."
	USRMSG_STRING="$USRMSG_STRING $ERR_CODE"
	"$STATICMERGE"/x_msg -e "$USRMSG_STRING"
	exit 1
	;;
esac


FORCE_XY=
if [ "_$1" = "_-g" -o "_$1" = "--geometry" ]
then
	shift
	FORCE_XY=$1
	[ "_$FORCE_XY" != "_" ] && shift
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_xy_override()
{
	if [ "_$C_DIR" = "_" ]
	then
		# Windows not fully installed, so cannot do 
		# the resize stuff.
		return
	fi
	if [ "_$FORCE_XY" != "_" ]
	then
		# If the user has specified a size smaller than the
		# minimum dimensions supported by xcrt (500x375) then
		# override the user specified values with the minimum
		# values allowed.
		XX=`echo $FORCE_XY | cut -dx -f1`
		YY=`echo $FORCE_XY | cut -dx -f2`
		if [ $XX -lt 500 -o $YY -lt 375 ] ; then
			FORCE_XY=500x375
		fi
		return
	fi
	DISPLAY_XY=`xdpyinfo | grep "dimensions:"  \
			| while read A B C ; do Text $B ; break ; done`
	if [ "$USING_FWIN" = "using_fwin" ]
	then
		Log fwin
		# Doing fwin, so check for the fwin auto resizing option.
		WMODE=`/usr/bin/winconfig $USE_CONFIG list windowresizefwin`
		Log $WMODE
		if [ "_$WMODE" = "_windowresizefwin=true" ]
		then
			FORCE_XY=$DISPLAY_XY
		fi		
		Log FORCE_XY=$FORCE_XY
		return
	fi
	Log not fwin
	# Not doing fwin, so check for the non-fwin auto resizing option
	WMODE=`/usr/bin/winconfig $USE_CONFIG list windowresizewin`
	Log $WMODE
	if [ "_$WMODE" = "_windowresizewin=false" ]
	then
		return
	fi
	XX=`Text $DISPLAY_XY | cut -dx -f1`
	YY=`Text $DISPLAY_XY | cut -dx -f2`
	# Ok, doing resize, so find out which kind.
	WMODE=`/usr/bin/winconfig $USE_CONFIG list windowsizeauto`
	Log $WMODE
	if [ "_$WMODE" = "_windowsizeauto=false" ]
	then
		NEW_X=`/usr/bin/winconfig "$USE_CONFIG" list windowswidth \
			| cut -d= -f 2`
		NEW_Y=`/usr/bin/winconfig "$USE_CONFIG" list windowsheight\
			| cut -d= -f 2`
		# Make sure not bigger that the actual display.
		[ $NEW_X -gt $XX ] && NEW_X=$XX 
		[ $NEW_Y -gt $YY ] && NEW_Y=$YY 
		FORCE_XY=${NEW_X}x${NEW_Y}
		Log FORCE_XY=$FORCE_XY
		return
	fi
	# We are doing the "automatic" resizeing.
	# This is defined the next smaller standard screen size,
	# but no smaller than 600x400.
	# Standard screen sizes:
	#     1920x1440, 1920x1200, 1600x1200, 1280x1024, 1024x768,
	#     800x600, 640x480
	# 
	Smaller_x `Text $DISPLAY_XY | cut -dx -f1`
	XX=$NEW_X
	Smaller_y `Text $DISPLAY_XY | cut -dx -f2`
	YY=$NEW_Y
	FORCE_XY=${XX}x${YY}
	Log FORCE_XY=$FORCE_XY
	return
}
set_xy_override


# Only do the fixups when there is no possiblility that another Windows
# session is running from the same location.  Also do not try the fixups
# when the C drive directory does not exist.
if [ "x$C_DIR" != "x" -a ! -f "$C_DIR"/.merge_win95_lock ]
then
	if [ "$FIX_OK" = "fix_ok" ]
	then
		# The functions in fix_windows.sh expect that:
		#   WIN_CONFIG is set to the configuration name
		#   C_DIR be set to the full path to the C drive root.
		#	(This must be determined using unixdrive.sh)
		# The functions set and use:
		#   WIN_DIR FULL_WDIR FULL_WSYSDIR
		date +%H:%M:%S				>> "$LOGFILE"
		SAVE_PWD=`pwd`
		Log fix_windows.sh
		WIN_CONFIG=$USE_CONFIG
		. "$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"
	fi
	if [ "_$FORCE_XY" != "_" ]
	then
		date +%H:%M:%S				>> "$LOGFILE"
		Log "$STATICMERGE"/bin/setres -g $FORCE_XY $USE_CONFIG
		"$STATICMERGE"/bin/setres -g $FORCE_XY $USE_CONFIG
	fi
	date +%H:%M:%S					>> "$LOGFILE"
fi

exec $STATICMERGE/publicbin/dosexec -W $*
