#!/bin/sh
# $Id: setres.sh,v 1.3 2002/03/22 03:15:56 briank Exp $ 
# SCCSID(@(#)setres.sh	1.5) Modified 12:50:01 3/28/01
# Set the display resolution in the Windows registry.

Update_Registry()
{
	Log Update_Registry $NEW_XY

	cd "$PDIR"
	# First get the display settings from the registry.
	BAT1_FILE="${PDIR}sr_n1.bat"
	BAT2_FILE="${PDIR}sr_n2.bat"
	GET_FILE="${PDIR}sr_reg1.tmp"
	PUT_FILE="${PDIR}sr_reg2.tmp"
	rm -f "$BAT1_FILE" "$BAT2_FILE"
	rm -f "$GET_FILE" "$PUT_FILE"
	DOSText "c: " > "$BAT1_FILE"
	DOSText "cd \\\\$WINDIR "  >> "$BAT1_FILE"
	DOSText "regedit/L:\\\\$WINDIR\\\\system.dat /E \\\\sr_reg1.tmp \
\"HKEY_LOCAL_MACHINE\\\\Config\\\\0001\\\\Display\\\\Settings\"" \
		>> "$BAT1_FILE"

	Dos_Cmd +k$WIN_CONFIG +b +r sr_n1.bat >> "$LOGFILE" 2>&1
	RET_VAL=$?
	# Make sure the batch file ran and produced a file.
	if [ ! -f "$GET_FILE" ]
	then
		echo "Failed to extract the info" >> $LOGFILE
		echo "dosexec returned $RET_VAL"  >> $LOGFILE
		return
	fi

	# Make sure we got something sane.
	OLD_SETTING=`grep -i "Resolution" "$GET_FILE" | cut -d\" -f4`
	if [ "_$OLD_SETTING" = "_" ]
	then
		Log "There is no Resolution setting!"
		return
	fi
	XX=`echo $OLD_SETTING | cut -d, -f1` 
	YY=`echo $OLD_SETTING | cut -d, -f2` 
	Log Resolution is currently $XX x $YY
	if [ $XX -eq $NEW_X -a $YY -eq $NEW_Y ]
	then
		Log Nothing to change
		return
	fi
	Log Setting Resolution to $NEW_X x $NEW_Y
	cat "$GET_FILE" \
		| sed -e "s/$XX,$YY/$NEW_X,$NEW_Y/" \
		> "$PUT_FILE"
 
	# First get the display settings from the registry.
	DOSText "c: " > "$BAT2_FILE"
	DOSText "cd \\\\$WINDIR "  >> "$BAT2_FILE"
	DOSText "regedit/L:\\\\$WINDIR\\\\system.dat c:\\\\sr_reg2.tmp" \
		>> "$BAT2_FILE"
	Dos_Cmd +k$WIN_CONFIG +b +r sr_n2.bat >> "$LOGFILE" 2>&1

	if [ "x$TRACE" != "xtrace" ]
	then
		rm -f "$GET_FILE"
		rm -f "$PUT_FILE"
		rm -f "$BAT1_FILE"
		rm -f "$BAT2_FILE"
	fi
}

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

# + + + 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, SYSTEMTYPE, HOME,
	# etc settings by dotting in mrgssv.sh.
	# This also defines the Dos_Cmd and Win_Cmd functions.
	. /etc/mrgssv.sh

	DOTMERGE=$HOME/.merge
	
	LOGFILE="$DOTMERGE/setres.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 PDIR.
# NOTE: PDIR has a trailing slash.
Determine_Personal()
{
	Log Determine_Personal

	# The fuction get_drive_root is in the file unixdrive.sh.
	. "$STATICMERGE"/bin/unixdrives.sh

	# For Windows configurations, the personal drive is always "C".
	PLETTER=c
	get_drive_root $PLETTER $WIN_CONFIG
	if [ $? -ne 0 ]
	then
		Echo "Error: The personal drive directory in the $WIN_CONFIG"
		Echo "configuration is not valid."
		Echo "Cannot continue.  Exiting..."
		exit 1
	fi
	PDIR=$DRV_ROOT

	Log Personal drive letter = $PLETTER
	Log Personal drive rooted at $PDIR

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

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

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

Get_System_Variables

# Get command line parameters.
# The first parameter must be "-g" that specifies XxY.
# The second optional parameter is the configuration name.  When not specified
# the default is "win".
MYNAME=$0
Usage()
{
	Echo "Usage: $MYNAME [ -g XxY [ configuration-name ] ]"
}

case "$1" in
-g|--geometry)
	shift
	NEW_XY=$1
	shift
	NEW_X=`echo $NEW_XY | cut -dx -f1`
	NEW_Y=`echo $NEW_XY | cut -dx -f2`
	NEW_X=`expr $NEW_X + 0 2> /dev/null`
	NEW_Y=`expr $NEW_Y + 0 2> /dev/null`
	if [ "_$NEW_X" = "_" -o "_$NEW_Y" = "_" ]
	then
		echo Malformed resolution setting
		Usage
		exit 1
	fi
	;;
*)
	Usage
	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
	Echo "Error, there is no configuration named $WIN_CONFIG"
	exit 1
fi
if [ "$SESS" = "dos" ]
then
	Echo "Error, the configuration named $WIN_CONFIG is not"
	Echo "a Windows configuration."
	exit 1
fi

# Determine_Personal uses $WIN_CONFIG and sets PLETTER and PDIR.
Determine_Personal

WINDIR=`winconfig $WIN_CONFIG list windowsdir | cut -d= -f2`
Log "WINDIR=$WINDIR"
if [ "x$WINDIR" = "x" ]
then
	# Default to the "windows" subdirectory.
	WINDIR=windows
fi
FULL_WDIR="${PDIR}$WINDIR"
FULL_WSYSDIR="$FULL_WDIR/system"
if [ ! -d "$FULL_WDIR" ]
then
	Echo "Error, the Windows installation directory"
	Echo "\(specified by the configuration named $WIN_CONFIG\),"
	Echo "$FULL_WDIR"
	Echo "does not exist."
	exit 1
fi
if [ ! -d "$FULL_WSYSDIR" ]
then
	Echo "Error, the Windows system directory"
	Echo "$FULL_WSYSDIR does not exist."
	exit 1
fi

Update_Registry

exit 0
