# Bash completion for e4s-cl
# Auto-generated by scripts/generate_completion_bash.py — do not edit manually.
# To regenerate: python scripts/generate_completion_bash.py > completions/e4s-cl.bash
#
# Installation (pick one):
#   make install                               — copies to bash-completion user dir
#   source /path/to/e4s-cl/completions/e4s-cl.bash  — one-time manual setup

__e4s_cl_complete_profiles() {
    local _profiles
    _profiles=$(python3 - <<'_PY_EOF' 2>/dev/null
import json, os, pathlib
_db = (
    pathlib.Path(os.environ.get('__E4S_CL_USER_PREFIX__',
                 str(pathlib.Path.home() / '.local' / 'e4s_cl')))
    / 'user.json'
)
try:
    _d = json.loads(open(_db).read())
    print(' '.join(
        v['name']
        for v in _d.get('Profile', {}).values()
        if isinstance(v, dict) and 'name' in v
    ))
except Exception:
    pass
_PY_EOF
)
    COMPREPLY=($(compgen -W "$_profiles" -- "$1"))
}

_e4s_cl_comp() {
    local cur prev words cword
    # Use bash-completion helper if available; fall back to raw COMP_WORDS.
    _init_completion 2>/dev/null || {
        words=("${COMP_WORDS[@]}")
        cword=$COMP_CWORD
        cur="${COMP_WORDS[$COMP_CWORD]}"
        prev="${COMP_WORDS[$((COMP_CWORD-1))]}"
    }

    case "${words[1]}" in
        config)
            COMPREPLY=($(compgen -W '-h --help' -- "$cur"))
            ;;
        help)
            COMPREPLY=($(compgen -W '-h --help' -- "$cur"))
            ;;
        init)
            COMPREPLY=($(compgen -W '-h --help --launcher --launcher_args --mpi --source --image --backend --profile --wi4mpi' -- "$cur"))
            ;;
        launch)
            case "$prev" in
                --profile)
                    __e4s_cl_complete_profiles "$cur"; return ;;
                --from)
                    COMPREPLY=($(compgen -W 'intelmpi interface mpich mvapich openmpi' -- "$cur")); return ;;
            esac
            COMPREPLY=($(compgen -W '-h --help --profile --image --backend --source --files --libraries --wi4mpi --from' -- "$cur"))
            ;;
        profile)
            case "${words[2]}" in
                copy)
                    case "$prev" in
                        -@)
                            COMPREPLY=($(compgen -W 'system user' -- "$cur")); return ;;
                    esac
                    # Complete a positional profile name.
                    if [[ $cword -ge 3 && "$cur" != -* ]]; then
                        __e4s_cl_complete_profiles "$cur"; return
                    fi
                    COMPREPLY=($(compgen -W '-h --help -@' -- "$cur"))
                    ;;
                create)
                    COMPREPLY=($(compgen -W '-h --help --libraries --files --backend --image --source --wi4mpi' -- "$cur"))
                    ;;
                delete)
                    case "$prev" in
                        -@)
                            COMPREPLY=($(compgen -W 'system user' -- "$cur")); return ;;
                    esac
                    # Complete a positional profile name.
                    if [[ $cword -ge 3 && "$cur" != -* ]]; then
                        __e4s_cl_complete_profiles "$cur"; return
                    fi
                    COMPREPLY=($(compgen -W '-h --help -@' -- "$cur"))
                    ;;
                detect)
                    case "$prev" in
                        --mpi-filter)
                            COMPREPLY=($(compgen -W 'auto manual off' -- "$cur")); return ;;
                    esac
                    COMPREPLY=($(compgen -W '-h --help -p --profile --mpi-filter --exclude-lib-prefix --exclude-lib-name' -- "$cur"))
                    ;;
                diff)
                    # Complete a positional profile name.
                    if [[ $cword -ge 3 && "$cur" != -* ]]; then
                        __e4s_cl_complete_profiles "$cur"; return
                    fi
                    COMPREPLY=($(compgen -W '-h --help' -- "$cur"))
                    ;;
                dump)
                    case "$prev" in
                        -@)
                            COMPREPLY=($(compgen -W 'system user' -- "$cur")); return ;;
                    esac
                    COMPREPLY=($(compgen -W '-h --help -@' -- "$cur"))
                    ;;
                edit)
                    # Complete a positional profile name.
                    if [[ $cword -ge 3 && "$cur" != -* ]]; then
                        __e4s_cl_complete_profiles "$cur"; return
                    fi
                    COMPREPLY=($(compgen -W '-h --help --name --backend --image --source --add-files --remove-files --add-libraries --remove-libraries --wi4mpi' -- "$cur"))
                    ;;
                list)
                    case "$prev" in
                        -@)
                            COMPREPLY=($(compgen -W 'system user' -- "$cur")); return ;;
                    esac
                    COMPREPLY=($(compgen -W '-h --help -s --short -d --dashboard -l --long -@' -- "$cur"))
                    ;;
                select)
                    # Complete a positional profile name.
                    if [[ $cword -ge 3 && "$cur" != -* ]]; then
                        __e4s_cl_complete_profiles "$cur"; return
                    fi
                    COMPREPLY=($(compgen -W '-h --help' -- "$cur"))
                    ;;
                show)
                    # Complete a positional profile name.
                    if [[ $cword -ge 3 && "$cur" != -* ]]; then
                        __e4s_cl_complete_profiles "$cur"; return
                    fi
                    COMPREPLY=($(compgen -W '-h --help' -- "$cur"))
                    ;;
                unselect)
                    # Complete a positional profile name.
                    if [[ $cword -ge 3 && "$cur" != -* ]]; then
                        __e4s_cl_complete_profiles "$cur"; return
                    fi
                    COMPREPLY=($(compgen -W '-h --help' -- "$cur"))
                    ;;
                *)
                    COMPREPLY=($(compgen -W 'copy create delete detect diff dump edit list select show unselect -h --help' -- "$cur"))
                    ;;
            esac
            ;;
        *)
            COMPREPLY=($(compgen -W 'config help init launch profile -h --help -d --dry-run -v --verbose -q --quiet -V --version --print-config' -- "$cur"))
            ;;
    esac
}
complete -F _e4s_cl_comp -o bashdefault -o default e4s-cl
