#compdef littlesnitch

_littlesnitch() {
  integer ret=1

  if (( CURRENT > 2 )); then
    (( CURRENT-- ))
    shift words
    if (( $+functions[_littlesnitch-$words[1]] )); then
      _call_function ret _littlesnitch-$words[1]
    else
      ret=0
    fi
  else
    local context state state_descr line
     _littlesnitch_cmds && ret=0
  fi

  return ret
}

_littlesnitch_cmds() {
  typeset -a cmds
  cmds=(
    "capture-traffic:Captures all network traffic from a particular executable and outputs it to a file or to standard output"
    "debug-categories:Analyzing bugs and unexpected behavior with guidance from our support"
    "export-model:Same thing as the user interface action Little Snitch > File > Create Backup"
    "list-preferences:Lists all Little Snitch preference options and their values"
    "log:Frontend to the macOS log command, using preconfigured filters suitable for Little Snitch"
    "log-traffic:Access to the underlying data visualized by Network Monitor"
    "read-preference:Prints a preference value in JSON format"
    "recrypt-config:All files storing configuration data on disk are encrypted with a password stored in the macOS System Keychain"
    "restore-model:Imports backups and configuration database files from various versions of Little Snitch"
    "update-rule-groups:Creates or updates application rule groups"
    "write-preference:Modify preferences"
  )
  _describe -t command "subcommands" cmds
}

_littlesnitch-capture-traffic() {
  #usage: littlesnitch capture-traffic [-hp] [-v <via process path>] <process path> [<output file>]
  integer ret=1
  local curcontext=$curcontext state line

  _arguments -w -C -S -s \
    '-h[Print a short help and exit.]' \
    '--help[Print a short help and exit.]' \
    '-v[Capture only if the connecting is made via the given helper process.]:via process path:' \
    '--via[Capture only if the connecting is made via the given helper process.]:via process path:' \
    '-p[Use pcap output format.]' \
    '--pcap[Use pcap output format.]' \
    && ret=0

  return ret
}

_littlesnitch-debug-categories() {
  #usage: littlesnitch debug-categories [-adfhls] [<categoryName>, ...]
  integer ret=1
  local curcontext=$curcontext state line

  _arguments -w -C -S -s \
    '-h[Print a short help and exit.]' \
    '--help[Print a short help and exit.]' \
    '-a[Add to current set of categories.]' \
    '--add[Add to current set of categories.]' \
    '-d[Delete from current set of categories.]' \
    '--delete[Delete from current set of categories.]' \
    '-s[Set current set of categories.]' \
    '--set[Set current set of categories.]' \
    '-f[Reset to factory default.]' \
    '--factory-reset[Reset to factory default.]' \
    && ret=0

  return ret
}

_littlesnitch-export-model() {
  #usage: littlesnitch export-model [-h] [<output file>]
  integer ret=1
  local curcontext=$curcontext state line

  _arguments -w -C -S -s \
    '-h[Print a short help and exit.]' \
    '--help[Print a short help and exit.]' \
    && ret=0

  return ret
}

_littlesnitch-list-preferences() {
  #usage: littlesnitch list-preferences [-ghu]
  integer ret=1
  local curcontext=$curcontext state line

  _arguments -w -C -S -s \
    '-h[Print a short help and exit.]' \
    '--help[Print a short help and exit.]' \
    '-g[Show only preferences affecting all users.]' \
    '--global[Show only preferences affecting all users.]' \
    '-u[Show only preferences affecting the current user.]' \
    '--user[Show only preferences affecting the current user.]' \
    && ret=0

  return ret
}

_littlesnitch-log() {
  #usage: littlesnitch log [-adfhjs] [-l <time>[m|h|d]] [-p <log predicate>]
  integer ret=1
  local curcontext=$curcontext state line

  _arguments -w -C -S -s \
    '-h[Print a short help and exit.]' \
    '--help[Print a short help and exit.]' \
    '-l[Show entries not older than the given time. (Default 10m)]:time [m|h|d]:' \
    '--last[Show entries not older than the given time. (Default 10m)]:time [m|h|d]:' \
    '-s[Live stream log messages.]' \
    '--stream[Live stream log messages.]' \
    '-a[Also show log messages from frameworks.]' \
    '--all[Also show log messages from frameworks.]' \
    '-f[Show log messages from frameworks only.]' \
    '--frameworks-only[Show log messages from frameworks only.]' \
    '-d[Show debug messages from frameworks.]' \
    '--show-debug[Show debug messages from frameworks.]' \
    '-p[Use the given predicate string to filter messages.]:log predicate:' \
    '--predicate[Use the given predicate string to filter messages.]:log predicate:' \
    '-j[Output in JSON format.]' \
    '--json[Output in JSON format.]' \
    && ret=0

  return ret
}

