#! /bin/sh
#
# vre: wrapper for vreng
#
prefix=/usr
exec_prefix=/usr

PATH=${prefix}/bin:/usr/X11R6/bin:$PATH; export PATH
LD_LIBRARY_PATH=${prefix}/lib:/usr/X11R6/lib:/usr/lib:${prefix}/lib/mysql:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH

VRENG32_OGL=${exec_prefix}/bin/vreng-ogl
VRENG32_TGL=${exec_prefix}/bin/vreng-tgl
VRENG64_OGL=/usr/local/sparcv9/bin/vreng-ogl
VRENG64_TGL=/usr/local/sparcv9/bin/vreng-tgl

os=`uname -s``uname -r`

#
# get color depth
#
for i in 32 28 24 16 8
do
	depth=`xdpyinfo | grep "$i planes" | tail -1 | awk '{print $2}'`
	if [ "${depth}" != "" ]; then
		break
	fi
done
if [ "${depth}" = "" ]; then
	echo "Your visual has an unsupported color depth"
	exit 1
fi

#
# get bits architecture
#
bits=32
if [ -x /usr/bin/isainfo ]; then
	bits=`/usr/bin/isainfo -b`
fi
bits=32

case ${bits} in
32)
	#
	# 32 bits architecture
	#
	if xdpyinfo | grep GLX >/dev/null ; then
		#
		# X server has GLX extension
		#
		case ${depth} in
		8)
			if [ -x ${VRENG32_OGL} ]; then
				exec ${VRENG32_OGL} "$@"
			fi
			#if [ -x ${VRENG32_TGL} ]; then
			#	exec ${VRENG32_TGL} "$@"
			#fi
			;;
		16|24|28|32)
			LD_LIBRARY_PATH=/usr/openwin/lib:$LD_LIBRARY_PATH
			if [ -x ${VRENG32_OGL} ]; then
				exec ${VRENG32_OGL} -q -F "$@"
			fi
			;;
		esac
	else
		#
		# X server has not GLX extension
		#
		if [ -x ${VRENG32_OGL} ]; then
			exec ${VRENG32_OGL} "$@"
		fi
	fi
	;;
64)
	#
	# 64 bits architecture
	#
	if xdpyinfo | grep GLX >/dev/null ; then
		#
		# X server has GLX extension
		#
		case ${depth} in
		8)
			LD_LIBRARY_PATH=/usr/local/sparcv9/lib:/usr/X11R6/sparcv9/lib:$LD_LIBRARY_PATH
			if [ -x ${VRENG64_OGL} ]; then
				exec ${VRENG64_OGL} "$@"
			fi
			;;
		16|24|28|32)
			LD_LIBRARY_PATH=/usr/openwin/sparcv9/lib:/usr/openwin/lib/sparcv9:/usr/X11R6/sparcv9/lib:$LD_LIBRARY_PATH
			if [ -x ${VRENG64_OGL} ]; then
				exec ${VRENG64_OGL} -q "$@"
			fi
			;;
		esac
	else
		#
		# X server has not GLX extension
		#
		LD_LIBRARY_PATH=/usr/local/sparcv9/lib:/usr/X11R6/sparcv9/lib:$LD_LIBRARY_PATH
		if [ -x ${VRENG64_OGL} ]; then
			exec ${VRENG64_OGL} "$@"
		fi
	fi
	;;
esac

echo "no vreng.*gl executable found"
