#!/bin/sh
#	shortcut-fast remote login / ssh
#	remembers hosts and looks for them in .rl/hosts
#	by Nick - this version 4.2 - 16 Mar 2016


rldir=$HOME/".rl"
if (test ! -d $rldir) then
   mkdir $rldir
fi

listfile=$rldir/"hosts"

sname=`basename $1 | awk 'BEGIN{FS="@"}{if (NF==2){print $2}else{print}}'`
username=`basename $1 | awk 'BEGIN{FS="@"}{if (NF==2){print $1}}'`
#echo sname=$sname
#echo username=$username

# get full name with domain:
hname=`awk '	BEGIN{FS="[, ]"}{for (i=1;i<=NF;i++)
		    {if($i ~ "'$sname'.*"){print $i;exit}}}' $listfile`
if [ "$hname" = "" ]
then			#	this host is not listed in $listfile:
	hname=$sname	#	assume it is spelled out completely inline
fi
fullname=$hname
if [ "$username" != "" ]
then
	fullname="$username"@"$hname"
fi
command="ssh -X $fullname"
echo "===> $command"
if($command)then
    t=/tmp/rl.$$
    cp $listfile $t
    echo $hname >> $t
    sort -u $t > $listfile
    rm $t
fi
