#!/bin/sh
#
# Copyright (c) 2006 by Karl J. Runge <runge@karlrunge.com>
#
# ssvnc_cmd:
#
#    A wrapper that calls ss_vncviewer to use the enhanced TightVNC viewer. 
#
# The enhanced TightVNC viewer features are:
#
#	- SSL support for connections using the co-bundled stunnel program.
#	- rfbNewFBSize VNC support (screen resizing)
#	- cursor alphablending with x11vnc at 32bpp
#	- xgrabserver support for fullscreen mode (for old window mgrs)
#
#
# Your platform (e.g. Linux.i686) is autodetected and enhanced
# vncviewer and stunnel binaries for it are used (see the ./bin directory).
#
# See the build.unix script if your platform is not in this package.
# You can also set the env. var. UNAME=os.arch to any "os.arch" you want
# to override the autodetetion.
#
# Usage:
#
#     ssvnc_cmd [ss_vncviewer-args] hostname:N [tightvncviewer-args]
#
# "hostname:N" is the host and VNC display to connect to, e.g. snoopy:0
#
# See the script util/ss_vncviewer for details about its arguments:
#
#	-verify pemfile
#	-mycert pemfile
#	-proxy  phost:pport
#	-alpha
#	-grab
#
#
# If the *very first* argument is "-cotvnc" then it is assumed you are on
# Darwin and want to run the Chicken of the VNC viewer via our wrapper.
#
#
# See the TightVNC viewer documentation for on its cmdline arguments.
#
# For convenience, here is the current (7/2006) TightVNC viewer -help output:
#
#       TightVNC viewer version 1.3dev5
#       
#       Usage: vncviewer [<OPTIONS>] [<HOST>][:<DISPLAY#>]
#              vncviewer [<OPTIONS>] [<HOST>][::<PORT#>]
#              vncviewer [<OPTIONS>] -listen [<DISPLAY#>]
#              vncviewer -help
#       
#       <OPTIONS> are standard Xt options, or:
#               -via <GATEWAY>
#               -shared (set by default)
#               -noshared
#               -viewonly
#               -fullscreen
#               -noraiseonbeep
#               -passwd <PASSWD-FILENAME> (standard VNC authentication)
#               -user <USERNAME> (Unix login authentication)
#               -encodings <ENCODING-LIST> (e.g. "tight copyrect")
#               -bgr233
#               -owncmap
#               -truecolour
#               -depth <DEPTH>
#               -compresslevel <COMPRESS-VALUE> (0..9: 0-fast, 9-best)
#               -quality <JPEG-QUALITY-VALUE> (0..9: 0-low, 9-high)
#               -nojpeg
#               -nocursorshape
#               -x11cursor
#               -autopass
#       
#       Option names may be abbreviated, e.g. -bgr instead of -bgr233.
#       See the manual page for more information.
#  

if [ "X$1" = "X-h" -o "X$1" = "X-help" -o "X$1" = "X--help" ]; then
	head -76 "$0" | grep -v bin/sh
	exit
fi

# Include /usr/bin... to be sure to get regular utilities:
#
PATH=$PATH:/usr/bin:/bin
export PATH

# Set this for ss_vncviewer to pick up:
#
if [ "X$1" = "X-cotvnc" ]; then
	shift
	DARWIN_COTVNC=1
	export DARWIN_COTVNC
elif [ "X$DARWIN_COTVNC" = "X" -a "X$DISPLAY" = "X" ]; then
	uname=`uname`
	if [ "X$uname" = "XDarwin" ]; then
		DARWIN_COTVNC=1
		export DARWIN_COTVNC
	fi
fi

use_ours=0
if [ "X$VNCVIEWERCMD" = "X" ]; then
	VNCVIEWERCMD="vncviewer"
	export VNCVIEWERCMD
	if [ "X$DARWIN_COTVNC" != "X" ]; then
		use_ours=1
	fi
fi

# work out os.arch platform string and check for binaries:
#
name=$UNAME
if [ "X$name" = "X" ]; then
	name=`uname -sm | sed -e 's/ /./g'`
fi

f="$0"
for t in 1 2 3 4 5 6
do
	if [ -L "$f" ]; then
		f0="$f"
		f=`ls -l "$f" | sed -e 's/^.* -> //'`
		if echo "$f" | grep '^/' > /dev/null; then
			:
		else
			f="`dirname "$f0"`/$f"
		fi
	else
		break
	fi
done
dir=`dirname "$f"`
PATH="$dir:$PATH"

nearby=0
if [ -x "$dir/vncviewer" -a -x "$dir/stunnel" ]; then
	nearby=1
fi
if [ ! -d "$dir/$name" -a $nearby = 0 ]; then
	echo
	echo "Cannot find platform dir for your OS `uname -sm`:"
	echo
	echo "    $dir/$name" 
	echo
	PATH=$PATH:/usr/sbin:/usr/local/sbin:/dist/sbin

	quit=0
	if type vncviewer >/dev/null 2>/dev/null; then
		:
	else
		echo "vncviewer not found in PATH." 
		quit=1
	fi
	if type stunnel >/dev/null 2>/dev/null; then
		:
	else
		echo "stunnel not found in PATH."
		quit=1
	fi
	echo
	if [ "X$quit" = "X1" ]; then
		echo "You can set the \$UNAME env. var. to override the OS setting."
		echo "Or, if available, run the ./build.unix script to build it."
		echo "Or install external \"vncviewer\" and \"stunnel\" packages."
		exit 1
	fi
	echo "Using externel \"vncviewer\" and \"stunnel\" found in PATH."

else
	STUNNEL_EXTRA_OPTS=${STUNNEL_EXTRA_OPTS:-"maxconn = 1"}
	export STUNNEL_EXTRA_OPTS
	SSVNC_VIEWER_INTERNAL=1
	export SSVNC_VIEWER_INTERNAL
fi

if [ "X$DARWIN_COTVNC" = "X" -a "X$VNCVIEWERCMD" = "Xvncviewer" ]; then
	hstr=`"$VNCVIEWERCMD" -h 2>&1 | head -5`
	if echo "$hstr" | grep '^TightVNC.*version 1\.[23]' > /dev/null; then
		# we need to avoid raw encoding
		use_ours=1
	fi
fi

# Put our os.arch and other utils dirs at head of PATH to be sure to
# pick them up:
#
PATH="$dir:$dir/$name:$dir/util:$PATH"
if echo "$dir" | grep '^/' > /dev/null; then
	:
else
	dir=`pwd`/$dir
	PATH="$dir:$dir/$name:$dir/util:$PATH"
fi

base=`basename "$0"`
if [ "X$1" = "X-ssl" ]; then
	shift	
	base="ssvnc_cmd"
fi

# If ours (and not cotvnc), force the use of tight encoding for localhost
# redir connection:
#
#
if [ $use_ours = 1 ]; then
	if [ "X$base" = "Xtightvncviewer" ]; then
		"$VNCVIEWERCMD" -encodings 'copyrect tight zrle zlib hextile' "$@"
	else
		ss_vncviewer "$@" -encodings 'copyrect tight zrle zlib hextile'
	fi
else
	if [ "X$base" = "Xtightvncviewer" ]; then
		"$VNCVIEWERCMD" "$@"
	else
		ss_vncviewer "$@"
	fi
fi