#!/bin/sh
# $Id: regedit.sh,v 1.10 2004/10/01 16:43:03 lreiter Exp $
#
#	regedit.sh:
#		Provides a UNIX frontend to the DOS REGEDIT command, with
#		limited functionality
#
#	Copyright (C) 1984-2004 NeTraverse, Inc.
#
#

#
#	usage:
#		regedit.sh [-k <config>] -e [<regpath>]
#		regedit.sh [-k <config>] -d <regpath>
#		regedit.sh [-k <config>] <importfile>
#
#		-k		specifies configuration, default is win
#		-e		extracts all or partial registry to stdout
#		-d		deletes a registry key, specified in <regpath>
#		<importfile>	file to import, or - for stdin
#
usage()
{
	echo ""
	echo "usage:"
	echo "  $0 -k [<config>] -e [<startpath>]"
	echo "  $0 -k [<config>] -d <regpath>"
	echo "  $0 -k [<config>] <importfile>"
	echo ""
	echo "  -k            specifies configuration, default is win"
	echo "  -e            extracts registry to stdout"
	echo "  <startpath>   optional starting registry key to export from"
	echo "                (defaults to exporting the entire registry)"
	echo "  -d            deletes key specified by <regpath> from registry"
	echo "  <importfile>  file containing data to import into registry, or - for stdin"
	echo ""
	exit 1
}

# do not allow to run as root
USERID=`id -u`
if [ "$USERID" = "0" ]
then
	echo "$0: cannot be run as root."
	exit 1
fi

# source in variables and set up some tools
. /etc/mrgssv.sh
. $STATICMERGE/bin/unixdrives.sh
DOS2UNIX=$STATICMERGE/publicbin/dos2unix
UNIX2DOS=$STATICMERGE/publicbin/unix2dos

# check usage
PROGNAME=`basename $0`
if [ "$1" = "-k" ]
then
	shift
	if [ "$1" != "" ]
	then
		USECONFIG=$1
		shift
	else
		usage $PROGNAME
	fi
else
	USECONFIG="win"
fi

# use a POSIX locale to avoid problems with unix2dos and registry imports in 
# unsupported locales
LANG="C"; export LANG
eval `locale`

# get the C drive for the specified configuration
get_drive_root c $USECONFIG
C_DIR=$DRV_ROOT
if [ ! -d "$C_DIR" ]
then
	echo "$PROGNAME: personal Windows installation not found."
	exit 1
fi

# check the rest of the usage
ARGC=$#
if [ "$1" = "-e" ]
then
	if [ $ARGC -lt 3 ]
	then
		MODE=e
		if [ "$2" != "" ]
		then
			STARTKEY="$2"
		fi
	else
		usage $PROGNAME
	fi
elif [ "$1" = "-d" ]
then
	if [ $ARGC = 2 ]
	then
		MODE=d
		REGPATH="$2"
	else
		usage $PROGNAME
	fi
elif [ $ARGC = 1 ]
then
	if [ "$1" = "-" ]
	then
		MODE=i
		IMPORTFILE=$C_DIR/tmp/re$$.tmp
		IFILETMP=1
		cat |$UNIX2DOS >$IMPORTFILE
		
	else
		IMPORTFILE=$1
	fi
else
	usage $PROGNAME
fi

# go to work
case $MODE in
e)
	OFILE=c:\\tmp\\re$$.tmp
	UFILE=$C_DIR/tmp/re$$.tmp
	if [ "$STARTKEY" != "" ]; then
		STARTSTR="\"$STARTKEY\""
	else
		STARTSTR=""
	fi
	$STATICMERGE/publicbin/dos -t +w +b +k$USECONFIG REGEDIT /E $OFILE $STARTSTR >/dev/null
	if [ -r $UFILE ]
	then
		$DOS2UNIX $UFILE
		rm $UFILE
		exit 0
	else
		exit 1
	fi
	;;
d)
	$STATICMERGE/publicbin/dos -t +w +b +k$USECONFIG REGEDIT /D \"$REGPATH\" >/dev/null
	exit 0
	;;
i)
	IFILE=c:\\tmp\\re$$.tmp
	if [ $IFILETMP ]
	then
		UFILE=$IMPORTFILE
	else
		UFILE=$C_DIR/tmp/re$$.tmp
		$UNIXTODOS <$IMPORTFILE >$UFILE
	fi
	$STATICMERGE/publicbin/dos -t +w +b +k$USECONFIG REGEDIT $IFILE >/dev/null
	if [ $IFILETMP ]
	then
		rm $UFILE
	fi
	exit 0
	;;
*)
	;;
esac


