#!/bin/sh

# Copyright (C)2004 Landmark Graphics Corporation
# Copyright (C)2005 Sun Microsystems, Inc.
# Copyright (C)2009-2016, 2018-2019, 2021 D. R. Commander
#
# This library is free software and may be redistributed and/or modified under
# the terms of the wxWindows Library License, Version 3.1 or (at your option)
# any later version.  The full license is in the LICENSE.txt file included
# with this distribution.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# wxWindows Library License for more details.

usage()
{
	echo
	echo "Usage: `basename $0` [options] [--] <3D application> [3D application args]"
	echo
	echo "VirtualGL options (see documentation for a more comprehensive list)"
	echo
	echo "-c <c>    : proxy = Send rendered frames uncompressed using X11 Transport"
	echo "                    [default if the 2D X server is on this machine]"
	echo "            jpeg = Compress rendered frames using JPEG/send using VGL Transport"
	echo "                   [default if the 2D X server is on another machine]"
	echo "            rgb = Encode rendered frames as RGB/send using VGL Transport"
	echo "            xv = Encode rendered frames as YUV420P/send using XV Transport"
	echo "            yuv = Encode rendered frames as YUV420P/send using the VGL"
	echo "                  Transport and display on the client using X Video"
	echo "            [If an image transport plugin is being used, then <c> can be any"
	echo "             number >= 0 (default = 0).]"
	echo
	echo "-nodl     : Don't interpose the dlopen() function.  dlopen() is normally"
	echo "            interposed in order to force applications that load libGL using"
	echo "            dlopen() to load the VirtualGL Faker instead.  For the more common"
	echo "            case of applications that link directly with libGL, disabling the"
	echo "            dlopen() interposer makes VirtualGL less intrusive, since it will"
	echo "            no longer load libGL until the 3D application actually uses that"
	echo "            library."
	echo
	echo "-d <d>    : <d> = the X display/screen or DRI device to use for 3D rendering"
	echo "           [default = :0.0]"
	echo
	echo "-fps <f>  : Limit image transport frame rate to <f> frames/sec"
	echo
	echo "-gamma <g>: Set gamma correction factor to <g> (see User's Guide)"
	echo
	echo "-ge       : Fool 3D application into thinking that LD_PRELOAD is unset"
	echo
	echo "-ms <s>   : Force OpenGL multisampling to be enabled with <s> samples"
	echo "            (<s> = 0 forces multisampling to be disabled)"
	echo
	echo "-np <n>   : Use <n> threads to perform image compression [default = 1]"
	echo
	echo "+ocl/-ocl : Enable/disable OpenCL interposer [default = disabled]"
	echo
	echo "+/-pr     : Enable/disable performance profiling output [default = disabled]"
	echo
	echo "-q <q>    : Compression quality [1 <= <q> <= 100]"
	echo "            [default = 95 for JPEG/VGL Transport.  Has no effect with"
	echo "             other built-in image transports or encoding types]"
	echo
	echo "-samp <s> : Chrominance subsampling factor"
	echo "            <s> = gray, 1x, 2x, 4x"
	echo "            [default = 1x for JPEG/VGL Transport.  Has no effect with"
	echo "             other built-in image transports or encoding types]"
	echo
	echo "+/-s      : Enable/disable SSL encryption of VGL Transport or custom"
	echo "            image transport, if applicable."
	echo "            [default = disabled.  Has no effect on the VGL Transport unless"
	echo "             VirtualGL was built with OpenSSL support]"
	echo
	echo "+/-sp     : Turn on/off frame spoiling [default = enabled]"
	echo
	echo "-st <s>   : left = Read back/transport only left eye buffer of rendered stereo"
	echo "                   frame"
	echo "            right = Read back/transport only right eye buffer of rendered"
	echo "                    stereo frame"
	echo "            quad = Use quad-buffered stereo if available, otherwise use"
	echo "                   red/cyan (anaglyphic) stereo [default]"
	echo "            rc = Always use red/cyan (anaglyphic) stereo"
	echo "            gm = Always use green/magenta (anaglyphic) stereo"
	echo "            by = Always use blue/yellow (anaglyphic) stereo"
	echo "            i = Always use interleaved (passive) stereo"
	echo "            tb = Always use top/bottom (passive) stereo"
	echo "            ss = Always use side-by-side (passive) stereo"
	echo
	echo "+/-sync   : Enable/disable strict 2D/3D synchronization [default = disabled]"
	echo
	echo "+/-tr     : Enable/disable function call tracing (generates a lot of output)"
	echo "            [default = disabled]"
	echo
	echo "-trans <t>: Use transport plugin contained in library libvgltrans_<t>.so"
	echo
	echo "+/-v      : Enable/disable verbose VirtualGL messages [default = disabled]"
	echo
	echo "+xcb/-xcb : Enable/disable XCB interposer [default = enabled]"
	echo
	echo "+wm/-wm   : Enable/disable window manager mode (for running Compiz, etc.)"
	echo
	exit $1
}

