#!/bin/sh

if [ -z $1 ]; then

  LASTHOST="$HOME/.ssh/lasthost"
  TEXT="SSH is a secure way to login to a remote computer.\n
You must have an user account on that computer.\n
Please select the hostname from the list,\n
or type it by yourself like this:\n 
[username@]hostname.example.net.\n
Enter username only if other than $USER"

  HOSTLIST=""

  for LINE in `awk '{ print $1 }' < $HOME/.ssh/known_hosts `
  do
    HOST=`echo $LINE | cut -d ',' -f 1`
    HOSTLIST="$HOSTLIST $HOST"
  done

  if [ -z $HOSTLIST ]; then
    HOSTLIST="localhost"
  fi
  
  mkdir -p ~/.ssh
  Xdialog --title "SSH" --editable --combobox "$TEXT" 16 50 $HOSTLIST 2> $LASTHOST

  [ ! $? = 0 ] && exit 1

  HOSTNAME=`cat $LASTHOST`
  if [ "$HOSTNAME" ]; then
    exec aterm -e $0 $HOSTNAME
  fi
  exit 0
fi

ssh $HOSTNAME
if [ ! $? = 0 ]; then
   echo "Error. press Enter to quit"
   read
fi
