#!/bin/sh
# $Id: rxtelnet.in,v 1.1 1998/03/25 14:48:59 art Exp $
#
usage="Usage: $0 [-l username] [-k] [-t args_to_telnet] [-x args_to_xterm] [-w term_emulator] host [port]"
term=
kx_args=-P
while true
do
  case $1 in
  -l) telnet_args="${telnet_args} -l $2 "; kx_args="${kx_args} -l $2"; title="${2}@"; shift 2;;
  -t) telnet_args="${telnet_args} $2 "; shift 2;;
  -x) xterm_args="${xterm_args} $2 "; shift 2;;
  -k) kx_args="${kx_args} -k"; shift;;
  -w) term=$2; shift 2;;
  -*) echo "$0: Bad option $1"; echo $usage; exit 1;;
  *) break;;
  esac
done
if test $# -lt 1; then
  echo $usage
  exit 1
fi
host=$1
port=$2
title="${title}${host}"
bindir=/usr/X11R6/bin
pdc_trams=`dirname $0`
PATH=$pdc_trams:$bindir:$PATH
export PATH
set -- `kx $kx_args $host`
if test $# -ne 3; then
  exit 1
fi
screen=`echo $DISPLAY | sed -ne 's/[^:]*:[0-9]*\(\.[0-9]*\)/\1/p'`
pid=$1
disp=${2}${screen}
auth=$3
oldifs=$IFS
IFS=:
set -- $PATH
IFS=$oldifs
if test -z "$term"; then
  for j in xterm dtterm aixterm dxterm hpterm; do
    for i in $*; do
      test -n "$i" || i="."
      if test -x $i/$j; then
	term=$j; break 2
      fi
    done
  done
fi
if test -n "$term"; then
  ($term -title $title -n $title $xterm_args -e env DISPLAY=$disp XAUTHORITY=$auth telnet -D $telnet_args $host $port; kill -USR2 $pid) &
else
  env DISPLAY=$disp XAUTHORITY=$auth telnet -D $telnet_args $host $port
  kill -USR2 $pid
fi
