#!/bin/bash
_balooctl() {
  local cur prev opts cmd subcmd formats targets
  COMPREPLY=()
  commands="status enable disable start stop restart suspend resume check index clear config monitor indexSize"
  cur="${COMP_WORDS[COMP_CWORD]}"
  prev="${COMP_WORDS[COMP_CWORD-1]}"
#   echo "prev,cur='${prev},${cur}'"
  opts="--help --version"
  case "$prev,$cur" in 
  balooctl,-*)
    COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
    return 0
  ;;
  balooctl,*)
    COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
    return 0
  ;;
  config,*)
    subcommands="add remove list show set help"
    COMPREPLY=( $(compgen -W "${subcommands}" -- ${cur}) )
    return 0
  ;;
  status,-*)
    opts="--format"
    COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
    return 0
  ;;
  --format,*)
    formats="simple json multiline"
    COMPREPLY=( $(compgen -W "${formats}" -- ${cur}) )
    return 0
  ;;
  
  esac
  cmd="${COMP_WORDS[1]}"
  subcmd="${COMP_WORDS[2]}"

  case "${cmd},${subcmd}" in
  status,*)
    COMPREPLY=( $(compgen -f -d -- ${cur}) )
    return 0
  ;;
  config,list|config,show)
    targets="hidden contentIndexing includeFolders excludeFolders excludeFilters excludeMimetypes"
    if [ "$prev" = "list" ] || [ "$prev" = "show" ]; then 
      COMPREPLY=( $(compgen -W "${targets}" -- ${cur}) )
    fi
    return 0
  ;;
  config,set)
    if [ "$prev" = "set" ]; then 
      targets="hidden contentIndexing"
      COMPREPLY=( $(compgen -W "${targets}" -- ${cur}) )
    elif [ "$prev" = "hidden" ] || [ "$prev" = "contentIndexing" ]; then 
      targets="yes no"
      COMPREPLY=( $(compgen -W "${targets}" -- ${cur}) )
    fi
    return 0
  ;;
  config,add|config,remove)
    targets="includeFolders excludeFolders excludeFilters excludeMimetypes"
    case "$prev" in
    includeFolders|excludeFolders)
        COMPREPLY=( $(compgen -d -- ${cur}) )
        return 0
    ;;
    add|remove)
      COMPREPLY=( $(compgen -W "${targets}" -- ${cur}) )
      return 0
    ;;
    excludeFilters|excludeMimetypes)
      return 0
    ;;
    esac
  ;;
  esac
}

complete -F _balooctl balooctl


_baloodb() {
  local cur prev opts cmd subcmd
  COMPREPLY=()
  commands="list devices clean"
  cur="${COMP_WORDS[COMP_CWORD]}"
  prev="${COMP_WORDS[COMP_CWORD-1]}"
  opts="--help --version"
  case "$prev,$cur" in 
  baloodb,-*)
    COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
    return 0
  ;;
  baloodb,*)
    COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
    return 0
  ;;
  list,-*|devices,-*)
    opts="--device-id --missing-only"
    COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
    return 0
  ;;
  clean,-*)
    opts="--device-id --dry-run --mounted-only"
    COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
    return 0
  ;;
  esac
  
  cmd="${COMP_WORDS[1]}"
  case "$cmd,$cur" in
  prune,-*|clean,-*)
    opts="--device-id --dry-run --mounted-only"
    COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
    return 0
  ;;
  esac
}

complete -F _baloodb baloodb






