# 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
#

global_options='--force-load --create --trace --quiet --file --verbose --canonical'

_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_cmd_run()
{
    vendor_scripts=$(perl -MConfig::Model::Lister -E 'my $p = $INC{"Config/Model/Lister.pm"}; $p =~ s/Lister.pm/scripts/; say $p')
    if [[ $COMP_CWORD -eq 2 ]] ; then
        scripts=$(find ~/.cme/scripts/ /etc/cme/scripts $vendor_scripts -type f ! -name '*~' -printf '%f\n' 2>/dev/null)
        COMPREPLY=( $( compgen -W "-list $scripts" -- $cur ) )
    elif [[ $prev == '-arg' ]]; then
        script=$(find  ~/.cme/scripts/ /etc/cme/scripts $vendor_scripts -type f -name ${COMP_WORDS[2]})
        var=$( perl -E 'my @v; while(<>) { push  @v, /\$(\w+)/g }; my %map = map { ($_ => 1)} @v; print map { "$_= " } sort keys %map;' $script);
        COMPREPLY=( $( compgen -W "$var" -- $cur ) )
    elif [[ $prev != '-list' ]]; then
        COMPREPLY=( $( compgen -W "--arg --doc --commit --no-commit --cat $global_options" -- $cur ) )
    fi
}

_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

    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 ) )
            ;;
           --format)
                COMPREPLY=( $( compgen -W 'yaml json perl cml' -- $cur ) )
            ;;
           --model-dir|-root-dir|-fuse-dir)
                _filedir -d
            ;;
           --file)
                _filedir
            ;;
             *)
            case ${COMP_WORDS[1]} in
                check)
                   COMPREPLY=( $( compgen -W "$global_options --strict" -- $cur ) )
                ;;
                dump)
                   COMPREPLY=( $( compgen -W "$global_options --dumptype --format" -- $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 --format" -- $cur ) )
                ;;
                run)
                   COMPREPLY=( $( compgen -W "$global_options --doc --cat --no-commit" -- $cur ) )
				;;
                search)
                   COMPREPLY=( $( compgen -W "$global_options --search --narrow-search" -- $cur ) )
				;;
            esac
        esac
    fi
}
complete -F _cme cme