# OpenIndiana dladm(1M) completion                          -*- shell-script -*-
# ------------------------------------------------------------------------------
# Copyright (c) 2012, Rob Gulewich <robert.gulewich@joyent.com>
# Copyright (c) 2018, Michal Nowak <mnowak@startmail.com>

_dladm()
{
    local cur prev
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    if [[ ${prev} == 'dladm' ]]; then
      local cmds="$(dladm 2>&1 | awk '/^ / { print $1 }')"
      COMPREPLY=( $(compgen -W "${cmds}" -- ${cur}) )
    elif [[ ${prev} =~ "-z" ]]; then
      # Some illumos OS distributions have zone-aware dladm. Treat -z as a zone name option
      local zones="$(zoneadm list -c | grep -v '^global$')"
      COMPREPLY=( $(compgen -W "${zones}" -- ${cur}) )
    elif [[ ${prev} =~ 'delete-vnic' ]]; then
      # Redirect stderr to /dev/null not to polute console, e.g.:
      #   dladm: could not open /dev/dld: object not found
      local vnics="$(dladm show-vnic -p -o link 2> /dev/null)"
      COMPREPLY=( $(compgen -W "${vnics}" -- ${cur}) )
    elif [[ ${prev} =~ 'delete-etherstub' ]]; then
      # Redirect stderr to /dev/null not to polute console, e.g.:
      #   dladm: could not open /dev/dld: object not found
      local stubs="$(dladm show-etherstub -p 2> /dev/null)"
      COMPREPLY=( $(compgen -W "${stubs}" -- ${cur}) )
    fi
}

complete -F _dladm dladm

# ex: filetype=sh
# vim: tabstop=2 shiftwidth=2 expandtab smartindent