GEOPROBE_USEGLX=1
export GEOPROBE_USEGLX
PROMAGIC_USEGLX=1
export PROMAGIC_USEGLX
VBOX_CROGL_FORCE_SUPPORTED=1
export VBOX_CROGL_FORCE_SUPPORTED

__VGL_DL=1
__VGL_GE=0
VGL_LIBRARY_PATH=
MYPATH=`dirname $0`

while [ $# -gt 0 ]
do
	case "$1" in
	+sy*)       VGL_SYNC=1        ; export VGL_SYNC     ;;
	-sy*)       VGL_SYNC=0        ; export VGL_SYNC     ;;
	-st*)       VGL_STEREO=$2     ; export VGL_STEREO   ; shift     ;;
	+sp*)       VGL_SPOIL=1       ; export VGL_SPOIL    ;;
	-sp*)       VGL_SPOIL=0       ; export VGL_SPOIL    ;;
	-sa*)       VGL_SUBSAMP=$2    ; export VGL_SUBSAMP  ; shift     ;;
	-s*)        VGL_SSL=0         ; export VGL_SSL      ;;
	+s*)        VGL_SSL=1         ; export VGL_SSL      ;;
	-pr*)       VGL_PROFILE=0     ; export VGL_PROFILE  ;;
	+pr*)       VGL_PROFILE=1     ; export VGL_PROFILE  ;;
	-q*)        VGL_QUAL=$2       ; export VGL_QUAL     ; shift     ;;
	+de*)       VGL_DEBUG=1       ; export VGL_DEBUG    ;;
	-de*)       VGL_DEBUG=0       ; export VGL_DEBUG    ;;
	-nodl)      __VGL_DL=0        ;;
	-trans*)    VGL_TRANSPORT=$2  ; export VGL_TRANSPORT; shift     ;;
	+tr*)       VGL_TRACE=1       ; export VGL_TRACE    ;;
	-tr*)       VGL_TRACE=0       ; export VGL_TRACE    ;;
	-d*)        VGL_DISPLAY=$2    ; export VGL_DISPLAY  ; shift     ;;
	-cl*)       VGL_CLIENT=$2     ; export VGL_CLIENT   ; shift     ;;
	-p*)        VGL_PORT=$2       ; export VGL_PORT     ; shift     ;;
	-c*)        VGL_COMPRESS=$2   ; export VGL_COMPRESS ; shift     ;;
	-np*)       VGL_NPROCS=$2     ; export VGL_NPROCS   ; shift     ;;
	-gamma)     VGL_GAMMA=$2      ; export VGL_GAMMA    ; shift     ;;
	-ge)        __VGL_GE=1        ;;
	-g*)        VGL_GAMMA=0       ; export VGL_GAMMA    ;;
	+g*)        VGL_GAMMA=1       ; export VGL_GAMMA    ;;
	-v*)        VGL_VERBOSE=0     ; export VGL_VERBOSE  ;;
	+v*)        VGL_VERBOSE=1     ; export VGL_VERBOSE  ;;
	-fps)       VGL_FPS=$2        ; export VGL_FPS      ; shift     ;;
	-ms)        VGL_SAMPLES=$2    ; export VGL_SAMPLES  ; shift     ;;
	+wm)        VGL_WM=1          ; export VGL_WM       ;;
	-wm)        VGL_WM=0          ; export VGL_WM       ;;
	+ocl)       VGL_FAKEOPENCL=1  ; export VGL_FAKEOPENCL  ;;
	-ocl)       VGL_FAKEOPENCL=0  ; export VGL_FAKEOPENCL  ;;
	+xcb)       VGL_FAKEXCB=1     ; export VGL_FAKEXCB  ;;
	-xcb)       VGL_FAKEXCB=0     ; export VGL_FAKEXCB  ;;
	-ld)        VGL_LIBRARY_PATH=$2 ; shift ;;
	-ldp)       VGL_PRELOAD=$2 ; shift ;;
	-h)         usage 0 ;;
	--help)     usage 0 ;;
	--)         shift   ; break ;;
	-*)         usage 1 ;;
	*)          break   ;;
	esac
	shift
