# nmcli(1) completion                                      -*- shell-script -*-
# Originally based on
# https://github.com/GArik/bash-completion/blob/master/completions/nmcli

_nmcli_list()
{
    COMPREPLY=( $( compgen -W '$1' -- $cur ) )
}

_nmcli_con_id()
{
    local IFS=$'\n'
    COMPREPLY=( $( compgen -W "$(nmcli -t -f NAME con show c 2>/dev/null )" -- $cur ) )
}

_nmcli_con_uuid()
{
    COMPREPLY=( $( compgen -W "$(nmcli -t -f UUID con show c 2>/dev/null )" -- $cur ) )
}

_nmcli_con_path()
{
    COMPREPLY=( $( compgen -W "$(nmcli -t -f DBUS-PATH con show c 2>/dev/null )" -- $cur ) )
}

_nmcli_con_apath()
{
    COMPREPLY=( $( compgen -W "$(nmcli -t -f DBUS-PATH con show a 2>/dev/null )" -- $cur ) )
}

_nmcli_ap_ssid()
{
    local IFS=$'\n'
    COMPREPLY=( $( compgen -W "$(nmcli -t -f SSID dev wifi list 2>/dev/null )" -- $cur ) )
}

_nmcli_ab_bssid()
{
    COMPREPLY=( $( compgen -W "$(nmcli -t -f BSSID dev wifi list 2>/dev/null )" -- $cur ) )
}

_nmcli_NM_devices()
{
    COMPREPLY=( $( compgen -W "$(nmcli -t -f DEVICE dev status 2>/dev/null )" -- $cur ) )
}

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

    case $prev in
        -m|--mode)
            _nmcli_list "tabular multiline"
            return 0
            ;;
        -f|--fields)
            _nmcli_list "all common"
            return 0
            ;;
        -e|--escape)
            _nmcli_list "yes no"
            return 0
            ;;
        id)
            _nmcli_con_id
            return 0
            ;;
        uuid)
            _nmcli_con_uuid
            return 0
            ;;
        path)
            _nmcli_con_path
            return 0
            ;;
        apath)
            _nmcli_con_apath
            return 0
            ;;
        iface)
            #_available_interfaces
            _nmcli_NM_devices
            return 0
            ;;
        bssid)
            _nmcli_ab_bssid
            return 0
            ;;
        wep-key-type)
            _nmcli_list "key phrase"
            return 0
            ;;
    esac

    if [[ $cword -eq 1 ]] ; then
        if [[ "$cur" == -* ]]; then
           if [[ "$cur" == -[tpmfenavh] ]]; then
                _nmcli_list "-t -p -m -f -e -n -a -v -h"
           else
                _nmcli_list "--terse --pretty --mode --fields --escape --nocheck --ask --version --help"
           fi
        else
            _nmcli_list "general networking radio connection device"
        fi
    else
        local object=${words[1]}
        local command=${words[2]}

        [[ $command == help ]] && return 0

        case $object in
            general)
                case $command in
                    status | permissions)
                        return 0
                        ;;
                    logging)
                        _nmcli_list "level domains"
                        return 0
                        ;;
                esac

                _nmcli_list "status permissions logging help"
                ;;

            networking)
                case $command in
                    on | off)
                    return 0
                    ;;
                esac
                _nmcli_list "on off help"
                ;;

            radio)
                case $command in
                    all | wifi | mobile | wimax)
                        _nmcli_list "on off"
                        return 0
                        ;;
                esac

                _nmcli_list "all wifi mobile wimax help"
                ;;

            connection)
                case $command in
                    show)
                        local subcommand=${words[3]}

                        case $subcommand in
                            configured)
                                _nmcli_list "id uuid path"
                                return 0
                                ;;
                            active)
                                _nmcli_list "id uuid path apath"
                                return 0
                                ;;
                        esac

                        _nmcli_list "configured active"
                        return 0
                        ;;
                    up)
                        if [[ "$cur" == -* ]]; then
                            _nmcli_list "--nowait --timeout"
                        else
                            _nmcli_list "id uuid path iface ap nsp"
                        fi
                        return 0
                        ;;
                    down)
                        _nmcli_list "id uuid path apath"
                        return 0
                        ;;
                    delete)
                        _nmcli_list "id uuid path"
                        return 0
                        ;;
                esac

                _nmcli_list "show up down delete help"
                ;;

            device)
                case $command in
                    show)
                        _nmcli_NM_devices
                        return 0
                        ;;
                    disconnect)
                        if [[ "$cur" == -* ]]; then
                            _nmcli_list "--nowait --timeout"
                        else
                            _nmcli_NM_devices
                        fi
                        return 0
                        ;;
                    wifi)
                        local subcommand=${words[3]}

                        case $subcommand in
                            list)
                                _nmcli_list "iface bssid"
                                return 0
                                ;;
                            connect)
                                if [[ "$cur" == -* ]]; then
                                    _nmcli_list "--private --nowait --timeout"
                                else
                                    if [[ "$prev" == "connect" ]]; then
                                        _nmcli_ap_ssid
                                    else
                                        _nmcli_list "password wep-key-type iface bssid name"
                                    fi
                                fi
                                return 0
                                ;;
                            scan)
                                if [[ "$cur" == i* ]]; then
                                    _nmcli_list "iface"
                                else
                                    _nmcli_NM_devices
                                fi
                                return 0
                                ;;
                        esac

                        _nmcli_list "list connect scan"
                        return 0
                        ;;
                    wimax)
                        local subcommand=${words[3]}

                        case $subcommand in
                            list)
                                _nmcli_list "iface nsp"
                                return 0
                                ;;
                        esac

                        _nmcli_list "list"
                        return 0
                        ;;

                esac

                _nmcli_list "status show disconnect wifi wimax help"
                ;;
        esac

    fi

    return 0
} &&
complete -F _nmcli nmcli

# ex: ts=4 sw=4 et filetype=sh
