Bug 22770: DBRev 19.06.00.004
[koha.git] / debian / koha-common.bash-completion
1 #!/bin/bash
2 #
3 # koha-common.bash-completion script for koha-* commands
4 #
5 # This file is part of Koha.
6 #
7 # Copyright 2013 Universidad Nacional de Cordoba
8 #                Tomas Cohen Arazi
9 #
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 3 of the License, or (at your option) any later
13 # version.
14 #
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, see <http://www.gnu.org/licenses>.
21
22 _build_substract_switches()
23 {
24     local substract
25
26     for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
27         if [[ ${COMP_WORDS[i]} == -* ]]; then
28             substract="$substract -e ${COMP_WORDS[i]}"
29         fi
30     done
31
32     echo "$substract"
33 }
34
35 _build_substract_instances()
36 {
37     local substract
38
39     for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
40         substract="$substract -e ${COMP_WORDS[i]}"
41     done
42
43     echo "$substract"
44 }
45
46 _koha_single_instance()
47 {
48     local filter=$1
49
50     cur=${COMP_WORDS[COMP_CWORD]}
51     prev=${COMP_WORDS[COMP_CWORD-1]}
52     if [ $COMP_CWORD -eq 1 ]; then
53         COMPREPLY=( $( compgen -W "$(koha-list $filter)" $cur ) )
54     else
55         COMPREPLY=()
56     fi
57
58     return 0
59 }
60
61 _koha_list_cmd()
62 {
63     local filter=$1
64
65     local cur substract instancelist
66     _get_comp_words_by_ref cur
67
68     # Build a list of the already used words
69     substract=`_build_substract_instances`
70
71     if [[ "$substract" != "" ]]; then
72         instancelist=$( koha-list $filter | grep -v -x $substract )
73     else
74         instancelist=$( koha-list $filter )
75     fi
76
77     COMPREPLY=( $(compgen -W "$instancelist" -- $cur ) )
78     return 0
79 }
80
81 _koha_email_disable()
82 {
83     _koha_list_cmd "--email"
84     return 0
85 }
86 complete -F _koha_email_disable koha-email-disable
87
88 _koha_email_enable()
89 {
90     _koha_list_cmd "--noemail"
91     return 0
92 }
93 complete -F _koha_email_enable koha-email-enable
94
95 _koha_sip_enabled_instances()
96 {
97     _koha_list_cmd "--sip"
98     return 0
99 }
100
101 _koha_sip_disabled()
102 {
103     _koha_list_cmd "--nosip"
104     return 0
105 }
106
107 _koha_disabled_instances()
108 {
109     _koha_list_cmd "--disabled"
110     return 0
111 }
112
113 _koha_enabled_instances()
114 {
115     _koha_list_cmd "--enabled"
116     return 0
117 }
118
119 # koha-enable autocompletes with disabled instances
120 complete -F _koha_disabled_instances koha-enable
121
122 # koha-disable autocompletes with enabled instances
123 complete -F _koha_enabled_instances koha-disable
124
125 # koha-mysql autocompletes with a single instance name
126 complete -F _koha_single_instance koha-mysql
127
128 _koha_list()
129 {
130     local cur opts substract
131
132     COMPREPLY=()
133     _get_comp_words_by_ref cur
134     opts="--enabled --disabled --email --noemail --plack --noplack --sip --nosip --help -h"
135
136     # Build a list of the already used option switches
137     for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
138         if [[ ${COMP_WORDS[i]} == -* ]]; then
139             case ${COMP_WORDS[i]} in
140                 --disabled)
141                     substract="$substract -e --enabled"; ;;
142                 --enabled)
143                     substract="$substract -e --disabled"; ;;
144                 --email)
145                     substract="$substract -e --noemail"; ;;
146                 --noemail)
147                     substract="$substract -e --email"; ;;
148                 --plack)
149                     substract="$substract -e --noplack"; ;;
150                 --noplack)
151                     substract="$substract -e --plack"; ;;
152                 --sip)
153                     substract="$substract -e --nosip"; ;;
154                 --nosip)
155                     substract="$substract -e --sip"; ;;
156                 --help)
157                     substract="$substract -e -h"; ;;
158                 -h)
159                     substract="$substract -e --help"; ;;
160             esac
161             substract="$substract -e ${COMP_WORDS[i]}"
162         fi
163     done
164
165     if [[ "$substract" != "" ]]; then
166         opts=$( echo $opts | sed -e 's/ /\n/g'  | grep -v -x $substract )
167     fi
168
169     COMPREPLY=( $(compgen -W "$opts" -- $cur ) )
170
171     return 0
172 }
173 complete -F _koha_list koha-list
174
175 _koha_plack_instances()
176 {
177     _koha_list_cmd "--plack"
178     return 0
179 }
180
181 _koha_noplack_instances()
182 {
183     _koha_list_cmd "--noplack"
184     return 0
185 }
186
187 _koha-plack()
188 {
189     local cur opts substract
190
191     COMPREPLY=()
192     _get_comp_words_by_ref cur
193     opts="--start --stop --restart --enable --disable --quiet -q --help -h"
194
195     # Build a list of the already used option switches
196     for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
197         if [[ ${COMP_WORDS[i]} == -* ]]; then
198             case ${COMP_WORDS[i]} in
199                 --start) _koha_plack_instances ; return 0 ;;
200                  --stop) _koha_plack_instances ; return 0 ;;
201               --restart) _koha_plack_instances ; return 0 ;;
202                --enable) _koha_noplack_instances ; return 0 ;;
203               --disable) _koha_plack_instances ; return 0 ;;
204                  --help) COMPREPLY=() ; return 0 ;; # no more completions
205                      -h) COMPREPLY=() ; return 0 ;; # no more completions
206                 --quiet) # filter the other quiet switches and go on
207                     substract="$substract -e -q"; ;;
208                 -q)      # filter the other quiet switches and go on
209                     substract="$substract -e --quiet"; ;;
210             esac
211             substract="$substract -e ${COMP_WORDS[i]}"
212         fi
213     done
214
215     if [[ "$substract" != "" ]]; then
216         opts=$( echo $opts | sed -e 's/ /\n/g'  | grep -v -x $substract )
217     fi
218
219     COMPREPLY=( $(compgen -W "$opts" -- $cur ) )
220
221     return 0
222 }
223 complete -F _koha-plack koha-plack
224
225 _koha-indexer()
226 {
227     local cur opts substract
228
229     COMPREPLY=()
230     _get_comp_words_by_ref cur
231     opts="--start --stop --restart --quiet -q --help -h"
232
233     # Build a list of the already used option switches
234     for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
235         if [[ ${COMP_WORDS[i]} == -* ]]; then
236             case ${COMP_WORDS[i]} in
237                 --start) _koha_list_cmd ; return 0 ;;
238                  --stop) _koha_list_cmd ; return 0 ;;
239               --restart) _koha_list_cmd ; return 0 ;;
240                  --help) COMPREPLY=() ; return 0 ;; # no more completions
241                      -h) COMPREPLY=() ; return 0 ;; # no more completions
242                 --quiet) # filter the other quiet switches and go on
243                     substract="$substract -e -q"; ;;
244                 -q)      # filter the other quiet switches and go on
245                     substract="$substract -e --quiet"; ;;
246             esac
247             substract="$substract -e ${COMP_WORDS[i]}"
248         fi
249     done
250
251     if [[ "$substract" != "" ]]; then
252         opts=$( echo $opts | sed -e 's/ /\n/g'  | grep -v -x $substract )
253     fi
254
255     COMPREPLY=( $(compgen -W "$opts" -- $cur ) )
256
257     return 0
258 }
259 complete -F _koha-indexer koha-indexer
260
261 _koha-zebra()
262 {
263     local cur opts substract
264
265     COMPREPLY=()
266     _get_comp_words_by_ref cur
267     opts="--start --stop --restart --status --quiet -q --help -h"
268
269     # Build a list of the already used option switches
270     for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
271         if [[ ${COMP_WORDS[i]} == -* ]]; then
272             case ${COMP_WORDS[i]} in
273                 --start) _koha_list_cmd ; return 0 ;;
274                  --stop) _koha_list_cmd ; return 0 ;;
275               --restart) _koha_list_cmd ; return 0 ;;
276                --status) _koha_list_cmd ; return 0 ;;
277                  --help) COMPREPLY=() ; return 0 ;; # no more completions
278                      -h) COMPREPLY=() ; return 0 ;; # no more completions
279                 --quiet) # filter the other quiet switches and go on
280                     substract="$substract -e -q"; ;;
281                 -q)      # filter the other quiet switches and go on
282                     substract="$substract -e --quiet"; ;;
283             esac
284             substract="$substract -e ${COMP_WORDS[i]}"
285         fi
286     done
287
288     if [[ "$substract" != "" ]]; then
289         opts=$( echo $opts | sed -e 's/ /\n/g'  | grep -v -x $substract )
290     fi
291
292     COMPREPLY=( $(compgen -W "$opts" -- $cur ) )
293
294     return 0
295 }
296 complete -F _koha-zebra koha-zebra
297
298 _koha-sip()
299 {
300     local cur opts substract
301
302     COMPREPLY=()
303     _get_comp_words_by_ref cur
304     opts="--start --stop --restart --status --enable --verbose -v --help -h"
305
306     # Build a list of the already used option switches
307     for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
308         if [[ ${COMP_WORDS[i]} == -* ]]; then
309             case ${COMP_WORDS[i]} in
310                 --start) _koha_sip_enabled_instances ; return 0 ;;
311                  --stop) _koha_sip_enabled_instances ; return 0 ;;
312               --restart) _koha_sip_enabled_instances ; return 0 ;;
313                --status) _koha_sip_enabled_instances ; return 0 ;;
314                --enable) _koha_sip_disabled ; return 0 ;;
315                  --help) COMPREPLY=() ; return 0 ;; # no more completions
316                      -h) COMPREPLY=() ; return 0 ;; # no more completions
317               --verbose) # filter the other quiet switches and go on
318                     substract="$substract -e -q"; ;;
319                 -v)      # filter the other quiet switches and go on
320                     substract="$substract -e --verbose"; ;;
321             esac
322             substract="$substract -e ${COMP_WORDS[i]}"
323         fi
324     done
325
326     if [[ "$substract" != "" ]]; then
327         opts=$( echo $opts | sed -e 's/ /\n/g'  | grep -v -x $substract )
328     fi
329
330     COMPREPLY=( $(compgen -W "$opts" -- $cur ) )
331
332     return 0
333 }
334 complete -F _koha-sip koha-sip
335
336 # Local variables:
337 # mode: shell-script
338 # sh-basic-offset: 4
339 # sh-indent-comment: t
340 # indent-tabs-mode: nil
341 # End:
342 # ex: ts=4 sw=4 et filetype=sh