chiark / gitweb /
1832d4aba4aa95047ad80953456b6363e70c2fea
[elogind.git] / shell-completion / bash / kernel-install
1 # kernel-install(8) completion                                   -*- shell-script -*-
2 #
3 # This file is part of systemd.
4 #
5 # Copyright 2013 Kay Sievers
6 #
7 # systemd is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU Lesser General Public License as published by
9 # the Free Software Foundation; either version 2.1 of the License, or
10 # (at your option) any later version.
11 #
12 # systemd is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with systemd; If not, see <http://www.gnu.org/licenses/>.
19
20 __contains_word () {
21         local word=$1; shift
22         for w in $*; do [[ $w = $word ]] && return 0; done
23         return 1
24 }
25
26 _kernel_install() {
27         local i verb comps
28         local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
29         local OPTS='-h --help'
30
31         local -A VERBS=(
32                 [ADD]='add'
33                 [REMOVE]='remove'
34         )
35
36         for ((i=0; $i <= $COMP_CWORD; i++)); do
37                 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
38                  ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
39                         verb=${COMP_WORDS[i]}
40                         break
41                 fi
42         done
43
44         if [[ -z $verb  && $cur = -* ]]; then
45                 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
46                 return 0
47         fi
48
49         if [[ -z $verb ]]; then
50                 comps=${VERBS[*]}
51
52         elif __contains_word "$verb" ${VERBS[ADD]}; then
53                 if [[ $prev = "$verb" ]]; then
54                         comps=$(cd /lib/modules; echo [0-9]*)
55                 elif [[ $prev = [0-9]* ]]; then
56                         comps=$(echo /boot/vmlinuz-$prev*)
57                 fi
58
59         elif __contains_word "$verb" ${VERBS[REMOVE]}; then
60                 comps=$(cd /lib/modules; echo [0-9]*)
61
62         fi
63
64         COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
65         return 0
66 }
67
68 complete -F _kernel_install kernel-install