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

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

    if ! [ -r /usr/share/bash-completion/completions/systemctl ] ; then
        return
    fi
    . /usr/share/bash-completion/completions/systemctl

    # based on upstream's function, but additionally include "alias" units
    __get_not_masked_unit_files() {
	# filter out masked, not-found, or template units.
	__systemctl $1 list-unit-files --state enabled,enabled-runtime,linked,linked-runtime,static,indirect,disabled,generated,transient,alias "$2*" | \
	    { while read -r a b; do [[ $a =~ @\. ]] || echo " $a"; done; }
    }

    local -a cmds actions
    cmds=(
      queue-clear
      queue-run
      queue-show
      queue-start
      summary
      sync-state
    )
    actions=(
      is-managed
      reload
      restart
      start
      status
      stop
    )

    if [[ "$cur" == -* ]]; then
        local -a options=(
          --skip-ha-state-check
          --timeout
          --async
          --enqueue
          --queue
          -a  --action
          -s  --service
          -g  --group
          -v  --verbose
          --quiet
          --debug
          -h  --help
        )
        COMPREPLY=( $( compgen -W '${options[@]}' -- "$cur" ) )
        return 0
    fi

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

    # handle special characters, see the systemctl completion file for details
    cur_orig=$cur
    if [[ $cur =~ '\\' ]]; then
        cur="$(echo $cur | xargs echo)"
    else
        cur_orig="$(printf '%q' $cur)"
    fi

    if [[ $cword -eq 1 ]]; then
        COMPREPLY=( $( compgen -W '${cmds[@]} ${actions[@]}' -- "$cur" ) )
        return 0
    fi

    if [[ "$prev" == "--timeout" ]] ; then
        COMPREPLY=( $(compgen -W '{0..30}' -- "$cur") )
        return 0
    fi

    if [[ "$prev" == "--action" ]] ; then
        COMPREPLY=( $( compgen -W '${actions[@]}' -- "$cur" ) )
        return 0
    fi

    if [[ "$prev" == "status" ]] || [[ "$prev" == "is-managed" ]] || [[ "$prev" == "--service" ]] ; then
        comps=$( __get_non_template_units --system "$cur" )
        compopt -o filenames
        COMPREPLY=( $(compgen -o filenames -W '$comps' -- "$cur_orig") )
        return 0
    fi

    if [[ "$prev" == "reload" ]] ; then
        comps=$( __get_reloadable_units --system "$cur" )
        compopt -o filenames
        COMPREPLY=( $(compgen -o filenames -W '$comps' -- "$cur_orig") )
        return 0
    fi

    if [[ "$prev" == "restart" ]] ; then
        comps=$( __get_restartable_units --system "$cur" )
        compopt -o filenames
        COMPREPLY=( $(compgen -o filenames -W '$comps' -- "$cur_orig") )
        return 0
    fi

    if [[ "$prev" == "start" ]] ; then
        comps=$( __get_startable_units --system "$cur" )
        compopt -o filenames
        COMPREPLY=( $(compgen -o filenames -W '$comps' -- "$cur_orig") )
        return 0
    fi

    if [[ "$prev" == "stop" ]] ; then
        comps=$( __get_stoppable_units --system "$cur" )
        compopt -o filenames
        COMPREPLY=( $(compgen -o filenames -W '$comps' -- "$cur_orig") )
        return 0
    fi
} &&
complete -F _ngcp-service ngcp-service

# ex: filetype=bash
