#! /bin/sh

# geomview shell script driver

### If you want to be able to run geomview from a directory other than
### its distribution directory, or if you want to install it
### permanently on your system, first decide on a permanent place to
### keep the geomview distribution directory, and move or copy the
### entire directory (and all its subdirectories) to that location.
### Then change "." on the line defining geomview_dir below to the
### absolute pathname of the distribution directory.  It is important
### that there be no spaces around the '=' sign.  For example, if the
### pathname of the geomview distribution directory on your system is
### "/usr/local/Geomview", change the line below to
### 
### 	GEOMROOT="/usr/local/Geomview"
### 
### Once you have made this change you can install a copy of this
### shell script anywhere you want (e.g. /usr/local/bin) and use it to
### invoke geomview.
###
### Here is the line to change:

GEOMROOT="/u/gcg/ngrap"

########################################################################
#
#		  DO NOT CHANGE ANYTHING BELOW HERE		       #
#
########################################################################

### Geomview and/or its modules use the following environment
### variables.  For each of these that does not already have a value,
### this shell script assigns a value based on the setting of
### geomview_dir above.  You can override these values if you want by
### setting them in your shell environment before invoking this
### script.
### 
### GEOMVIEW_DIR: pathname of geomview distribution directory
### GEOMVIEW_GVX: pathname of executable file "gvx"
### GEOMVIEW_LOAD_PATH: colon-separated list of directories to search
### 	for files
### GEOMVIEW_EMODULE_PATH: colon-separated list of directories to search
### 	for modules
### GEOMVIEW_SYSTEM_INITFILE: pathname of an initialization file to
### 	read upon startup
### GEOMDATA: [ used only by some modules; for backward compatibility ]
### 	pathname of the data directory

MACHTYPE=sgi

: ${GEOMROOT=$geomview_dir}
: ${GEOMVIEW_GVX=${GEOMROOT}/bin/${MACHTYPE}/gvx}
suf=

# : ${GEOMVIEW_GVX=`dirname $0`/gvx}
: ${GEOMVIEW_LOAD_PATH=.:${GEOMROOT}/data/geom:${GEOMROOT}/data}
: ${GEOMVIEW_EMODULE_PATH=${GEOMROOT}/modules/${MACHTYPE}}
: ${GEOMVIEW_SYSTEM_INITFILE=${GEOMROOT}/data/.geomview}

# Geomview no longer uses GEOMDATA, but some external modules do.

: ${GEOMDATA=${GEOMROOT}/data}


export GEOMROOT GEOMVIEW_GVX GEOMVIEW_LOAD_PATH GEOMDATA \
        GEOMVIEW_EMODULE_PATH GEOMVIEW_SYSTEM_INITFILE


gvx="${GEOMVIEW_GVX}"
case "$gvx" in
  *gvx.[a-zA-Z3])
    gvx="`expr match \"$GEOMVIEW_GVX\" '(.*)\.[^./]*' \| \"$GEOMVIEW_GVX\"`" ;;
esac

can_C3D() {
  case "$DISPLAY" in
    :*|unix:*) test -w /dev/fbs/ffb0 || return 1 ;;
  esac
  case `ldd "${gvx}.C3D" 2>&1` in
    *found*|*"No such"*) return 1 ;;
  esac
}

gvx_option() {
 case "$1" in
  -ogl|-OGL|-opengl|-OpenGL|-OPENGL)
	suf=.OGL; suf2=.MESA
	if [ "$MACHTYPE" = "solaris" ]; then
	    suf=.MESA
	    if can_C3D; then suf=.C3D; fi
	fi
	shift ;;
  -mesa|-Mesa|-MESA)
	suf=.MESA; suf2=.OGL ;;
  -x11|-X11)
	suf=.X11; shift ;;
  -c3d|-C3D|-Creator3D|-creator3d)
	suf=.C3D; suf2=.MESA ;;
  -h|-help|-HELP|-\?*)
    echo "For advice on selecting which geomview graphics option to use,
see also \"geomview -graphics\"."
    ;;
  -grap*|-Grap*|-GRAP*)
    if [ -f "$gvx.X11" -o -f "$gvx.MESA" -o -f "$gvx.C3D" -o -f "$gvx.OGL" ]; then
	g=`basename $0`
	echo "The $0 script can invoke any of several executables:"
	test -f "$gvx.X11" && \
	  echo "  with geomview's own X software rendering: use $g -x11"
	test -f "$gvx.MESA" && \
	  echo "  with Mesa OpenGL-like software rendering: use $g -mesa"
	test -f "$gvx.X11" && \
	  echo "  with Sun OpenGL for UltraSparcs with Creator3D: use $g -creator3d"
	test -f "$gvx.OGL" && \
	  echo "  with Open GL: use $g -opengl"
	echo "If given, this option MUST BE THE FIRST on geomview's command line."
	echo "Using some variant of Open GL allows transparency and texture-mapping;"
	echo "software OpenGL implementations are slower than $g -x11, but hardware ones"
	echo "run faster."
	echo "To change the default, setenv GEOMVIEW_GVXOPT to the appropriate option."
    fi >&2
    exit 1
    ;;
  *) test ;;
 esac
}

gvx_option ${GEOMVIEW_GVXOPT}

if gvx_option "$1"; then shift; fi


if [ "$MACHTYPE" = "solaris" -a -z "$suf" -a -w /dev/fbs/ffb0 ] && can_C3D; then
  echo "$0: Invoking Creator3D version (see \"geomview -graphics\" for info)" >&2
  suf=.C3D
fi

if [ -f "$gvx$suf" ]; then
  GEOMVIEW_GVX="$gvx$suf"
elif [ -f "$gvx$suf2" ]; then
  GEOMVIEW_GVX="$gvx$suf2"
elif [ -f "$gvx" ]; then
  GEOMVIEW_GVX="$gvx"
fi
export GEOMVIEW_GVX

case "$1" in
  -dbx) GEOMVIEW_DEBUG=1 exec dbx $GEOMVIEW_GVX ;;
  -edge) GEOMVIEW_DEBUG=1 exec edge $GEOMVIEW_GVX ;;
  -gdb) GEOMVIEW_DEBUG=1 exec gdb $GEOMVIEW_GVX ;;
  -xdb) GEOMVIEW_DEBUG=1 exec xdb $GEOMVIEW_GVX ;;
  *) exec $GEOMVIEW_GVX ${1+"$@"} ;;
esac
