#!/bin/sh 
#
# Parse the -dxroot option so that we can handle the case in which
# a UI installed with one DXROOT can connect to a machine with
# another DXROOT. 
#
x=`echo "$*" | grep  'dxroot'`
ARGS="$*"
if [ ! -z "$x" ]; then
    while [ ! "$*" = "" ]; do
	case $1 in
	    -dxroot)   DXROOT=$2; shift ;;
	    *)      
	    ;;
	esac
	shift
    done
fi


if [ -z "$DXROOT" ]; then
  prefix=/usr/lib
  DXROOT=$prefix/dx
fi
export DXROOT

if [ -z "$DXDATA" ]; then
  DXDATA=$DXROOT/samples/data
  if [ -d "$DXDATA" ]; then
    export DXDATA
  fi
fi

# if DXMACROS not set, default it to the usual unless dir doesn't exist
if [ -z "$DXMACROS" ]; then
  DXMACROS=$DXROOT/samples/macros
  if [ ! -d "$DXMACROS" ]; then
	DXMACROS=
  fi
fi
# if JXMACROS not set and default dir exists, add it to the DXMACROS path
if [ -z "$JXMACROS" ]; then
  JXMACROS=$DXROOT/java/server/dxmacros
  if  [ -d "$JXMACROS" ]; then
	if [ -z "$DXMACROS" ]; then
		DXMACROS=$JXMACROS
	else
		DXMACROS=$DXMACROS:$JXMACROS
	fi
  fi
fi
# if JXUSERMACROS not set and default dir exists, add it to the DXMACROS path
if [ -z "$JXUSERMACROS" ]; then
  JXUSERMACROS=$DXROOT/java/server/usermacros
  if  [ -d "$JXUSERMACROS" ]; then
	if [ -z "$DXMACROS" ]; then
		DXMACROS=$JXUSERMACROS
	else
		DXMACROS=$DXMACROS:$JXUSERMACROS
	fi
  fi
fi

if [ ! -z "$DXMACROS" ]; then
	 export DXMACROS
fi

if [ -f $DXROOT/bin/dxworker ]; then
  exec $DXROOT/bin/dxworker $ARGS 
fi 

# if we get here, no dxworker found.  try plain dx, if it isn't already us.
if [ "$0" != $DXROOT/bin/dx ]; then
  exec $DXROOT/bin/dx $ARGS 
fi

echo $DXROOT/bin/dxworker not found
exit 1
