# -*- shell-script -*-

function _bashrun2 {

    COMPREPLY=()
    local cur="${COMP_WORDS[COMP_CWORD]}"
    local prev="${COMP_WORDS[COMP_CWORD-1]}"
    local sec="${COMP_WORDS[1]}"

    local options="--mode --help --version --debug --remote-control -m -h -v -d"
    local commands="show hide toggle smart exit quit reload restart su do wid pid"

    local modes=()
    local modesfile="${XDG_CACHE_HOME:-$HOME/.cache}/bashrun2/modes"

    [[ -f "$modesfile" ]] &&  modes=( $(<$modesfile) )
    modes="${modes[@]}"

    local actions=()
    local actionsfile="${XDG_CACHE_HOME:-$HOME/.cache}/bashrun2/actions"

    [[ -f "$actionsfile" ]] &&  actions=( $(<${actionsfile}) )
    actions="${actions[@]}"

    if [[ "$prev" == "bashrun"2 ]]; then
	COMPREPLY=( $(compgen -W "$options $commands" -- "${cur}") )
	return 0
    fi
    
    if [[ "$prev" =~ \-\-mode|\-m ]]; then
	    COMPREPLY=( $(compgen -W "$modes" -- "${cur}") )
	    return 0
    fi

    if [[ "$prev" =~ ${modes// /|} ]]; then
	COMPREPLY=( $(compgen -W "$commands" -- "${cur}") )
	return 0
    fi

    if [[ "$prev" =~ su|do ]]; then
	COMPREPLY=( $(compgen -W "$actions" -- "${cur}") )
	return 0
    fi

    return 0
}
complete -F _bashrun2 bashrun2
