1# bootctl(1) completion                               -*- shell-script -*-
2# SPDX-License-Identifier: LGPL-2.1-or-later
3#
4# This file is part of systemd.
5#
6# systemd is free software; you can redistribute it and/or modify it
7# under the terms of the GNU Lesser General Public License as published by
8# the Free Software Foundation; either version 2.1 of the License, or
9# (at your option) any later version.
10#
11# systemd is distributed in the hope that it will be useful, but
12# WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14# General Public License for more details.
15#
16# You should have received a copy of the GNU Lesser General Public License
17# along with systemd; If not, see <http://www.gnu.org/licenses/>.
18
19__contains_word () {
20    local w word=$1; shift
21    for w in "$@"; do
22        [[ $w = "$word" ]] && return
23    done
24}
25
26__get_entry_ids() {
27    bootctl --no-pager list 2>/dev/null | { while read -r a b; do [[ $a == 'id:' ]] && echo " $b"; done }
28}
29
30_bootctl() {
31    local i verb comps
32    local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
33    local -A OPTS=(
34        [STANDALONE]='-h --help -p --print-esp-path -x --print-boot-path --version --no-variables --no-pager --graceful'
35        [ARG]='--esp-path --boot-path --make-machine-id-directory'
36    )
37
38    if __contains_word "$prev" ${OPTS[ARG]}; then
39        case $prev in
40            --esp-path|--boot-path)
41                if [[ -z $cur ]]; then
42                    comps=$(compgen -A directory -- "/" )
43                else
44                    comps=$(compgen -A directory -- "$cur" )
45                fi
46                compopt -o filenames
47                ;;
48            --make-machine-id-directory)
49                comps="yes no auto"
50                ;;
51        esac
52        COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
53        return 0
54    fi
55
56    if [[ "$cur" = -* ]]; then
57        COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
58        return 0
59    fi
60
61    local -A VERBS=(
62        # systemd-efi-options takes an argument, but it is free-form, so we cannot complete it
63        [STANDALONE]='help status install update remove is-installed random-seed systemd-efi-options list set-timeout set-timeout-oneshot'
64        [BOOTENTRY]='set-default set-oneshot'
65        [BOOLEAN]='reboot-to-firmware'
66    )
67
68    for ((i=0; i < COMP_CWORD; i++)); do
69        if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]}; then
70            verb=${COMP_WORDS[i]}
71            break
72        fi
73    done
74
75    if [[ -z ${verb-} ]]; then
76        comps=${VERBS[*]}
77    elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
78        comps=''
79    elif __contains_word "$verb" ${VERBS[BOOTENTRY]}; then
80        name=
81        for ((i++; i < COMP_CWORD; i++)); do
82            if ! __contains_word "${COMP_WORDS[i]}" ${OPTS[*]} ${VERBS[*]} &&
83                    ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
84                name=${COMP_WORDS[i]}
85                break;
86            fi
87        done
88
89        if [[ -z $name ]]; then
90            comps=$( __get_entry_ids )
91        else
92            comps=''
93        fi
94    elif __contains_word "$verb" ${VERBS[BOOLEAN]}; then
95        comps="yes no"
96    fi
97
98    COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
99    return 0
100}
101
102complete -F _bootctl bootctl
103