#compdef zpool-set
# zsh completion for zpool-set(1). Install in a directory of $fpath (e.g.
# /usr/local/share/zsh/site-functions/_zpool-set).

_zpool_set_pools() {
  local -a pools
  pools=(${(f)"$(zpool list -H -o name 2>/dev/null)"})
  _describe -t pools 'pool' pools
}

_zpool_set_props() {
  local -a props
  props=(${(f)"$(zpool-set -catalogue 2>/dev/null | awk '/^  [a-z]/ { print $1 }' | grep -v '@$')"})
  _describe -t properties 'property' props
}

_zpool_set_features() {
  local -a feats
  feats=(${(f)"$(zpool-set -catalogue 2>/dev/null | awk '/^  feature@/ { sub(/^feature@/, "", $1); print $1 }')"})
  _describe -t features 'feature' feats
}

_zpool_set_assign() {
  if compset -P 1 '*='; then
    local prop=${IPREFIX%=}
    local -a vals
    vals=(${(f)"$(zpool-set -describe $prop 2>/dev/null | awk '/^    [a-z0-9(]/ { print $1 }' | grep -v '^(empty)$')"})
    if (( ${#vals} )); then
      _describe -t values 'value' vals
    else
      _message 'value'
    fi
  else
    local -a props
    props=(${(f)"$(zpool-set -catalogue 2>/dev/null | awk '/ settable / { print $1 }' | grep -v '@$')"})
    _describe -t properties 'property' props -S =
  fi
}

_zpool_set_vdevs() {
  local -a vdevs pool
  pool=(${line[1]:-$(zpool list -H -o name 2>/dev/null | head -1)})
  vdevs=(root all ${(f)"$(zpool-set -vdevs $pool 2>/dev/null | awk 'NR > 1 { print $1 }')"})
  _describe -t vdevs 'vdev' vdevs
}

_arguments -s \
  '-version[print version and exit]' \
  '-mouse[interactive: start with mouse support]' \
  '-features=[interactive: show the features (false: hidden)]:bool:(true false)' \
  '-color=[interactive: colour output]:bool:(true false)' \
  '*-vdev[the vdev whose properties -list/-get/-set/-reset mean]:vdev:_zpool_set_vdevs' \
  '-vdevs[print the vdev tree of the pool]' \
  '-list[print every property with value, source and meaning]' \
  '-local[with -list: only the properties set on the pool]' \
  '-get[print the named properties]:property:_sequence _zpool_set_props' \
  '*-set[set PROP=VALUE]:assignment:_zpool_set_assign' \
  '*-reset[reset PROP to its default / remove a user property]:property:_zpool_set_props' \
  '*-enable[enable a pool feature]:feature:_zpool_set_features' \
  '-where[the property on every pool]:property:_zpool_set_props' \
  '-dump[JSON snapshot of the properties set on the pool]' \
  '-restore[restore a -dump snapshot]:file:_files' \
  '-catalogue[print the property catalogue and the features]' \
  '-describe[describe one property or feature]:property:_zpool_set_props' \
  '-n[dry run: print the zpool commands]' \
  '-y[apply without asking]' \
  '-check[dry run, exit 3 if anything would change]' \
  '-json[JSON output]' \
  '-lenient[accept unknown property names]' \
  '1:pool:_zpool_set_pools'