_littlesnitch-log-traffic() {
  #usage: littlesnitch log-traffic [-hs] [-b <YYYY-MM-DD HH:MM:SS>] [-e <YYYY-MM-DD HH:MM:SS>]
  integer ret=1
  local curcontext=$curcontext state line

  _arguments -w -C -S -s \
    '-h[Print a short help and exit.]' \
    '--help[Print a short help and exit.]' \
    '-b[Where to begin reading logged traffic history. If not specified, no historic data is printed. Optional.]:YYYY-MM-DD:' \
    '--begin-date[Where to begin reading logged traffic history. If not specified, no historic data is printed. Optional.]:YYYY-MM-DD:' \
    '-e[Where to stop reading logged traffic history. Optional.]:YYYY-MM-DD:' \
    '--end-date[Where to stop reading logged traffic history. Optional.]:YYYY-MM-DD:' \
    '-s[Stream live traffic statistics. Optional, incompatible with --end-date.]' \
    '--stream[Stream live traffic statistics. Optional, incompatible with --end-date.]' \
    && ret=0

  return ret
}

_littlesnitch-read-preference() {
  #usage: littlesnitch read-preference [-h] <preference key>, ...
  integer ret=1
  local curcontext=$curcontext state line

  _arguments -w -C -S -s \
    '-h[Print a short help and exit.]' \
    '--help[Print a short help and exit.]' \
    && ret=0

  return ret
}

_littlesnitch-recrypt-config() {
  #usage: littlesnitch recrypt-config [-h] [-c <current encryption key>] [-p <new encryption key>] [<config file path>, ...]
  integer ret=1
  local curcontext=$curcontext state line

  _arguments -w -C -S -s \
    '-h[Print a short help and exit.]' \
    '--help[Print a short help and exit.]' \
    '-c[Use this password for decrypting current configuration.]:current encryption key:' \
    '--current-password[Use this password for decrypting current configuration.]:current encryption key:' \
    '-p[Write out configuration files using this encryption key. (Default "none")]:new encryption key:' \
    '--password[Write out configuration files using this encryption key. (Default "none")]:new encryption key:' \
    && ret=0

  return ret
}

_littlesnitch-restore-model() {
  #usage: littlesnitch restore-model [-hl] [-m backupUID > localUID, ...] [-p <password>] [<input file>]
  integer ret=1
  local curcontext=$curcontext state line

  _arguments -w -C -S -s \
    '-h[Print a short help and exit.]' \
    '--help[Print a short help and exit.]' \
    '-l[Do not actually restore, just list users contained in the file.]' \
    '--list-users[Do not actually restore, just list users contained in the file.]' \
    '-m[Provide a mapping between backup and local user-IDs.backupUID > localUID, ...]' \
    '--map-users[Provide a mapping between backup and local user-IDs.backupUID > localUID, ...]' \
    '-p[When importing a configuration.xpl file from a different computer, the "Little Snitch Encryption Key" from the system keychain may be required. Optional.]:password:' \
    '--password[When importing a configuration.xpl file from a different computer, the "Little Snitch Encryption Key" from the system keychain may be required. Optional.]:password:' \
    && ret=0

  return ret
}

_littlesnitch-update-rule-group() {
  #usage: littlesnitch update-rule-groups [-aht]
  integer ret=1
  local curcontext=$curcontext state line

  _arguments -w -C -S -s \
    '-h[Print a short help and exit.]' \
    '--help[Print a short help and exit.]' \
    '-a[Update Apple Applications group only]' \
    '--apple-apps[Update Apple Applications group only]' \
    '-t[Update Third Party Applications group only]' \
    '--third-party-apps[Update Third Party Applications group only]' \
    && ret=0

  return ret
}

_littlesnitch-write-preference() {
  #usage: littlesnitch write-preference [-hr] <key> [<new value>]
  integer ret=1
  local curcontext=$curcontext state line

  _arguments -w -C -S -s \
    '-h[Print a short help and exit.]' \
    '--help[Print a short help and exit.]' \
    '-r[Remove the value for the given key.]:key:' \
    '--remove[Remove the value for the given key.]:key:' \
    && ret=0

  return ret
}
