#compdef oneclient

local curcontext="$curcontext" state line ret=1
typeset -A opt_args


local arg_oneclient arg_fuse_helper

#
# Generation of hosts completion based on ssh config and ssh known_hosts files
# Based on http://serverfault.com/questions/170346/how-to-edit-command-completion-for-ssh-on-zsh
#
h=()
if [[ -r ~/.ssh/config ]]; then
  h=($h ${${${(@M)${(f)"$(cat ~/.ssh/config)"}:#Host *}#Host }:#*[*?]*})
fi
if [[ -r ~/.ssh/known_hosts ]]; then
  h=($h ${${${(f)"$(cat ~/.ssh/known_hosts{,2} | grep '^[a-zA-Z0-9]' || true)"}%%\ *}%%,*}) 2>/dev/null
fi
if [[ $#h -gt 0 ]]; then
  zstyle ':completion:*:ssh:*' hosts $h
  zstyle ':completion:*:slogin:*' hosts $h
  zstyle ':completion:*:hosts' hosts $h
fi

#
# Generate list of oneclient mountpoints for -u
#
onemounts=($(mount | grep fuse | awk '{print $3}'))


arg_oneclient=(\
  {-h,--help}'[Show help and exit]' \
  {-V,--version}'[Show current oneclient version and exit]' \
  {-c,--config}'[Specify path to user config file.]: :_files' \
  {-u,--unmount}'[Unmount oneclient and exit]: :{_values "Oneclient mount points" $onemounts}' \
  {-i,--insecure}'[Disable verification of server certificate, allows to connect to servers without valid certificate.]' \
  {-H,--host}'[Specify the hostname of the Oneprovider instance to which the oneclient should connect.]: :_hosts' \
  {-P,--port}'[Specify the port to which the oneclient should connect on the Oneprovider.]:_ports' \
  {-t,--token}'[Specify Onedata access token for authentication and authorization.]:token' \
  {-l,--log-path}'[Specify custom path for oneclient logs.]: :{_files -/}' \
  '--buffer-scheduler-thread-count[Specify number of parallel buffer scheduler threads.]:number' \
  '--communicator-thread-count[Specify number of parallel communicator threads.]:number' \
  '--scheduler-thread-count[Specify number of parallel scheduler threads.]:number' \
  '--storage-helper-thread-count[Specify number of parallel storage helper threads.]:number' \
  '--read-buffer-min-size[Specify minimum size in bytes of in-memory cache for input data blocks.]:number' \
  '--read-buffer-max-size[Specify maximum size in bytes of in-memory cache for input data blocks.]:number' \
  '--read-buffer-prefetch-duration[Specify read ahead period in seconds of in-memory cache for input data blocks.]:number' \
  '--write-buffer-min-size[Specify minimum size in bytes of in-memory cache for output data blocks.]:number' \
  '--write-buffer-max-size[Specify maximum size in bytes of in-memory cache for output data blocks.]:number' \
  '--write-buffer-flush-delay[Specify idle period in seconds before flush of in-memory cache for output data blocks.]:number' \
  '1: :{_files -/}'\
)

#
# OSXFuse mount options documentation extracted from:
# - https://github.com/osxfuse/osxfuse/wiki/Mount-options
#
arg_fuse_helper=(\
  {-f,--foreground}'[Foreground operation]' \
  {-d,--debug}'[Enable debug mode (implies -f)]' \
  {-s,--single-thread}'[Single-threaded operation]' \
  {-o,--opt}'[Pass mount arguments directly to OSXFuse]: :{_values -s , \
    "OSXFuse mount options" \
    "allow_other[This option overrides the security measure restricting file access to the filesystem owner, so that all users (including root) can access the files.]" \
    "allow_recursion[Allows creation of mountpoints within other osxfuse mountpoints.]" \
    "allow_root[Similar to allow_other, but only for root user.]" \
    "auto_cache[Automatically flush cache buffers on file changes.]" \
    "auto_xattr[Automatically select between using Apple Double or xattr for extended metadata.]" \
    "daemon_timeout=[Number of seconds before the OS should receive a reply to a callback.]" \
    "default_permissions[Perform permissions checks automatically based on access messages.]" \
    "defer_permissions[Skip permission checks on osxfuse level and defer them to the filesystem implementation.]" \
    "direct_io[Disable file size checks based on cached metadata.]" \
    "extended_security[Enable handling of Apple com.apple.system.Security ACLs]" \
    "fsid=[Specify 24-bit integer to be used as filesystem ID.]" \
    "fsname=[Specify custom filesystem name.]" \
    "fssubtype=[Specify custom filesystem subtype by ID.]" \
    "fstypename=[Specify custom filesystem subtype name (up to 7 characters).]" \
    "iosize=[IO size used by osxfuse when accessing the virtual storage device. Must be power of 2.]" \
    "jail_symlinks[Jail symbolic links within the mounted volume.]" \
    "kill_on_unmount[Force exit of oneclient after the volume has been unmounted.]" \
    "local[Make the virtual filesystem seem like local folder.]" \
    "negative_vncache[Enables negative vnode name caching in the kernel.]" \
    "noappledouble[Deny all types of access to Apple Double files (._).]" \
    "noapplexattr[Disable OSX extended attributes.]" \
    "nobrowse[Mark the volume as not browsable.]" \
    "nolocalcaches[Disables caching of vnodes, attributes and readaheads.]" \
    "noubc[Disables entire Unified Buffer Cache for this volume.]" \
    "ping_diskarb[osxfuse will notify Finder that a new volume has appeared.]" \
    "noping_diskarb[osxfuse will not notify Finder that a new volume has appeared.]" \
    "quiet[Suppress minor osxfuse alerts.]" \
    "volname=[Specify custom name for the volume, as will appear next to Finder icons.]" \
    }'\
)

_arguments -C -s $arg_oneclient $arg_fuse_helper \
  && ret=0

return ret
