#!/bin/sh
#
# nxviewer_helper - Small helper for external vncviewer program as needed by NX 3.0.0 backend.
#
# Copyright (c) 2007 by Fabian Franz <freenx@fabian-franz.de>
#
# License: GPL, v2
#
# SVN: $Id $
#

[ -z "$COMMAND_VNCVIEWER" ] && COMMAND_VNCVIEWER="vncviewer"
[ -z "$COMMAND_VNCPASSWD" ] && COMMAND_VNCPASSWD="$PATH_BIN/nxpasswd"
[ -z "$COMMAND_X11VNC" ] && COMMAND_X11VNC="x11vnc"

mkdir -p "$NXSESSION_DIRECTORY/scripts/"
echo "$agent_password" | $COMMAND_VNCPASSWD "$NXSESSION_DIRECTORY/scripts/.passwd" doit

# Start x11vnc
if [ -n "$mirrordisplay" ]
then
	(
		unset XAUTHORITY
		# Note: No "-accept popup" as its the same user and with that password
		#       he could do already more than -accept popup would allow.

		# Jeremy Wilkins modified version to permit X0 display mirroring
		# on Debian (not sure if it affects other dists)
		# Before x11vnc would fail if passed 127.0.0.1:0.0 as the DISPLAY if the
		# display was local.  Fixed by removing the host reference on local
		# displays.  Also, -localhost option was added for security and sleep
		# timeout was increased to reduce failed mirrors for slow loading.
		if [ "$mirrorhost" == "127.0.0.1" ]
		then
			MIRROR_DISPLAY=":$mirrordisplay.0"
		else
			MIRROR_DISPLAY="$mirrorhost:$mirrordisplay.0"
		fi
		DISPLAY="$MIRROR_DISPLAY" $COMMAND_X11VNC -localhost -timeout 120 -once -rfbauth "$NXSESSION_DIRECTORY/scripts/.passwd" >"$NXSESSION_DIRECTORY/scripts/.vnc_port" &
	)
	sleep 2
	agent_port=$(cat "$NXSESSION_DIRECTORY/scripts/.vnc_port" | egrep "^PORT=" | cut -d'=' -f 2)
	[ -z "agent_port" ] && agent_port="0"
	# note the :: is not a mistake, but rather a hint for nxviewer to use this as a port and not
	# interpret it as a display.
	agent_server="127.0.0.1::$agent_port"
	rm -f "$NXSESSION_DIRECTORY/scripts/.vnc_port"
fi

# Start the vncviewer

exec $COMMAND_VNCVIEWER -passwd "$NXSESSION_DIRECTORY/scripts/.passwd" -fullscreen $AGENT_EXTRA_OPTIONS_RFB "$agent_server" 
