#!/bin/sh

HOST="$1"

case "$2" in
-new)
	ssh -t "$HOST" vncserver -localhost ;;
esac

DPY="`ssh "$HOST" ls .vnc | sed -ne 's/\.pid$//p' | head -1 | cut -d: -f2`"
test -z "$DPY" && echo "No vncserver found; use $0 $1 -new" && exit 1
PORT="`expr 5900 + $DPY`"
ssh -L "$PORT:localhost:$PORT" "$HOST" sleep 604800 & trap "kill $!" 0 1 15
sleep 1
vncviewer localhost:$DPY

