#!/bin/sh

# $Id: getXITE_HOME,v 1.6 1997/02/10 13:23:07 svein Exp $

# Determine XITE home directory
# INPUT:
#     $1  : Suggested return value.
#     $2  : Directory where the XITE utility script getPath is found.
#           Default: current directory
#     $3  : Non-empty if dialog is to be written to stderr. This makes it
#	          possible to have a dialog and return a value when the script
#	          is called within backquotes (``)
# OUTPUT:
#    XITE home directory
#        Default: 
#          - Environment variable XITE_HOME
#          - Or else /usr/local/xite or interactively read

# Return first non-blank word.
hom=`expr "$1" : '[ ]*\([^ ]*\)'`
dir=`expr "$2" : '[ ]*\([^ ]*\)'`
dest=`expr "$3" : '[ ]*\([^ ]*\)'`

if test -n "$hom"
then
	XITE_HOME="$hom"
fi

if test -z "$dir"
then
	GETPATH=`pwd`/getPath
else
	GETPATH="$dir/getPath"
fi

if test -n "$dest"
then
	dest="1>&2"
fi

ok=0
while test "$ok" -ne 1
do
	if test -z "$XITE_HOME"
	then
		if test -n "`echo -n`"
		then
			eval "echo 'Main XITE directory (full path): [/usr/local/xite] \c'" \
			$dest
		else
			eval "echo -n 'Main XITE directory (full path): [/usr/local/xite] '" \
			$dest
		fi

		read XITE_HOME
		if test -z "$XITE_HOME"
		then
				XITE_HOME="/usr/local/xite"
		fi
	fi
	if test -n "$XITE_HOME"
	then
		XITE_HOME=`$GETPATH $XITE_HOME`
		if test -d "$XITE_HOME"
		then
			ok=1
		fi
	fi
	if test "$ok" -ne 1
	then
		eval "echo 'Non-existent XITE home directory $XITE_HOME'" $dest
		XITE_HOME=""
	fi
done

echo "$XITE_HOME"