done
if [ $# -eq 0 ]
then
	usage 0
fi

if [ -r /etc/opt/VirtualGL/vgl_xauth_key ]; then
	XAUTH=xauth
	if [ -x /usr/X11R6/bin/xauth ]; then
		XAUTH=/usr/X11R6/bin/xauth
	fi
	if [ -x /usr/openwin/bin/xauth ]; then
		XAUTH=/usr/openwin/bin/xauth
	fi
	$XAUTH merge /etc/opt/VirtualGL/vgl_xauth_key
fi

SSH_IP=`echo $SSH_CLIENT | cut -d' ' -f1`
if [ -z "$DISPLAY" ]; then
	if [ ! -z "$SSH_IP" ]; then
		DISPLAY=$SSH_IP
		export DISPLAY
		echo "[VGL] NOTICE: Automatically setting the DISPLAY environment variable to"
		echo "[VGL]    $DISPLAY, based on the IP address of your SSH client."
	fi
else
	DISPLAYHOST=`echo $DISPLAY | cut -d: -f1`
	if [ "$DISPLAYHOST" = "localhost" -o "$DISPLAYHOST" = "`hostname`" ]; then
		if [ ! -z "$SSH_IP" -a -z "$VGL_CLIENT" -a -z "$RRCLIENT" ]; then
			VGL_CLIENT=$SSH_IP
			export VGL_CLIENT
			echo "[VGL] NOTICE: Automatically setting VGL_CLIENT environment variable to"
			echo "[VGL]    $SSH_IP, the IP address of your SSH client."
		fi
	fi
fi

# This is so we can launch both 32-bit and 64-bit executables using the same
# script.
if [ -x "$MYPATH/.vglrun.vars64" ]; then
	. "$MYPATH/.vglrun.vars64"
fi
if [ -x "$MYPATH/.vglrun.vars32" ]; then
	. "$MYPATH/.vglrun.vars32"
fi
if [ ! -z "$VGL_LIBRARY_PATH" ]; then
	LD_LIBRARY_PATH=$VGL_LIBRARY_PATH:$LD_LIBRARY_PATH
	export LD_LIBRARY_PATH
	if [ ! -z "$VGL_VERBOSE" ]; then
		if [ $VGL_VERBOSE -eq 1 ]; then
			echo [VGL] NOTICE: Added $VGL_LIBRARY_PATH to LD_LIBRARY_PATH
		fi
	fi
fi

# This allows scripts to detect whether they are being launched with vglrun.
VGL_ISACTIVE=1
export VGL_ISACTIVE

SUFFIX=
if [ $__VGL_DL -eq 0 ]; then
	SUFFIX=-nodl
fi
if [ "$SUFFIX" = "" -a "$VGL_FAKEOPENCL" = "1" ]; then
	SUFFIX=-opencl
fi
if [ ! -z "$LD_PRELOAD" ]; then
	# Remove any existing VGL fakers from LD_PRELOAD.  This is necessary in order
	# to prevent a faker mismatch (and subsequent application crash) if vglrun is
	# executed from within a window manager or other process that was itself
	# launched using vglrun with different -nodl or +ocl options.
	LD_PRELOAD=$(echo "$LD_PRELOAD" | sed "s/libdlfaker\.so\:*//g" | \
		sed "s/libgefaker\.so\:*//g" | \
		sed "s/libvglfaker[^\.]*\.so\:*//g")
fi
if [ -z "$LD_PRELOAD" ]; then
	LD_PRELOAD=libvglfaker$SUFFIX.so
else
	LD_PRELOAD=libvglfaker$SUFFIX.so:$LD_PRELOAD
fi
if [ $__VGL_DL -eq 1 ]; then
	LD_PRELOAD=libdlfaker.so:$LD_PRELOAD
fi
if [ $__VGL_GE -eq 1 ]; then
	LD_PRELOAD=libgefaker.so:$LD_PRELOAD
fi
if [ ! -z "$VGL_PRELOAD" ]; then
	LD_PRELOAD=$VGL_PRELOAD:$LD_PRELOAD
fi
export LD_PRELOAD
exec ${1+"$@"}
