# ngcpcfg(8) completion                                     -*- shell-script -*-

_ngcpcfg()
{
    local cur prev words cword
    _init_completion || return

    case $prev in
        apply)
            if [[ "$cur" == -* ]]; then
                COMPREPLY=( $( compgen -W '--force-all-services' -- "$cur" ) )
            fi
            return
            ;;
        build)
            cur=${cur:-/etc/}
            _filedir
            return
            ;;
        cat)
            COMPREPLY=( $( compgen -W 'config network constants' -- "$cur" ) )
            return
            ;;
        check)
            if [[ "$cur" == -* ]]; then
                COMPREPLY=( $( compgen -W '--ignore-branch-check --ignore-shared-storage-check' -- "$cur" ) )
            else
                [[ -n "$cur" ]] && _filedir yml && return 0
                COMPREPLY=( $( compgen -G "/etc/ngcp-config/*.yml" -- "$cur" ) ) || _filedir yml
                compopt -o nospace
            fi
            return
            ;;
        clean)
            if [[ "$cur" == -* ]]; then
                local -a opts=(
                  --all
                  --branches
                  --force
                  --help
                  --reset-master
                  --stashes
                  --tracked-files
                  --untracked-files
                )
                COMPREPLY=( $( compgen -W '${opts[@]}' -- "$cur" ) )
            else
                _filedir
                compopt +o nospace
            fi
            return
            ;;
        commit)
            return
            ;;
        del)
            if [[ "$cur" == -* ]]; then
                COMPREPLY=( $( compgen -W '--diff' -- "$cur" ) )
            fi
            _filedir
            compopt +o nospace
            return
            ;;
        diff)
            if [[ "$cur" == -* ]]; then
                if [ -r /usr/share/bash-completion/completions/git ] ; then
                    unset __git_diff_common_options
                    . /usr/share/bash-completion/completions/git
                    COMPREPLY=( $( compgen -W "$__git_diff_common_options" -- "$cur" ) )
                fi
            else
                [[ -n "$cur" ]] && _filedir yml && return 0
                compopt -o nospace
            fi
            return
            ;;
        edit)
            cur=${cur:-/etc/ngcp-config/}
            _filedir
            return
            ;;
        get)
            # XXX - would be nice to get completion based on content of /etc/ngcp-config/*.yml
            return
            ;;
        help)
            return
            ;;
        log)
            if [[ "$cur" == -* ]]; then
                COMPREPLY=( $( compgen -W '-p' -- "$cur" ) )
            fi
            return
            ;;
        patch)
            if [[ "$cur" == -* ]]; then
                COMPREPLY=( $( compgen -W '--help --from-customtt' -- "$cur" ) )
            else
                _filedir
                compopt +o nospace
            fi
            return
            ;;
        post-build)
            return
            ;;
        pull)
            return
            ;;
        push|push-parallel)
            if [[ "$cur" == -* ]]; then
                COMPREPLY=( $( compgen -W '--nobuild --noapply --shared-only' -- "$cur" ) )
            else
                COMPREPLY=( $( compgen -W 'all' -- "$cur" ) )  # always provide 'all'
                _known_hosts_real -- "$cur"  # complete SSH known_hosts, /etc/hosts,...
            fi
            return
            ;;
        services)
            return
            ;;
        set)
            [[ -n "$cur" ]] && _filedir yml && return 0
            COMPREPLY=( $( compgen -G "/etc/ngcp-config/*.yml" -- "$cur" ) ) || _filedir yml
            compopt -o nospace
            return 0
            ;;
        show)
            COMPREPLY=( $( compgen -W "$(cd /etc/ngcp-config && git rev-list master --abbrev-commit 2>/dev/null)" -- "$cur" ) )
            return
            ;;
        status)
            COMPREPLY=( $( compgen -W '--local-only' -- "$cur" ) )
            return
            ;;
        version)
            return
            ;;
    esac

    if [[ "$cur" == -* ]]; then
        COMPREPLY=( $( compgen -W '--debug --validate --version' -- "$cur" ) )
    else
        if [[ $cword -eq 1 ]]; then
            local -a scripts=( help version )  # those options don't have separate ngcpcfg scripts
            for f in /usr/share/ngcp-ngcpcfg/scripts/* ; do
                case "$(basename "$f")" in
                    decrypt|encrypt|etckeeper|initialise|init-shared)  # not relevant for common operations
                        ;;
                    values)   # deprecated options
                        ;;
                    *)
                        scripts+=( $(basename "$f") )
                        ;;
                esac
            done
            COMPREPLY=( $( compgen -W '${scripts[@]}' -- "$cur" ) )
        fi
    fi
} &&
complete -F _ngcpcfg ngcpcfg ngcp-config

# ex: filetype=bash
