#!/usr/bin/env bash

###########################################################################
##
## This script sets up basic bash completion for the kdb command
##
##
## Put it under /etc/bash_completion.d or load it just for the current
## session with . <scriptname>
##
###########################################################################

_kdb()
{
	COMPREPLY=()

	# assign the currently active word
	local cur="${COMP_WORDS[COMP_CWORD]}"
	local prev="${COMP_WORDS[COMP_CWORD-1]}"

	# initialize
	local kdbpath=$(which kdb)

	# only kdb was entered yet, print a list of available commands
	if [[ $COMP_CWORD -eq 1 ]]
	then
		local IFS=$'\n'
		local commands=( $( ${kdbpath} 2>/dev/null | sed -e '0,/^Known commands are/d' | awk '{print $1}' ) )
		COMPREPLY=( $( compgen -W '${commands[@]}' -- "${cur}" ) )
		return
	fi

	# commands which expects a path as next parameter
	local pathcommands="export file get getmeta cp ls lsmeta mv rm set setmeta sget vset"
	if [[ $pathcommands =~ $prev ]]
	then
		local IFS=$'\n'
		local paths=( $( ( ${kdbpath} ls system && ${kdbpath} ls user ) 2>/dev/null ) )
		COMPREPLY=( $( compgen -W '${paths[@]}' -- "${cur}" ) )

		# Now escape special characters (spaces, single and double quotes),
		# so that the argument is really regarded a single argument by bash.
		# See http://stackoverflow.com/questions/1146098/properly-handling-spaces-and-quotes-in-bash-completion
		local i=0
		for entry in ${COMPREPLY[*]}
		do
			if [[ "${cur:0:1}" == "'" ]]
			then
				# started with single quote, escaping only other single quotes
				# [']bla'bla"bla\bla bla --> [']bla'\''bla"bla\bla bla
				local escaped_single_quote="'\''"
				COMPREPLY[$i]="${entry//\'/${escaped_single_quote}}"
			elif [[ "${cur:0:1}" == "\"" ]]
			then
				# started with double quote, escaping all double quotes and all backslashes
				# ["]bla'bla"bla\bla bla --> ["]bla'bla\"bla\\bla bla
				entry="${entry//\\/\\\\}"
				COMPREPLY[$i]="${entry//\"/\\\"}"
			else
				# no quotes in front, escaping _everything_
				# [ ]bla'bla"bla\bla bla --> [ ]bla\'bla\"bla\\bla\ bla
				entry="${entry//\\/\\\\}"
				entry="${entry//\'/\'}"
				entry="${entry//\"/\\\"}"
				COMPREPLY[$i]="${entry// /\\ }"
			fi
			(( i++ ))
		done

		return
	fi
}

# complete the command with _kdb and fall back to filename completion
complete -o default -F _kdb kdb
