# ngcp-kamctl(8) completion                                     -*- shell-script -*-

_ngcp-kamctl()
{
    local cur prev words cword
    _init_completion || return

    local -a lb_fifo_actions
    # executing `ngcp-kamctl lb fifo system.listMethods` requires root permissions
    if [[ "${UID}" == '0' ]] && command -v jq &>/dev/null ; then
      readarray lb_fifo_actions <<<"$(ngcp-kamctl lb fifo system.listMethods | jq -r '.[]')"
    else
      lb_fifo_actions=(
        corex.debug
        htable.dump
        system.listMethods
      )
    fi

    # we're offering only some commonly used actions
    local -a proxy_actions
    proxy_actions=(
      db
      dialplan
      dispatcher
      domain
      fifo
      online
      ul
    )

    local -a proxy_fifo_actions
    # executing `ngcp-kamctl proxy fifo system.listMethods` requires root permissions
    if [[ "${UID}" == '0' ]] && command -v jq &>/dev/null ; then
      readarray proxy_fifo_actions <<<"$(ngcp-kamctl proxy fifo system.listMethods | jq -r '.[]')"
    else
      proxy_fifo_actions=(
        core.info
        core.sockets_list
        core.uptime
        corex.debug
        dispatcher.list
        dlg.list
        dlg.list
        dlg.profile_get_size
        lcr.dump_gws
        lcr.dump_rules
        online
        stats.get_statistics
        system.listMethods
        tm.stats
      )
    fi

# useful for debugging:
#   echo >&2
#   echo "prev : (${prev})" >&2
#   echo "cur  : (${cur})" >&2
#   echo "words: (${words[@]})" >&2
#   echo "cword: (${cword})" >&2
#   echo >&2

    if [[ $cword -eq 1 ]]; then
      COMPREPLY=( $( compgen -W 'lb proxy' -- "$cur" ) )
      return 0
    fi

    ## lb
    if [[ "$prev" == "lb" ]] ; then
      COMPREPLY=( $(compgen -W 'fifo' -- "$cur") )
      return 0
    fi

    if [[ "${words[@]}" =~ "ngcp-kamctl lb fifo htable.dump" ]] ; then
      COMPREPLY=( $(compgen -W 'auth ipban' -- "$cur") )
      return 0
    fi

    if [[ "${words[@]}" =~ "ngcp-kamctl lb fifo" ]] ; then
      COMPREPLY=( $(compgen -W '${lb_fifo_actions[@]}' -- "$cur") )
      return 0
    fi

    ## proxy
    if [[ "${words[@]}" =~ "ngcp-kamctl proxy db show" ]] ; then
      # note: we could get this via `mysql --batch --silent -e 'use kamailio; show tables'`, but we limit it to most relevant ones
      COMPREPLY=( $(compgen -W 'active_watchers dialplan lcr_gw lcr_rule lcr_rule_target location sems_registrations trusted' -- "$cur") )
      return 0
    fi

    if [[ "${words[@]}" =~ "ngcp-kamctl proxy db" ]] ; then
      COMPREPLY=( $(compgen -W 'show' -- "$cur") )
      return 0
    fi

    if [[ "${words[@]}" =~ "ngcp-kamctl proxy dialplan" ]] ; then
      COMPREPLY=( $(compgen -W 'show' -- "$cur") )
      return 0
    fi

    if [[ "${words[@]}" =~ "ngcp-kamctl proxy dispatcher" ]] ; then
      COMPREPLY=( $(compgen -W 'dump reload' -- "$cur") )
      return 0
    fi

    if [[ "${words[@]}" =~ "ngcp-kamctl proxy domain" ]] ; then
      COMPREPLY=( $(compgen -W 'show showdb' -- "$cur") )
      return 0
    fi


    if [[ "${words[@]}" =~ "ngcp-kamctl proxy ul" ]] ; then
      COMPREPLY=( $(compgen -W 'show' -- "$cur") )
      return 0
    fi

    if [[ "${words[@]}" =~ "ngcp-kamctl proxy fifo dlg.profile_get_size" ]] ; then
      COMPREPLY=( $(compgen -W 'account' -- "$cur") )
      return 0
    fi

    if [[ "${words[@]}" =~ "ngcp-kamctl proxy fifo stats.get_statistics" ]] ; then
      COMPREPLY=( $(compgen -W 'all dialog dialog: usrloc' -- "$cur") )
      return 0
    fi

    if [[ "${words[@]}" =~ "ngcp-kamctl proxy fifo stats.get_statistics dialog" ]] ; then
      COMPREPLY=( $(compgen -W 'active_dialogs early_dialogs' -- "$cur") )
      return 0
    fi

    if [[ "${words[@]}" =~ "ngcp-kamctl proxy fifo stats.get_statistics usrloc" ]] ; then
      COMPREPLY=( $(compgen -W 'registered_users' -- "$cur") )
      return 0
    fi

    if [[ "${words[@]}" =~ "ngcp-kamctl proxy fifo" ]] ; then
      COMPREPLY=( $(compgen -W '${proxy_fifo_actions[@]}' -- "$cur") )
      return 0
    fi

    if [[ "$prev" == "proxy" ]] ; then
      COMPREPLY=( $(compgen -W '${proxy_actions[@]}' -- "$cur") )
      return 0
    fi
} &&
complete -F _ngcp-kamctl ngcp-kamctl

# ex: filetype=bash
