
_common_scheme ()
{
    local cur prev comm

    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}
    prev=${COMP_WORDS[COMP_CWORD-1]}
    comm=${COMP_WORDS[1]}

    # options completion
    if [[ "$cur" == -* ]]; then
        COMPREPLY=( $( compgen -W '-f -k -n -r -s -v \
                --force-build --keep-files --no-action --silent \
                --recursive --verbose --all --chicken --gambit \
                --gauche --guile --sisc' -- $cur ) )
        return 0
    fi

    # command specific completion
    case "$comm" in
        build|install|script)
            COMPREPLY=( $( compgen -f -X '!*.scm' -- $cur ) \
                        $( compgen -W '$( common-scheme search | sed -e "s/ -.*//" )' -- $cur ) )
            return 0
            ;;
        remove|upgrade)
            COMPREPLY=( $( compgen -W '$( common-scheme list )' -- $cur ) )
            return 0
            ;;
    esac

    # default
    COMPREPLY=( $( compgen -W 'build help index install list remove script search show update upgrade' -- $cur ) )
    return 0

}

complete -F _common_scheme -o default common-scheme
