#compdef mount umount

if [[ "$OSTYPE" == cygwin ]]; then
  if [[ "$service" == mount ]] ; then
    _arguments -s \
      - mount \
	'(-b -t --text --binary)'{-b,--binary}'[Unix line endings LF]' \
	'(-f --force)'{-f,--force}'[be silent]' \
	'(-s -u --user --system)'{-s,--system}'[system-wide mount point]' \
	'(-t -b --binary --text)'{-t,--text}'[DOS line endings CR-LF]' \
	'(-u -s --system --user)'{-u,--user}'[user private mount point]' \
	'(-x -X --executable --cygwin-executable)'{-x,--executable}'[all files under mountpoint are executables]' \
	'(-x -X --executable --cygwin-executable)'{-X,--cygwin-executable}'[all files under mountpoint are cygwin executables]' \
	':Windows path:' \
	':Unix path:_path_files -P/ -W "(/)" -/' \
      - control \
	'(-)'{-i,--import-old-mounts}'[import old mounts]' \
	'(-)'{-p,--show-cygdrive-prefix}'[show cygdrive prefix]' \
	'(-)'{-c,--change-cygdrive-prefix}'[cygdrive prefix]:cygdrive prefix (POSIX path):_files -P/ -W "(/)" -/' \

    return
  else
    local line
    local -a wpaths upaths
    mount | while read -r line; do
      [[ $line == ?:\ * ]] && continue
      wpaths+=( ${line%% on*} )
      upaths+=( ${${line##*on }%% type*} )
    done
    _alternative \
      'windowspath:Windows path:compadd -a wpaths' \
      'unixpath:Unix path:compadd -a upaths'
    return
  fi
fi

# This is table-driven: the tables for the values for the different
# file system types are directly below. The tables describing the
# arguments for the `mount' command for different operating systems
# are below these table.

local curcontext="$curcontext" state line expl suf ret=1
local args deffs=iso9660 tmp typeops=-t _nfs_access _fs_nfs _nfs_ufs \
_fs_ufs _fs_efs _fs_cd9660 _fs_iso9660 _fs_cachefs _fs_s5fs _fs_tmpfs _fs_pcfs \
_fs_hsfs _fs_advfs _fs_cdfs _fs_affs _fs_ext2 _fs_fat _fs_ext3 _fs_msdos \
_fs_msdosfs _fs_umsdos _fs_vfat _fs_hpfs _fs_ntfs _fs_reiserfs _fs_smbfs \
_fs_xfs _fs_std _fs_devfs _fs_fdesc _fs_kernfs _fs_linprocfs _fs_linsysfs \
_fs_procfs _fs_btrfs _fs_ext4

typeset -A opt_args

if (( ! $+_fs_any )); then

  local _fs_any

  # These are tables describing the possible values and their
  # arguments for the `-o' option. There is one array per
  # file system type (only for those that accept more values
  # than those in the `_fs_any' array). The elements of the
  # array are used as arguments to `_values'. The first tables
  # are used by several systems while system specific tables are
  # (re)defined in a "$OSTYPE" case.

  _fs_any=(
    '(rw)ro[mount file system read-only]'
    '(ro)rw[mount file system read-write]'
  )
  _nfs_access=(
    'acregmin[specify cached file attributes minimum hold time]:cached file attributes minimum hold time'
    'acregmax[specify cached file attributes maximum hold time]:cached file attributes maximum hold time'
    'acdirmin[specify cached directory attributes minimum hold time]:cached directory attributes minimum hold time'
    'acdirmax[specify cached directory attributes maximum hold time]:cached directory attributes maximum hold time'
    "actimeo[set all attributes' cache timeout]:cache timeout"
  )
  _fs_nfs=(
    'rsize[specify read buffer size]:read buffer size:(8192)'
    'wsize[specify write buffer size]:write buffer size:(8192)'
    '(soft)hard[hang process on server crash (so to say)]'
    '(hard)soft[allow time out on server crash]'
    '(nolock)lock[use locking]'
    "(lock)nolock[don't use locking]"
    'timeo[specify initial timeout for UDP]:initial timeout for UDP'
    'retrans[set number of NFS retransmissions]:number of NFS retransmissions'
    'retry[specify number of mount failure retries]:mount failure retries'
    '(fg)bg[retry in the background]'
    '(bg)fg[retry in the foreground]'
    '(nintr)intr[allow operations to be interrupted]'
    '(intr)nintr[prevent operations from being interrupted]'
    '(nfsv3)nfsv2[use NFS version 2]'
    '(nfsv2)nfsv3[use NFS version 3]'
    'proto[specify protocol]:protocol:(udp tcp)'
    'port[specify server port number]:server port number'
    'proplist[allow property lists]'
    "$_nfs_access[@]"
    "nocto[don't get fresh attributes on open]"
    "noac[don't set attribute caching]"
  )
  _nfs_ufs=(
    '(nodev)dev[interpret devices]'
    "(dev)nodev[don't interpret devices]"
    '(nosuid)suid[use suid and sgid bits]'
    '(suid)nosuid[ignore suid and sgid bits]'
    '(nosync)sync[do I/O synchronously]'
    '(sync)nosync[do all I/O asynchronously]'
    '(noexec)exec[permit execution of binaries]'
    "(exec)noexec[don't allow execution of binaries]"
    '(nogrpid)grpid[new file gets group ID of directory]'
    '(grpid)nogrpid[new file gets fsgid of current process]'
  )
  _fs_ufs=(
    'dirty[allow mount even if not cleanly unmounted]'
    "$_nfs_ufs[@]"
  )

  case "$OSTYPE" in
  aix*)
    _fs_any=(
      'bsy[prevent mount over cwd of process]'
      'log[logical volume to log operations]:logical volume name'
      'nodev[cannot open devices from mount]'
      'nosuid[prevent running setuid/setgid from mount]'
      "$_fs_any[@]"
    )

    _fs_nfs=(
      '(fg)bg[mount in background]'
      '(bg)fg[mount in foreground]'
      'vers[NFS version]:NFS version:(2 3)'
      'proto[specify transport protocol]'
      'retry[number of retries]:no. of retries'
      'rsize[read buffer size]:read buffer size'
      'wsize[write buffer size]:write buffer size'
      'llock[local locking]'
      'timeo[time-out period]:time-out period'
      'retrans[NFS transmissions]:no. of NFS transmissions'
      'port[IP port no]:IP port no'
      '(hard)soft[error on no server response]'
      '(soft)hard[retry pending server response]'
      '(nointr)intr[allow keyboard interrupts on hard mount]'
      '(intr)nointr[disallow keyboard interrupts on hard mount]'
      'posix[exchange pathconf info on NFS version 2 mount]'
      'secure[use DES encryption]'
      'grpid[inherit group id of parent directory]'
      "(acl)noacl[don't use access control lists]"
      '(noacl)acl[use access control lists for this mount]'
      'noac[no attribute or directory caching]'
      'shortdev[server lack support for 32-bit device special files]'
      "$_nfs_access[@]"
    )
    ;;
  irix*)
    _fs_any=(
      'nodev[cannot open devices from mount]'
      'nosuid[prevent running setuid/setgid from mount]'
      'grpid[inherit group id of parent directory]'
      "$_fs_any[@]"
    )

    _fs_efs=(
      'raw[raw device pathname to filesystem]:raw device pathname:->devordir'
      '(nofsck)fsck[fsck should check this filesystem by default]'
      '(fsck)nofsck[fsck should not check this filesystem by default]'
      '(noquota)quota[turn on quotas]'
      '(quota)noquota[turn off quotas]'
      'lbsize[no of bytes transferred in each operation]:bytes'
    )

    _fs_iso9660=(
      'setx[set execute permissions on every file]'
      'notranslate[don'\''t translate filenames]'
      'cache[no of 2048 blocks for directory cache]:cache size'
      'noext[ignore rock ridge extensions]'
      '(nosusp)susp[enable system use sharing protocol]'
      '(susp)nosusp[disable system use sharing protocol]'
      '(norrip)rrip[enable rock ridge extensions]'
      '(rrip)norrip[disable rock ridge extensions]'
      'nmconv[specify filename translation]:filename translation:((c\:no\ translation l\:to\ lowercase m\:suppress\ version\ no))'
    )

    _fs_nfs=(
      '(fg)bg[mount in background]'
      '(bg)fg[mount in foreground]'
      'retry[number of retries]:no. of retries'
      'rsize[read buffer size]:read buffer size'
      'wsize[write buffer size]:write buffer size'
      'timeo[time-out period]:time-out period'
      'retrans[NFS transmissions]:no. of NFS transmissions'
      'port[IP port no]:IP port no'
      '(hard)soft[error on no server response]'
      '(soft)hard[retry pending server response]'
      'intr[allow keyboard interrupts on hard mount]'
      'noac[no attribute caching]'
      'private[use local locking and do not flush on last close]'
      'symttl[time-to-live of cached symbolic links]:seconds'
      "$_nfs_access[@]"
    )

    _fs_cachefs=(
      'backfstype[type of the back file system]:back file system type:(efs nfs iso9660 dos hfs cachefs)'
      'backpath[specify back file system location]:back file system location:_files -/'
      'cachedir[name of the cache directory]:name of cache directory:_files -/'
      'cacheid[cache ID]:cache ID'
      '(write-around)non-shared[cache not shared]'
      '(non-shared)write-around[cache shared]'
      'noconst[disable consistency checking]'
      'local-access[check permissions locally]'
      'purge[purge any cached information]'
      "$_nfs_access[@]"
    )

    ;;
  solaris*)
    _fs_s5fs=(
      'remount[remount file system]'
      '(suid)nosuid[ignore suid and sgid bits]'
      '(nosuid)suid[use suid and sgid bits]'
    )
    _fs_ufs=(
      "(atime)noatime[don't update access time]"
      'f[fake an /etc/mnttab entry]'
      "m[don't make an /etc/mnttab entry]"
      '(noforcedirection)forcedirection[do I/O synchronously]'
      '(forcedirection)noforcedirection[do all I/O asynchronously]'
      '(nointr)intr[allow operations to be interrupted]'
      '(intr)nointr[prevent operations from being interrupted]'
      "(nolargefiles)largefiles[don't restrict file size]"
      '(largefiles)nolargefiles[restrict file size]'
      '(nologging)logging[log transactions]'
      '(logging)nologging[log transactions]'
      'onerror[action to recover from error]:action:(panic lock umount)'
      'quota[turn on quotas]'
      '(ro rw)rq[mount file system read-write with quotas]'
      "$_fs_s5fs[@]"
    )
    _fs_tmpfs=(
      'size[set file system size]:size'
    )
    _fs_pcfs=(
      '(nofoldcase)foldcase[force filenames to lowercase]'
      "(foldcase)nofoldcase[don't force filenames to lowercase]"
    )
    _fs_hsfs=(
      'nrr[no rock ridge]'
      'notraildot[no trail dot when no extension]'
      "nomaplcase[don't force lowercase]"
      'nosuid[ignore suid and sgid bits]'
    )
    _fs_nfs=(
      '(fg)bg[retry in the background]'
      '(bg)fg[retry in the foreground]'
      '(nogrpid)grpid[new file gets group ID of directory]'
      '(grpid)nogrpid[new file gets fsgid of current process]'
      '(nointr)intr[allow operations to be interrupted]'
      '(intr)nointr[prevent operations from being interrupted]'
      '(sec secure)kerberos[use kerberos authentication]'
      'noac[no attribute caching]'
      'port[server IP port number]:port'
      'posix[posix semantics]'
      'proto[specify protocol]:protocol:'\("$(grep -v '^#' /etc/netconfig 2> /dev/null | cut -d ' ' -f 1)"\)
      'public[force public file handle]'
      '(noquota)quota[enable quotas]'
      '(quota)noquota[disable quotas]'
      'timeo[specify initial timeout for UDP]:initial timeout for UDP'
      'retrans[set number of NFS retransmissions]:number of NFS retransmissions:(5)'
      'retry[specify number of mount failure retries]:mount failure retries:(10000)'
      'rsize[specify read buffer size]:read buffer size:(8192)'
      'wsize[specify write buffer size]:write buffer size:(8192)'
      '(kerberos secure)sec[set the security mode]:security mode:(sys dh krb4 none)'
      '(kerberos sec)secure[use Diffie-Hellman public key system]'
      'vers[set NFS version number]:NFS version number:(2 3)'
      "$_nfs_access[@]"
      "$_fs_s5fs[@]"
    )
    _fs_cachefs=(
      'backfstype[type of the back file system]:back file system type:(nfs hsfs)'
      'backpath[specify back file system location]:back file system location:_files -/'
      'cacheid[specify a cache ID]:cache ID'
      'local-access[check permissions locally]'
      'noconst[disable cache consistency checking]'
      'purge[purge any cached information]'
      '(suid)nosuid[ignore setuid and setgid bits]'
      '(nosuid)suid[use setuid and setgid bits]'
      '(write-around)non-shared[purge cache on writes]'
      '(non-shared)write-around[keep file in cache on writes]'
      "$_nfs_access[@]"
    )
    ;;
  osf*)
    _fs_advfs=(
      '(ro rw)rq[mount file system read-write]'
      'dual[allow mount even if same domain ID as already mounted domain]'
      'sync[do I/O synchronously]'
    )
    _fs_cdfs=(
      '(nodefperm)defperm[ignore permission bits]'
      '(defperm)defperm[use permission bits]'
      'noversion[strip off version extension]'
      'rrip[use RRIP extensions]'
    )
    _fs_nfs=(
      "$_nfs_ufs[@]"
      "$_fs_nfs[@]"
    )
    ;;
  linux*)
    _fs_any=(
      '(sync)async[do all I/O asynchronously]'
      '(noatime)atime[update access time]'
      '(norelatime)relatime[update atime relative to mtime/ctime]'
      '(nostrictatime)strictatime[full atime updates]'
      '(strictatime)nostrictatime[default atime update behaviour]'
      '(rw suid dev exec async)defaults[use default options]'
      '(nodev)dev[interpret devices]'
      '(nodiratime)diratime[update directory access time]'
      "(diratime)nodiratime[don't update directory access time]"
      '(noexec)exec[permit execution of binaries]'
      "noiversion[don't increment i_version inode field]"
      '(nomand)mand[allow mandatory locks]'
      "(atime)noatime[don't update access time]"
      "(relatime)norelatime[update access time without regard to mtime/ctime]"
      "(dev)nodev[don't interpret devices]"
      "(exec)noexec[don't allow execution of binaries]"
      "(mand)nomand[don'tallow mandatory locks]"
      '(suid)nosuid[ignore suid and sgid bits]'
      'remount[mount already mounted file system]'
      '(rw)ro[mount file system read-only]'
      '(ro)rw[mount file system read-write]'
      '(nosuid)suid[use suid and sgid bits]'
      '(async)sync[do I/O synchronously]'
      'dirsync[perform directory updates synchronously]'
      'loop[use loopback device]::loopback device:_files'
      'encryption[enable encryption]:cypher'
      'keybits[set number of bits in encryption key]:key size:(64 128 160 192 256)'
      'offset[specify data start for loopback mount]:offset (bytes)'
      '(loud)silent' '(silent)loud'
      '(fscontext defcontext)context:context'
      '(context)'{fscontext,defcontext}':context'
      'rootcontext:context'
    )
    _fs_adfs=(
      'uid[set owner of root]:user ID'
      'gid[set group of root]:group ID'
      'ownmask[permission mask for ADFS "owner" permissions]:mask (octal)'
      'othmask[permission mask for ADFS "other" permissions]:mask (octal)'
    )
    _fs_affs=(
      'uid[set owner of root]:user ID'
      'gid[set group of root]:group ID'
      'setuid[set owner of all files]:user ID'
      'setgid[set group of all files]:group ID'
      'mode[set file permissions]:file permission bits'
      "protect[don't allow changes to permissions]"
      'usemp[set owner of r                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                