# This file is part of systemd.
#
# Copyright 2010 Ran Benita
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# systemd is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with systemd; If not, see <http://www.gnu.org/licenses/>.

__contains_word() {
        local w word=$1; shift
        for w in "$@"; do
                [[ $w = "$word" ]] && return
        done
}

__journal_fields='MESSAGE MESSAGE_ID PRIORITY CODE_FILE CODE_LINE CODE_FUNC
                  ERRNO SYSLOG_FACILITY SYSLOG_IDENTIFIER SYSLOG_PID COREDUMP_EXE
                  _PID _UID _GID _COMM _EXE _CMDLINE
                  _AUDIT_SESSION _AUDIT_LOGINUID
                  _SYSTEMD_CGROUP _SYSTEMD_SESSION _SYSTEMD_UNIT _SYSTEMD_OWNER_UID
                  _SELINUX_CONTEXT _SOURCE_REALTIME_TIMESTAMP
                  _BOOT_ID _MACHINE_ID _HOSTNAME _TRANSPORT
                  _KERNEL_DEVICE _KERNEL_SUBSYSTEM
                  _UDEV_SYSNAME _UDEV_DEVNODE _UDEV_DEVLINK
                  __CURSOR __REALTIME_TIMESTAMP __CURSOR __MONOTONIC_TIMESTAMP'

_journalctl() {
        local field_vals cur prev comps
        _get_comp_words_by_ref cur prev

        local OPTS_STANDALONE='-a --all --full
                               -b --this-boot --disk-usage -f --follow --header
                               -h --help -l --local --new-id128 -m --merge --no-pager
                               --no-tail -q --quiet --setup-keys --this-boot --verify
                               --version --list-catalog --update-catalog --list-boots'
        local OPTS_ARG='-b --boot --this-boot -D --directory --file -F --field
                               -o --output -u --unit --user-unit'
        local OPTS_ARGUNKNOWN='-c --cursor --interval -n --lines -p --priority --since --until
                               -verify-key'

        if __contains_word "$prev" "$OPTS_ARG" $OPTS_ARGUNKNOWN; then
                case $prev in
                        --boot|--this-boot|-b)
                                comps=$(journalctl -F '_BOOT_ID' 2>/dev/null)
                        ;;
                        --directory|-D)
                                comps=$(compgen -o filenames -d -- "$cur")
                        ;;
                        --file)
                                comps=$(compgen -o filenames -f -- "$cur")
                        ;;
                        --output|-o)
                                comps='short short-monotonic verbose export json cat'
                        ;;
                        --field|-F)
                                comps=$__journal_fields
                        ;;
                        --unit|-u)
                                comps=$(journalctl -F '_SYSTEMD_UNIT' 2>/dev/null)
                        ;;
                        --user-unit)
                                comps=$(journalctl -F '_SYSTEMD_USER_UNIT' 2>/dev/null)
                        ;;
                        *)
                                return 0
                        ;;
                esac
                COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
                return 0
        fi

        if [[ $cur = -* ]]; then
                COMPREPLY=( $(compgen -W '$OPTS_ARG $OPTS_STANDALONE' -- "$cur") )
                return 0
        elif [[ $cur = *=* ]]; then
                field_vals=( $(journalctl -F "${prev%=}" 2>/dev/null) )
                COMPREPLY=( $(compgen -W '$field_vals' -- "${cur#=}") )
        elif [[ $cur = /dev* ]]; then
                COMPREPLY=( $(compgen -o filenames -f -- "${cur}") )
        elif [[ $cur = /* ]]; then
                # Append /dev/ to the list of completions, so that
                # after typing /<TAB><TAB> the user sees /dev/ as one
                # of the alternatives. Later on the rule above will
                # take care of showing device files in /dev/.
                field_vals=( $(journalctl -F "_EXE" 2>/dev/null; echo '/dev/') )
                COMPREPLY=( $(compgen -W '$field_vals' -- "${cur}") )
                if [[ "${COMPREPLY}" = '/dev/' ]]; then
                    COMPREPLY=( $(compgen -o filenames -f -- "${cur}") )
                fi
        elif [[ $prev = '=' ]]; then
                field_vals=( $(journalctl -F "${COMP_WORDS[COMP_CWORD-2]}" 2>/dev/null) )
                COMPREPLY=( $(compgen -W '$field_vals' -- "$cur") )
        else
                COMPREPLY=( $(compgen -o nospace -W '$__journal_fields' -S= -- "$cur") )
        fi
}
complete -F _journalctl journalctl
