#!/bin/sh


nargs=$#
args="$*"

# Add to the path variable named by $1 the component $2.  $3 must be
# "append" or "prepend" to indicate where the component is added.

# error message function
err () {
	echo "`basename $0`: $@" 1>&2
	exit 1
}

addpath () {
    eval value=\"\$$1\"
    case "$value" in
	*:$2:*|*:$2|$2:*|$2)
	    result="$value"
	    ;;
	"")
	    result="$2"
	    ;;
	*)
	    case "$3" in
		p*)
		    result="$2:${value}"
		    ;;
		*)
		    result="${value}:$2"
		    ;;
	    esac
    esac
    eval $1=$result
    unset result value
}

# convenience routine which appends a string to a path.
append () {
    addpath "$1" "$2" append
}

# convenience routine which prepends a string to a path.
prepend () {
    addpath "$1" "$2" prepend
}


case "X${ARBHOME}" in
  X)
	if [ -x /usr/arb/bin/arb ];then
		ARB=/usr/arb/bin/arb
	else
	      case "$0" in
		/*)
		       ARB="$0";;
		*)	
		     if ARB=`which $0`; then
		       echo $ARB
		     else
		       ARB=`pwd`/$0
		     fi;;
	      esac
	fi
	
	ARBBIN=`dirname $ARB`
	export ARBBIN
	ARBHOME=`dirname $ARBBIN`
	export ARBHOME
      echo "Environment Variable ARBHOME not set"
      echo "	set to $ARBHOME";;
esac

prepend PATH			$ARBHOME/bin
prepend LD_LIBRARY_PATH		$ARBHOME/lib
append  LD_LIBRARY_PATH		/usr/dt/lib
append  LD_LIBRARY_PATH		/usr/openwin/lib
prepend SHLIB_PATH		$ARBHOME/lib

# environment variables that this shell script sets/changes:
export LD_LIBRARY_PATH MANPATH PATH ARBHOME SHLIB_PATH

# global envs:

export PWD HOME USER

if [ -x $ARBHOME/bin/ghostview ] ; then
  GS_LIB="$ARBHOME/DEPOT/ghostscript"
  export GS_LIB
fi

if [ "X$ARBMACROHOME" = "X" ] ; then
  ARBMACROHOME="$ARBHOME/lib/macros"
  export ARBMACROHOME
fi

ARB_PID="$$"; export ARB_PID

if [ ! -d ${HOME}/.arb_prop ] ; then
  echo 'Directory $HOME/.arb_prop not found creating ...'
  mkdir ${HOME}/.arb_prop
fi

if [ "X$GDE_HELP_DIR" = "X" ] ; then
  GDE_HELP_DIR="$ARBHOME/GDEHELP"
  export GDE_HELP_DIR
fi

# Default Texteditor/CMDTOOL used by ARB
. $ARBHOME/lib/config.`uname`

# save LD_LIBRARY_PATH
ARB_LIBRARY_PATH="${LD_LIBRARY_PATH}"
export ARB_LIBRARY_PATH

echo "Program ARB will be startet in the background, please wait"
(arb_ntree $args;arb_clean >/dev/null 2>&1;echo ARB done) &