#!/usr/bin/env bash


# make zsh emulate bash if necessary
if [[ -n "$ZSH_VERSION" ]]; then
    autoload bashcompinit
    bashcompinit
fi


# autocompletion
_goto_completions() 
{
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    opts="--verbose --force --open"

    if [[ "$prev" =~ ^(goto|show|copy|cd|rm|update|mv|rename|open|-o|--open|subl)$ ]]; then
        COMPREPLY=( $(compgen -W "$(goto list)" ${cur}) )
        return 0
    fi

    # autocomplete second magicwords in goto rename.
    # only useful if the user specifies the --force or -f option/flag.
    if [[ $COMP_CWORD -ge 3 && "${COMP_WORDS[1]}" =~ ^(mv|rename)$ ]]; then
        COMPREPLY=( $(compgen -W "$(goto list)" ${cur}) )
        return 0
    fi

    if [[ ${cur} == --* ]] ; then
            COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        return 0
    fi
}
complete -o default -F _goto_completions goto


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

    if [[ ${prev} == "project" || ${prev} == "rm" ]]; then 
        COMPREPLY=( $(compgen -W "$(project list)" ${cur}) )
        return 0
    fi
}
complete -F _project_completions project


#make it possible to cd
function goto {
	# jump around in browser or in terminal
        if [ -n "$(command -v goto)" ]; then
    	    source goto $@
        else
            echo "Error - goto command not found (exit code: $?)" 
        fi
}
