# Let bash complete your instancemanager command.
#
# On Debian-like systems:
# - Put this e.g. in /etc/bash_completion.d/ (possibly with a symlink)
# - Uncomment some lines in /etc/bash.bashrc that read the
#   /etc/bash_completion file
# This takes effect the next time you start bash

_instancemanager()
{
    local cur prev
    COMMAND_NAME='instancemanager'

    COMPREPLY=()
    # Word that is currently being expanded:
    cur=${COMP_WORDS[COMP_CWORD]}
    # Previous expanded word:
    prev=${COMP_WORDS[COMP_CWORD-1]}

    CONFIGDIR=~/.instancemanager
    if test ! -d $CONFIGDIR; then
        return 0
    fi
    PROJECT_ALL='ALL'
    PROJECTS="$PROJECT_ALL $(find $CONFIGDIR/*.py -printf '%f\n' \
        | grep -v __init__.py | grep -v userdefaults.py \
        | sed "s/.py$//")"

    ACTIONS='backup create datafs fg fresh printconfig products
    restart restore soft start stop'

    if test $prev = $COMMAND_NAME; then
        COMPREPLY=( $(compgen -W '$PROJECTS' -- $cur ) )
    else        
        COMPREPLY=( $(compgen -W '$ACTIONS' -- $cur ) )
    fi
}
complete -F _instancemanager instancemanager
