1#!/bin/sh
2# $Id$
3#
4usage="Usage: $0 [-l username] [-k] [-fF] [-t args_to_telnet] [-x args_to_xterm] [-K args_to_kx] [-w term_emulator] [-b telnet_binary] [-n] [-v] [-h | --help] [--version] host [port]"
5binary=telnet
6term=
7kx_args=-P
8while true
9do
10  case $1 in
11  -l) telnet_args="${telnet_args} -l $2 "; kx_args="${kx_args} -l $2"; title="${2}@"; shift 2;;
12  -t) telnet_args="${telnet_args} $2 "; shift 2;;
13  -x) xterm_args="${xterm_args} $2 "; shift 2;;
14  -f) telnet_args="${telnet_args} -f"; shift;;
15  -F) telnet_args="${telnet_args} -F"; shift;;
16  -k) kx_args="${kx_args} -k"; shift;;
17  -K) kx_args="${kx_args} $2 "; shift 2;;
18  -n) term=none; shift;;
19  -w) term=$2; shift 2;;
20  -b) binary=$2; shift 2;;
21  --version) echo "$0: %PACKAGE% %VERSION%"; exit 0;;
22  -h) echo $usage; exit 0;;
23  --help) echo $usage; exit 0;;
24  -v) set -x; verb=1; shift;;
25  -*) echo "$0: Bad option $1"; echo $usage; exit 1;;
26  *) break;;
27  esac
28done
29if test $# -lt 1; then
30  echo $usage
31  exit 1
32fi
33host=$1
34port=$2
35title="${title}${host}"
36bindir=%bindir%
37pdc_trams=`dirname $0`
38PATH=$pdc_trams:$bindir:$PATH
39export PATH
40set -- `kx $kx_args $host`
41if test $# -ne 3; then
42  echo "Warning: Cound not setup X forwarding"
43  pid=NO  
44  disp=""
45  auth=""
46else
47  screen=`echo $DISPLAY | sed -ne 's/[^:]*:[0-9]*\(\.[0-9]*\)/\1/p'`
48  pid=$1
49  disp=${2}${screen}
50  auth=$3
51fi
52oldifs=$IFS
53IFS=:
54set -- $PATH
55IFS=$oldifs
56if test -z "$term"; then
57  for j in xterm dtterm aixterm dxterm hpterm; do
58    for i in $*; do
59      test -n "$i" || i="."
60      if test -x $i/$j; then
61	term=$j; break 2
62      fi
63    done
64  done
65fi
66test "$verb" && echo "Telnet command used is `type $binary`."
67if test -n "$term" -a "$term" != "none"; then
68  ($term -title $title -n $title $xterm_args -e env DISPLAY=$disp XAUTHORITY=$auth $binary -D $telnet_args $host $port; test x"$pid" != xNO && kill -USR2 $pid) &
69else
70  env DISPLAY=$disp XAUTHORITY=$auth $binary -D $telnet_args $host $port
71  test x"$pid" != xNO && kill -USR2 $pid
72fi
73