#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}'))
if [ $onemounts[(I)$onemounts[-1]] -eq 0 ]; then
  onemounts=""
fi

arg_oneclient=(\
  {-h,--help}'[Show help and exit]' \
  {-V,--version}'[Show current oneclient version and exit]' \
  {-v,--verbose-log-level}'[Specify the log verbosity level]' \
  {-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 -/}' \
  '--no-buffer[Disable in-memory cache for input/output data blocks]' \
  '--provider-timeout[Specify Oneprovider connection timeout in seconds.]:number' \
  '--disable-read-events[Disable reporting of file read events]' \
  '--force-fullblock-read[Force fullblock read mode.]' \
  '--force-proxy-io[Force proxied access to storage via Oneprovider for all spaces]' \
  '--force-direct-io[Force direct access to storage for all spaces]' \
  '--buffer-scheduler-thread-count[Specify number of parallel buffer scheduler threads.]:number' \
  '--communicator-pool-size[Specify number of connections in communicator pool.]: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' \
  '--seqrd-prefetch-threshold[Specify linear read prefetch threshold]:number' \
  '--rndrd-prefetch-threshold[Specify random read prefetch threshold]:number' \
  '--metadata-cache-size[Specify maximum number of entries in file metadata cache.]:number' \
  '--io-trace-log[Enable detailed IO trace log.]' \
  '--disable-read-events[Disable reporting of file read events.]' \
  '--no-fullblock-read[Disable fullblock read mode.]' \
  '--rndrd-prefetch-eval-frequency[Number of reads from single file handle which will be skipped before next evaluation of cluster prefetch.]:number' \
  '--rndrd-prefetch-block-threshold[Number of separate blocks after which replication for the file is triggered automatically.]:number' \
  '--rndrd-prefetch-cluster-window[Cluster window size for prefetching in bytes.]:number' \
  '--rndrd-prefetch-cluster-block-threshold[Number of separate blocks in a cluster window around current read.]:number' \
  '--rndrd-prefetch-cluster-window-grow-factor[Prefetch cluster window grow factor.]:number' \
  '--prefetch-mode[Defines the type of block prefetch mode.]:mode' \
  '--cluster-prefetch-threshold-random[Enables random cluster prefetch threshold selection.]' \
  '--readdir-prefetch-size[Specify the size of requests made during readdir prefetch.]:number' \
  '--tag-on-create[Adds name=value extended attribute to each locally created file.]:value' \
  '--tag-on-modify[Adds name=value extended attribute to each locally modified file.]:value' \
  '--space[Allows to specify which space should be mounted by name.]:space' \
  '--space-id[Allows to specify which space should be mounted by space id.]:space' \
  '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
