#!/bin/bash
#XXX due to the -S option of test not being a bourne shell option this script
#needs ksh or bash
#XXX
#login to a remote X session
# starts an Xvnc server on an unused display number in -query -once mode to
# an XDMCP server speficied as the first argument and then launches vncviewer
# to connect to it.
if [ $# -ne 1 ] 
then
echo "usage: $0 host"
echo "where host = a server that accepts XDMCP querys"
exit 1;
fi


X11SOCK=0
for i in /tmp/.X11-unix/*
do
if [ -S $i ] 
then
X11SOCK=`expr $X11SOCK + 1`
fi
done

#prevent us from starting Xvnc as display 0
case $X11SOCK in

0)
X11SOCK=1;;
*)
;;
esac

#Start the Xvnc server.
echo "Launching Xvnc:"
echo "Xvnc -geometry 1024x768 :$X11SOCK -depth 24 -localhost -query $1 -once &"
Xvnc -geometry 1024x768 :$X11SOCK -depth 24 -localhost -query $1 -once  -fp tcp/:7100 &
#get Xvnc PID so we can kill it on a sigint and wait for it later.
WAIT=$!
#kill Xvnc on sigint
trap "kill $WAIT; wait $WAIT" 2
#launch the viewer.
sleep 1
vncviewer localhost:$X11SOCK
kill $WAIT;
#wait for Xvnc to exit
wait $WAIT



