#!/bin/sh
#
#	regedit.sh:
#		Provides a UNIX frontend to the DOS REGEDIT command, with
#		limited functionality
#
#	Copyright (C) 2002 NeTraverse, Inc.
#
#

#
#	usage:
#		regedit.sh -e [<regpath>]
#		regedit.sh -d <regpath>
#		regedit.sh <importfile>
#
#		-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 -e [<startpath>]"
	echo "  $0 -d <regpath>"
	echo "  $0 <importfile>"
	echo ""
	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

# make sure that Windows is installed for this user
if [ ! -d $HOME/win -o ! -d $HOME/.merge ]
then
	echo "$0: personal Windows installation not found."
	exit 1
fi

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

# check usage
ARGC=$#
if [ "$1" = "-e" ]
then
	if [ $ARGC -lt 3 ]
	then
		MODE=e
		if [ $2 ]
		then
			STARTKEY=$2
		fi
	else
		usage $0
	fi
elif [ "$1" = "-d" ]
then
	if [ $ARGC = 2 ]
	then
		MODE=d
		REGPATH=$2
	else
		usage $0
	fi
elif [ $ARGC = 1 ]
then
	if [ "$1" = "-" ]
	then
		MODE=i
		IMPORTFILE=$HOME/win/tmp/re$$.tmp
		IFILETMP=1
		cat |$UNIX2DOS >$IMPORTFILE
		
	else
		IMPORTFILE=$1
	fi
else
	usage $0
fi

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


