#!/bin/sh
# $Id: kxssh 2009-01-20 17:17:17Z haba $
#
usage="Usage: $0 [-l username] [-fF] [-s args_to_ssh] [-x args_to_term] [-b ssh_binary] [-n] [-v] [-h | --help] [--version] host [port]"
binary=ssh
term=
kx_args=-P
ssh_args="-Y -o GSSAPIKeyExchange=yes -o GSSAPIAuthentication=yes"
while true
do
  case $1 in
  -l) ssh_args="${ssh_args} -l $2 "; kx_args="${kx_args} -l $2"; title="${2}@"; shift 2;;
  -s) ssh_args="${ssh_args} $2 "; shift 2;;
  -x) xterm_args="${xterm_args} $2 "; shift 2;;
  -f) ssh_args="${ssh_args} -o GSSAPIDelegateCredentials=yes"; shift;;
  -F) ssh_args="${ssh_args} -o GSSAPIDelegateCredentials=yes"; shift;;
  -n) term=none; shift;;
  -b) binary=$2; shift 2;;
  --version) echo "$0: %PACKAGE% %VERSION%"; exit 0;;
  -h) echo $usage; exit 0;;
  --help) echo $usage; exit 0;;
  -v) set -x; verb=1; ssh_args="${ssh_args} -v "; shift;;
  -*) 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/heimdal/bin
pdc_trams=`dirname $0`
PATH=$pdc_trams:$bindir:$PATH
export PATH

oldifs=$IFS
IFS=:
set -- $PATH
IFS=$oldifs
if test -z "$term"; then
  for j in rxvt xterm ; do
    for i in $*; do
      test -n "$i" || i="."
      if test -x $i/$j; then
	term=$j; break 2
      fi
    done
  done
fi
test "$verb" && echo "SSH command used is `type $binary`."
if test -n "$term" -a "$term" != "none"; then
  ($term -title $title -name $title $xterm_args -e $binary $ssh_args $host $port) &
else
  env DISPLAY=$disp XAUTHORITY=$auth $binary $ssh_args $host $port
fi
