# cme(1) completion -*- shell-script -*-
#
#
# This file is part of App::Cmd::Cme
#
# This software is Copyright (c) 2011, 2014 by Dominique Dumont
#
# This is free software, licensed under:
#
#   The GNU Lesser General Public License, Version 2.1, February 1999
#

# New style of Debian bash completion use load on demand of
# files. Unfortunately, cme plugins come with bash completions
# snippets that are not loaded with this mechanism. Hence, these lines
# circumvent delayed for the bash completions of cme plugins
for file in /usr/share/bash-completion/completions/cme[-_]*
do
    . $file
done

_cme_models()
{
   MODELS=$(perl -MConfig::Model::Lister -e'print Config::Model::Lister::models;')
   COMPREPLY=( $( compgen -W "$MODELS" -- $cur ) )
}

_cme_appli()
{
   MODELS=$(perl -MConfig::Model::Lister -e'print Config::Model::Lister::applications;')
   COMPREPLY=( $( compgen -W "$MODELS" -- $cur ) )
}

_cme_commands()
{
    # use perl so that plugged in subcommand (like meta) are listed
    SUBCMDS=$(perl -MApp::Cme -e'print join("\n", grep {not /-/} App::Cme->new->command_names);')
    COMPREPLY=( $( compgen -W "$SUBCMDS" -- $cur ) )
}

_cme_handle_app_arg()
{
	[[ $COMP_CWORD -eq 3 ]] && _cme_${COMP_WORDS[2]} 2> /dev/null ;
}

_cme_handle_cmd_arg()
{
	[[ $COMP_CWORD -ge 2 ]] && _cme_cmd_${COMP_WORDS[1]} 2> /dev/null ;
}

_cme()
{
    local cur

    COMPREPLY=()
    _get_comp_words_by_ref -n : cur prev

    global_options='-dev -force-load -create -backend -trace -quiet -file'

    if [[ $COMP_CWORD -eq 1 ]] ; then
        _cme_commands
    elif _cme_handle_cmd_arg; then
        TRASH=1; # bash does not support empty then/elif
    elif [[ $COMP_CWORD -eq 2 ]] ; then
        _cme_appli
    elif ! _cme_handle_app_arg; then
        case $prev in
            -ui)
                COMPREPLY=( $( compgen -W 'tk curses shell' -- $cur ) )
            ;;
           -dumptype)
                COMPREPLY=( $( compgen -W 'full preset custom' -- $cur ) )
            ;;
           -model-dir|-root-dir|-fuse-dir)
                _filedir -d
            ;;
             *)
            case ${COMP_WORDS[1]} in
                check)
                   COMPREPLY=( $( compgen -W "$global_options -strict" -- $cur ) )
                ;;
                dump)
                   COMPREPLY=( $( compgen -W "$global_options -dumptype" -- $cur ) )
                ;;
                edit)
                   COMPREPLY=( $( compgen -W "$global_options -ui -open-item" -- $cur ) )
                ;;
                fix)
                   COMPREPLY=( $( compgen -W "$global_options -from -filter" -- $cur ) )
                ;;
                fusefs)
                   COMPREPLY=( $( compgen -W "$global_options -fuse-dir -dfuse -dir-char" -- $cur ) )
                ;;
                migrate)
                   COMPREPLY=( $( compgen -W "$global_options" -- $cur ) )
                ;;
                update)
                   COMPREPLY=( $( compgen -W "$global_options" -- $cur ) )
                ;;
                # modify completion could be much more elaborate...
                modify)
                   COMPREPLY=( $( compgen -W "$global_options -save -backup" -- $cur ) )
                ;;
                search)
                   COMPREPLY=( $( compgen -W "$global_options -search -narrow-search" -- $cur ) )
				;;
				edit)
                   COMPREPLY=( $( compgen -W "$global_options -open-item" -- $cur ) )
                ;;
            esac
        esac
    fi
}
complete -F _cme cme
