#
# BASH completion for SSH hosts
#
# Completes:
#  - the ssh command with defined hostnames from your
#    ~/.ssh/config file (a grep -i for "host").
#  - the identity file for -i options (all files matching "id*" but not "*.pub")
#
#
# Install:
#   Just put it into /etc/bash_completion.d/ and restart the shell.
#   The autocompletion code will then autoload this file.
#
# @author Benedikt Hallinger <beni@hallinger.org>
#
# Written with the help of:
#   http://www.debian-administration.org/article/An_introduction_to_bash_completion_part_1
#   http://www.debian-administration.org/article/An_introduction_to_bash_completion_part_2


# The completion routines
_ssh_completion() 
{
	if [[ -r ~/.ssh && -r ~/.ssh/config ]]; then
		local cur prev opts
		COMPREPLY=()
		cur="${COMP_WORDS[COMP_CWORD]}"
		prev="${COMP_WORDS[COMP_CWORD-1]}"
		opts="-1 -2 -4 -6 -A -a -C -f -g -K -k -M -N -n -q -s -T -t -V -v -X -x -Y -y"
		optsargs="-b -c -D -e -F -I -i -L -l -m -o -O -p -R -S -w -W"

		if [[ ${cur} == -* ]]; then
			# option completion
                        COMPREPLY=( $(compgen -W "${opts} ${optsargs}" -- ${cur}) )
		else
			# current string is no option itself
			if [[ ${prev} == -i ]] ; then
				# if current option is -i, then complete identitys
        	                priv_keys="`grep -l PRIVATE ~/.ssh/*`"  # find private key files 
                	        COMPREPLY=( $(compgen -W "${priv_keys}" -- ${cur}) )
	                else
				# if no option is given, then try to complete with hostname
				hosts="`grep -i "^host " ~/.ssh/config |cut -d' ' -f2`"  # grep all lines
				COMPREPLY=( $(compgen -W "${hosts}" -- ${cur}) )
			fi
		fi
	fi
}

_scp_completion()
{
        if [[ -r ~/.ssh && -r ~/.ssh/config ]]; then
                local cur prev opts
                COMPREPLY=()
                cur="${COMP_WORDS[COMP_CWORD]}"
                prev="${COMP_WORDS[COMP_CWORD-1]}"
                opts="-1 -2 -4 -6 -B -C -p -q -r -v -i -h"

                if [[ ${cur} == -* ]]; then
                        # option completion
                        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
                else
                        # current string is no option itself
                        if [[ ${prev} == -i ]] ; then
                                # if current option is -i, then complete identitys
                                priv_keys="`grep -l PRIVATE ~/.ssh/*`"  # find private key files 
                                COMPREPLY=( $(compgen -W "${priv_keys}" -- ${cur}) )
                        else
                                # if no option is given, then try to complete with hostname and file names
                                hosts="`grep -i "^host " ~/.ssh/config |cut -d' ' -f2`"  # grep all lines
                                COMPREPLY=( $(compgen -f -W "${hosts}" -- ${cur}) )
                        fi
                fi
        fi
}


# Add completion functions to commands
complete -F _ssh_completion ssh
complete -F _scp_completion scp
complete -F _scp_completion sftp
