#compdef facl
# zsh completion for facl(1). Install as _facl somewhere in $fpath
# (e.g. /usr/local/share/zsh/site-functions/_facl).

_facl_templates() {
  local -a names
  names=(${(f)"$(facl -templates 2>/dev/null | awk 'NR > 1 { print "@" $1 }')"})
  (( $#names )) && _describe -t templates 'saved template' names
}

_facl_set() {
  _facl_templates
  _message -e acl 'ACL in setfacl(1) syntax (user:NAME:perms:flags:allow,...) or @TEMPLATE'
}

_facl_principal() {
  if compset -P 'user:'; then
    _users
  elif compset -P 'group:'; then
    _groups
  else
    compadd -S '' -- 'user:' 'group:'
    _users
  fi
}

_facl_chown() {
  if compset -P '*:'; then
    _groups
  else
    _users -S ':' -q
  fi
}

_facl_template_names() {
  local -a names
  names=(${(f)"$(facl -templates 2>/dev/null | awk 'NR > 1 { print $1 }')"})
  (( $#names )) && _describe -t templates 'saved template' names
}

_arguments -s \
  '-version[print version and exit]' \
  '-mouse[enable mouse support in the TUI (m toggles it at runtime)]' \
  '-fs[assume these file-system semantics (NFS client mounts)]:semantics:(zfs ufs zfs\:restricted zfs\:passthrough zfs\:passthrough-x zfs\:noallow zfs\:discard)' \
  '-set[ACL to apply, non-interactively (setfacl syntax, @TEMPLATE, @file\:PATH or @-)]:ACL:_facl_set' \
  '-from[apply the ACL of this path]:model path:_files' \
  '-add[insert entries into each path'"'"'s own ACL]:entries (setfacl syntax):' \
  '-remove[remove entries from each path'"'"'s own ACL (indexes, principals or entries)]:specs:' \
  '-modify[replace/add entries in each path'"'"'s own ACL (setfacl -m style)]:entries (setfacl syntax):' \
  '-at[index at which -add/-modify insert (0 = first)]:index:' \
  '-check[dry run, exit 3 if anything would change]' \
  '-where[list objects whose ACL names a principal]:principal:_facl_principal' \
  '-nontrivial[list objects with a non-trivial ACL]' \
  '-dump[JSON snapshot of every object under the paths]' \
  '-restore[bring ACLs back to a -dump snapshot]:snapshot file:_files' \
  '-save[save the ACL of the first path as a template]:template name:' \
  '-show[print a saved template]:template name:_facl_template_names' \
  '-delete-template[delete a saved template]:template name:_facl_template_names' \
  '-templates[list saved templates]' \
  '-audit[report inheritance drift under each directory (exit 3 if any)]' \
  '-fix[with -audit: repair the drift by propagating]' \
  '-scope[with -set, on directories]:scope:(this propagate replace)' \
  '-n[dry run: print the commands (or the current ACLs) and change nothing]' \
  '-y[with -set: apply without asking]' \
  '-effective[print the effective access of users/groups (comma-separated)]:principal:_facl_principal' \
  '-chmod[print the ACL a chmod to MODE would leave behind]:mode (octal or symbolic, e.g. 750, u+rwX):' \
  '-chown[change ownership (USER, :GROUP or USER:GROUP)]:owner:_facl_chown' \
  '-R[with -chown: also change the ownership of all descendants]' \
  '-json[JSON output for -n, -effective, -audit, -where and -nontrivial]' \
  '*:path:_files'
