#!/bin/sh

# Copyright (C)2007 Sun Microsystems, Inc.
#
# 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.

if [ "$1" = "-check" ]; then exit 0; fi

usage()
{
	echo "USAGE: $0 -s <server-side port on SSH tunnel>"
	echo "       $0 -x <DISPLAY environment from client> <xauth MIT-MAGIC-COOKIE>"
	exit 1
}

if [ "$1" = "" ]; then usage $0; fi

if [ "$1" = "-s" ]; then

	if [ "$2" = ""  ]; then usage $0; fi
	VGL_CLIENT=localhost
	export VGL_CLIENT
	VGL_PORT=$2
	export VGL_PORT
	__VGL_SSHTUNNEL=1
	export __VGL_SSHTUNNEL

elif [ "$1" = "-x" ]; then

	if [ "$2" = "" -o "$3" = "" ]; then usage $0; fi
	XAUTH=xauth
	if [ -x /usr/X11R6/bin/xauth ]; then
		XAUTH=/usr/X11R6/bin/xauth
	else
		if [ -x /usr/openwin/bin/xauth ]; then
			XAUTH=/usr/openwin/bin/xauth
		fi
	fi
	if [ -z "$DISPLAY" ]; then
		DNUM=`echo $2 | sed 's/.*[:]//g'`
		SSH_IP=`echo $SSH_CLIENT | sed 's/[ ].*//g' | sed 's/.*[:]//g'`
		if [ ! -z "$SSH_IP" -a ! -z "$DNUM" ]; then
			DISPLAY=$SSH_IP:$DNUM
			export DISPLAY
		fi
	fi
	$XAUTH add $DISPLAY . $3
fi

exec $SHELL -l
