:	'(c) Copyright	Empress Software Inc.	1983, 2000'

#*************************************************************
#   This script is used to update Win32 Registry key.
#	
#   It updates Registry key's name and stores previous key's
#   name value in $TEST_DIR/$REG_RESTORE_FILE 
#   for future restoring by $EMPRESSPATH/common/sys_bin/win32/regCleanUp.
#*************************************************************
USAGE="*** Usage ***  ./regSetKey registry_key_name value test_dir [-s silent]"


SHOW_USAGE=false
case $# in
 4)	;;
 5)	test "$5" != "-s" && SHOW_USAGE=true ;;
 *)	SHOW_USAGE=true ;;
esac

if test "$SHOW_USAGE" = "true" 
then
	echo "$USAGE"
	exit 1
fi


. $EMPRESSPATH/common/sys_bin/win32/regConfig || exit 1

KEY=$1
NAME=$2
VALUE=$3
TEST_DIR="$4"
test "$5" = "-s" && SILENT=true
OLD_VALUE=


if test ! -d "$TEST_DIR" 
then
	echo "Can't find $TEST_DIR directory. Exit."
	exit 1
fi
cd "$TEST_DIR"


#--------------------------------------------------------
# It's possible that some escape sequences 
# (\a \b \f \n \r \t \v) will present in the KEY name. 
# To get right interpretation of the KEY, just double all 
# back slashes. 
# Fixing query key...
#--------------------------------------------------------
echo "QUERY_KEY=\""$1"\"" | sed -e 's/\\/\\\\/g' > tmp
echo 'export QUERY_KEY="$QUERY_KEY"' >> tmp
. ./tmp
rm -f tmp


# Query $NAME
UPDATED_NAME="$NAME"
test "$NAME" = "(Default)" && UPDATED_NAME=""
#############################################################################
# Here we meet MKS bug: Quering (Default) key every time returns 
# NO_NAME_FOUND string, even this (Default) key exist. That causes the 
# (Default) key will be removed (not restored) by future resRestoreKey call.
#############################################################################

OLD_VALUE=`registry -p -r -k "$QUERY_KEY" -n "$UPDATED_NAME"` > nul 2>&1 || \
	OLD_VALUE="$NO_NAME_FOUND"


# Store old name and its value
if test ! -f "$REG_RESTORE_FILE"
then 
	test "$SILENT" = "true" && echo "   updating keys in Silent mode [-s]..."

	cat > $REG_RESTORE_FILE << EOM
:
#-------------------------------------------------------------
#	This is temporary file for test session only and 
#	generated by \$EMPRESSPATH/common/sys_bin/win32/regSetKey script.
#
#   It's used to restore Registry key's names after testing. 
#   This file should be launched 
#   by \$EMPRESSPATH/common/sys_bin/win32/regCleanUp script.
#
#   Note:	Do not edit this file!!!
#-------------------------------------------------------------

X=\`uname -a\`

case "\$X" in
 Windows*)	;;
 *)	echo "Opps! \$0 for Win32 using only"
	exit 1	;;
esac

case \$EMPRESSPATH in
 "")	echo "EMPRESSPATH not set."
	exit 1
	;;
esac
	
RETCODE=0
EOM
fi

#set silent mode for future KEY restoring.
SM=
test "$SILENT" = "true" && SM="-s"
CMD_LINE=". \$EMPRESSPATH/common/sys_bin/win32/regRestoreKey \"$KEY\" \"$NAME\" \"$OLD_VALUE\" "$SM" || RETCODE=1"
#--------------------------------------------------------
# It's possible that some escape sequences 
# (\a \b \f \n \r \t \v) will present in the KEY name. 
# To get right interpretation of the KEY, just double all 
# back slashes. 
# Fixing restoring key...
#--------------------------------------------------------
echo "$CMD_LINE" | sed -e 's/\\/\\\\/g' >> $REG_RESTORE_FILE

sed '/return/d' < $REG_RESTORE_FILE > reg_tmp$$
echo 'return $RETCODE' >> reg_tmp$$
mv reg_tmp$$ $REG_RESTORE_FILE



# Set new $VALUE for $NAME 
test "$SILENT" = "true" || echo "   setting $NAME..."
registry -s -k "$KEY" -n "$UPDATED_NAME" -v "$VALUE"

