#!/bin/sh
# Startup wrapper for all WorkShop Visual commands
#
# Copyright Imperial Software Technology Ltd 1994,1995. All Rights Reserved.
# Copyright (c) 1995-1996 Sun Microsystems, Inc. All Rights Reserved.
#
# "@(#)RELEASE VERSION WorkShop Visual 2.0 10/09/96"
# "@(#)SERIAL NUMBER VS-21-100996"

basename=`basename $0`
relpath=
xtra_args=

# Some Sun OpenWindows specific stuff first..
# if OPENWINHOME is not set but /usr/openwin exists, set it
if [ -z "$OPENWINHOME" -a -d /usr/openwin ]; then
	OPENWINHOME=/usr/openwin export OPENWINHOME
fi
# End of Sun OpenWindows specific stuff

# The following attempts to determine VISUROOT automatically and compares it
# with any setting inherited from the environment. Warn if the two differ..
# It should work even in the presence of (cursed) symbolic links
#
# First, determine path to apparent executable..
case $0 in 
	*/*)	path=`dirname $0`
		relpath=$path
		;;
	*)	SAVEIFS="$IFS"
		IFS="$IFS:"
		for path in $PATH; do
			if [ -x $path/$0 ]; then
				break
			fi
			# if we ever fall through it must be the current dir..
			path=.
		done
		IFS="$SAVEIFS"
		;;
esac
# Next, follow sym links, if any..
lsout=`ls -l $path/$basename`
while test `expr "$lsout" : "l"` = 1; do
	lpath=`echo $lsout | sed 's/.* //'`
	basename=`basename $lpath`
	case $lpath in
		/*)	path=`dirname $lpath` ;;
		*)	path=$path/`dirname $lpath` ;;
	esac
	lsout=`ls -l $path/$basename`
done
# Expand any sym links to get real path..
realdir=`cd $path; cd ..; pwd | sed -e 's.^/tmp_mnt/./.'`
# Now, compare with VISUROOT if set..
if [ ! -z "$VISUROOT" ]; then
	# Expand any sym links as before (forcing sh to re-eval path)..
	setdir=`cd $VISUROOT/bin/..; pwd | sed -e 's.^/tmp_mnt/./.'`
	if [ "$realdir" != "$setdir" ]; then
		echo "$0: warning: \$VISUROOT set to $VISUROOT" >&2
	fi
else
	VISUROOT=$realdir; export VISUROOT
fi

# if we still haven't determined it, bomb politely...
if [ -z "$VISUROOT" ]; then
	echo "$0: Can't determine location of command so can't set \$VISUROOT appropriately." >&2
	echo "You must set the VISUROOT environment variable to the path of the visu" >&2
	echo "release directory" >&2
	exit 1
fi

# Check LANG, if set, is supported
if [ ! -z "$LANG" ]; then
	if [ ! -f $VISUROOT/lib/locale/$LANG/app-defaults/visu ]; then
		echo "$0: unsupported LANG setting ($LANG) coerced to C" >&2
		LANG=C export LANG
	fi
fi

# Check LC_CTYPE and coerce LANG if necessary
if [ ! -z "$LC_CTYPE" ]; then
	if [ "$LANG" != "$LC_CTYPE" -a -f $VISUROOT/lib/locale/$LC_CTYPE/app-defaults/visu ]; then
		echo "$0: LANG setting coerced to LC_CTYPE ($LC_CTYPE)" >&2
		LANG=$LC_CTYPE export LANG
	fi
fi
# Determine if use wants particular user widgets integrated..
# Defaults..
opt=${VISUROOT}
# Coerce basename to lower case if not already..
basename=`echo $basename | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz`
fname=lib/$basename
if [ ! -z "$USER_WIDGETS" ]; then
	if [ "$USER_WIDGETS" != "default" ]; then
		# explicit setting.. (fail silently and fall back to default)
		case "$USER_WIDGETS" in

			/*)	;;
			*)	USER_WIDGETS=${VISUROOT}/user_widgets/${USER_WIDGETS} ;;

		esac
		if [ -f ${USER_WIDGETS}/$basename.bin ]; then
			opt=${USER_WIDGETS}
			fname=$basename.bin
		fi
	fi
else
	# Check to see if site default available in local..
	if [ -f ${VISUROOT}/user_widgets/local/$basename.bin ]; then
		opt=${VISUROOT}/user_widgets/local
		fname=$basename.bin
	fi
fi
# Warning: these few lines *may* get edited as part of the initial installation config process
# A null setting indicates 'use system default'
XDNLS=
XDKEY=
XDLIC=
# end of auto config stuff

# Avoid clobbering existing XFILESEARCHPATH...
if [ -z "$XFILESEARCHPATH" ]; then
	XFILESEARCHPATH=$opt/lib/locale/%L/%T${DTUSERSESSION+/CDE}/%N%S:$opt/lib/locale/%L/%T/%N%S:$opt/%T${DTUSERSESSION+/CDE}/%N%S:$opt/%T/%N%S:/usr/lib/X11/%T/%N'%C'%S:/usr/lib/X11/%T/%N%S:$opt/lib/locale/C/%T${DTUSERSESSION+/CDE}/%N%S:$opt/lib/locale/C/%T/%N%S
else
	XFILESEARCHPATH=$opt/lib/locale/%L/%T${DTUSERSESSION+/CDE}/%N%S:$opt/lib/locale/%L/%T/%N%S:$opt/%T${DTUSERSESSION+/CDE}/%N%S:$opt/%T/%N%S:$opt/lib/locale/C/%T${DTUSERSESSION+/CDE}/%N%S:$opt/lib/locale/C/%T/%N%S:${XFILESEARCHPATH}
fi
if [ ! -z "$SW_VISU_XFILESEARCHPATH" ]; then
	XFILESEARCHPATH=${SW_VISU_XFILESEARCHPATH}:${XFILESEARCHPATH}
fi
if [ ! -z "$OPENWINHOME" ]; then
	XFILESEARCHPATH=${XFILESEARCHPATH}:${OPENWINHOME}/lib/%T/%N%S
fi
export XFILESEARCHPATH
# Ditto for XBMLANGPATH...
if [ -z "$XBMLANGPATH" ]; then
	XBMLANGPATH=$opt/${SW_VISU_ICONS-bitmaps}/%B:${VISUROOT}/${SW_VISU_ICONS-bitmaps}/%B:${VISUROOT}/bitmaps/%B:%B:$HOME/bitmaps/%N/%B:$HOME/bitmaps/%B:/usr/lib/X11/bitmaps/%N/%B:/usr/lib/X11/bitmaps/%B:/usr/include/X11/bitmaps/%B
else
	XBMLANGPATH=$opt/${SW_VISU_ICONS-bitmaps}/%B:${VISUROOT}/${SW_VISU_ICONS-bitmaps}/%B:${VISUROOT}/bitmaps/%B:${XBMLANGPATH}
fi
if [ ! -z "$SW_VISU_XBMLANGPATH" ] ; then
	XBMLANGPATH=${SW_VISU_XBMLANGPATH}:${XBMLANGPATH}
fi
export XBMLANGPATH
# Use configured XKeysymDB file or allow user to override...
if [ -z "$XKEYSYMDB" -a ! -z "$XDKEY" ]; then
	XKEYSYMDB=${XDKEY}; export XKEYSYMDB
fi
# Fix up licensing..
if [ ! -z "$XDLIC" ]; then
	if [ -z "$LM_LICENSE_FILE" ]; then
		LM_LICENSE_FILE=${XDLIC}; export LM_LICENSE_FILE
	else
		LM_LICENSE_FILE=${XDLIC}:$LM_LICENSE_FILE; export LM_LICENSE_FILE
	fi
fi
# Fix NLSPATH for ref to Motif library message catalog..
NLSPATH=${NLSPATH:+${NLSPATH}:}:/usr/dt/lib/nls/msg/%L/%N.cat:${VISUROOT}/../dt/lib/nls/msg/%L/%N.cat
export NLSPATH

# Fix up the path to the GLUE session manager if necessary..
_DPGLUE_SM_PATH=${_DPGLUE_SM_PATH:=${VISUROOT}/../lib}
export _DPGLUE_SM_PATH

# Fix up LD_LIBRARY_PATH for libDtHelp..
LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}:/usr/dt/lib:${VISUROOT}/../dt/lib
export LD_LIBRARY_PATH
# fix path if not invoked via PATH..
if [ ! -z "$relpath" ]; then
	PATH=$relpath:$PATH export PATH
fi
# Create debugging environment if SW_VISU_DEBUG set..
if [ "${SW_VISU_DEBUG-xxx}" != "xxx" ]; then
	PS1="$basename debug> " export PS1
	cd `dirname $opt/$fname`
	echo $0: entering debugging environment - now in `pwd`...
	exec ${SHELL:-/bin/sh}
fi
# Ok, finally, let's go for it...
if [ ! -z "$*" ]; then
	exec $opt/$fname $xtra_args "$@"
else
	exec $opt/$fname $xtra_args
fi
