#compdef systemd-analyze
# SPDX-License-Identifier: LGPL-2.1-or-later

(( $+functions[_systemd-analyze_log-level] )) ||
    _systemd-analyze_log-level() {
        local -a _levels
        _levels=(debug info notice warning err crit alert emerg)
        _describe -t level 'logging level' _levels || compadd "$@"
    }

(( $+functions[_systemd-analyze_log-target] )) ||
    _systemd-analyze_log-target() {
        local -a _targets
        _targets=(console journal kmsg journal-or-kmsg null)
        _describe -t target 'logging target' _targets || compadd "$@"
    }

(( $+functions[_systemd-analyze_verify] )) ||
    _systemd-analyze_verify() {
        _sd_unit_files
    }

(( $+functions[_systemd-analyze_service-watchdogs] )) ||
    _systemd-analyze_service-watchdogs() {
        local -a _states
        _states=(on off)
        _describe -t state 'state' _states || compadd "$@"
    }

(( $+functions[_systemd-analyze_cat-config] )) ||
	_systemd-analyze_cat-config() {
        _files -W '(/run/systemd/ /etc/systemd/ /usr/lib/systemd/)' -P 'systemd/'
    }

(( $+functions[_systemd-analyze_security] )) ||
    _systemd-analyze_security() {
        _sd_unit_files
    }

(( $+functions[_systemd-analyze_commands] )) ||
    _systemd-analyze_commands(){
        local -a _systemd_analyze_cmds
        # Descriptions taken from systemd-analyze --help.
        _systemd_analyze_cmds=(
            'time:Print time spent in the kernel before reaching userspace'
            'blame:Print list of running units ordered by time to init'
            'critical-chain:Print a tree of the time critical chain of units'
            'plot:Output SVG graphic showing service initialization'
            'dot:Dump dependency graph (in dot(1) format)'
            'dump:Dump server status'
            'cat-config:Cat systemd config files'
            'unit-files:List files and symlinks for units'
            'unit-paths:List unit load paths'
            'exit-status:List known exit statuses'
            'syscall-filter:List syscalls in seccomp filter'
            'condition:Evaluate Condition*= and Assert*= assignments'
            'verify:Check unit files for correctness'
            'calendar:Validate repetitive calendar time events'
            'timestamp:Parse a systemd syntax timestamp'
            'timespan:Parse a systemd syntax timespan'
            'security:Analyze security settings of a service'
            # 'log-level:Get/set systemd log threshold'
            # 'log-target:Get/set systemd log target'
            # 'service-watchdogs:Get/set service watchdog status'
        )

        if (( CURRENT == 1 )); then
            _describe "options" _systemd_analyze_cmds
        else
            local curcontext="$curcontext"
            cmd="${${_systemd_analyze_cmds[(r)$words[1]:*]%%:*}}"
            if (( $#cmd )); then
                if (( $+functions[_systemd-analyze_$cmd] )) && (( CURRENT == 2 )); then
                    _systemd-analyze_$cmd
                else
                    _message "no more options"
                fi
            else
                _message "unknown systemd-analyze command: $words[1]"
            fi
        fi
    }

_arguments \
    {-h,--help}'[Show help text]' \
    '--version[Show package version]' \
    '--system[Operate on system systemd instance]' \
    '--user[Operate on user systemd instance]' \
    '--global[Show global user instance config]' \
    '--no-pager[Do not pipe output into a pager]' \
    '--man=[Do (not) check for existence of man pages]:boolean:(1 0)' \
    '--order[When generating graph for dot, show only order]' \
    '--require[When generating graph for dot, show only requirement]' \
    '--fuzz=[When printing the tree of the critical chain, print also services, which finished TIMESPAN earlier, than the latest in the branch]:TIMESPAN' \
    '--from-pattern=[When generating a dependency graph, filter only origins]:GLOB' \
    '--to-pattern=[When generating a dependency graph, filter only destinations]:GLOB' \
    {-H+,--host=}'[Operate on remote host]:userathost:_sd_hosts_or_user_at_host' \
    {-M+,--machine=}'[Operate on local container]:machine:_sd_machines' \
    '*::systemd-analyze commands:_systemd-analyze_commands'
