Bug 21235: Remove table services_throttle
[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 autocomplete with sip-enabled instances
102 complete -F _koha_sip_enabled_instances koha-start-sip
103 complete -F _koha_sip_enabled_instances koha-restart-sip
104 complete -F _koha_sip_enabled_instances koha-stop-sip
105
106 _koha_sip_disabled()
107 {
108     _koha_list_cmd "--nosip"
109     return 0
110 }
111
112 # koha-enable-sip autocompletes with sip-disabled instances
113 complete -F _koha_sip_disabled koha-enable-sip
114
115 _koha_disabled_instances()
116 {
117     _koha_list_cmd "--disabled"
118     return 0
119 }
120
121 _koha_enabled_instances()
122 {
123     _koha_list_cmd "--enabled"
124     return 0
125 }
126
127 # koha-enable autocompletes with disabled instances
128 complete -F _koha_disabled_instances koha-enable
129
130 # koha-disable autocompletes with enabled instances
131 complete -F _koha_enabled_instances koha-disable
132
133 # koha-mysql autocompletes with a single instance name
134 complete -F _koha_single_instance koha-mysql
135
136 _koha_list()
137 {
138     local cur opts substract
139
140     COMPREPLY=()
141     _get_comp_words_by_ref cur
142     opts="--enabled --disabled --email --noemail --plack --noplack --sip --nosip --help -h"
143
144     # Build a list of the already used option switches
145     for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
146         if [[ ${COMP_WORDS[i]} == -* ]]; then
147             case ${COMP_WORDS[i]} in
148                 --disabled)
149                     substract="$substract -e --enabled"; ;;
150                 --enabled)
151                     substract="$substract -e --disabled"; ;;
152                 --email)
153                     substract="$substract -e --noemail"; ;;
154                 --noemail)
155                     substract="$substract -e --email"; ;;
156                 --plack)
157                     substract="$substract -e --noplack"; ;;
158                 --noplack)
159                     substract="$substract -e --plack"; ;;
160                 --sip)
161                     substract="$substract -e --nosip"; ;;
162                 --nosip)
163                     substract="$substract -e --sip"; ;;
164                 --help)
165                     substract="$substract -e -h"; ;;
166                 -h)
167                     substract="$substract -e --help"; ;;
168             esac
169             substract="$substract -e ${COMP_WORDS[i]}"
170         fi
171     done
172
173     if [[ "$substract" != "" ]]; then
174         opts=$( echo $opts | sed -e 's/ /\n/g'  | grep -v -x $substract )
175     fi
176
177     COMPREPLY=( $(compgen -W "$opts" -- $cur ) )
178
179     return 0
180 }
181 complete -F _koha_list koha-list
182
183 _koha_plack_instances()
184 {
185     _koha_list_cmd "--plack"
186     return 0
187 }
188
189 _koha_noplack_instances()
190 {
191     _koha_list_cmd "--noplack"
192     return 0
193 }
194
195 _koha-plack()
196 {
197     local cur opts substract
198
199     COMPREPLY=()
200     _get_comp_words_by_ref cur
201     opts="--start --stop --restart --enable --disable --quiet -q --help -h"
202
203     # Build a list of the already used option switches
204     for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
205         if [[ ${COMP_WORDS[i]} == -* ]]; then
206             case ${COMP_WORDS[i]} in
207                 --start) _koha_plack_instances ; return 0 ;;
208                  --stop) _koha_plack_instances ; return 0 ;;
209               --restart) _koha_plack_instances ; return 0 ;;
210                --enable) _koha_noplack_instances ; return 0 ;;
211               --disable) _koha_plack_instances ; return 0 ;;
212                  --help) COMPREPLY=() ; return 0 ;; # no more completions
213                      -h) COMPREPLY=() ; return 0 ;; # no more completions
214                 --quiet) # filter the other quiet switches and go on
215                     substract="$substract -e -q"; ;;
216                 -q)      # filter the other quiet switches and go on
217                     substract="$substract -e --quiet"; ;;
218             esac
219             substract="$substract -e ${COMP_WORDS[i]}"
220         fi
221     done
222
223     if [[ "$substract" != "" ]]; then
224         opts=$( echo $opts | sed -e 's/ /\n/g'  | grep -v -x $substract )
225     fi
226
227     COMPREPLY=( $(compgen -W "$opts" -- $cur ) )
228
229     return 0
230 }
231 complete -F _koha-plack koha-plack
232
233 _koha-indexer()
234 {
235     local cur opts substract
236
237     COMPREPLY=()
238     _get_comp_words_by_ref cur
239     opts="--start --stop --restart --quiet -q --help -h"
240
241     # Build a list of the already used option switches
242     for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
243         if [[ ${COMP_WORDS[i]} == -* ]]; then
244             case ${COMP_WORDS[i]} in
245                 --start) _koha_list_cmd ; return 0 ;;
246                  --stop) _koha_list_cmd ; return 0 ;;
247               --restart) _koha_list_cmd ; return 0 ;;
248                  --help) COMPREPLY=() ; return 0 ;; # no more completions
249                      -h) COMPREPLY=() ; return 0 ;; # no more completions
250                 --quiet) # filter the other quiet switches and go on
251                     substract="$substract -e -q"; ;;
252                 -q)      # filter the other quiet switches and go on
253                     substract="$substract -e --quiet"; ;;
254             esac
255             substract="$substract -e ${COMP_WORDS[i]}"
256         fi
257     done
258
259     if [[ "$substract" != "" ]]; then
260         opts=$( echo $opts | sed -e 's/ /\n/g'  | grep -v -x $substract )
261     fi
262
263     COMPREPLY=( $(compgen -W "$opts" -- $cur ) )
264
265     return 0
266 }
267 complete -F _koha-indexer koha-indexer
268
269 _koha-zebra()
270 {
271     local cur opts substract
272
273     COMPREPLY=()
274     _get_comp_words_by_ref cur
275     opts="--start --stop --restart --status --quiet -q --help -h"
276
277     # Build a list of the already used option switches
278     for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
279         if [[ ${COMP_WORDS[i]} == -* ]]; then
280             case ${COMP_WORDS[i]} in
281                 --start) _koha_list_cmd ; return 0 ;;
282                  --stop) _koha_list_cmd ; return 0 ;;
283               --restart) _koha_list_cmd ; return 0 ;;
284                --status) _koha_list_cmd ; return 0 ;;
285                  --help) COMPREPLY=() ; return 0 ;; # no more completions
286                      -h) COMPREPLY=() ; return 0 ;; # no more completions
287                 --quiet) # filter the other quiet switches and go on
288                     substract="$substract -e -q"; ;;
289                 -q)      # filter the other quiet switches and go on
290                     substract="$substract -e --quiet"; ;;
291             esac
292             substract="$substract -e ${COMP_WORDS[i]}"
293         fi
294     done
295
296     if [[ "$substract" != "" ]]; then
297         opts=$( echo $opts | sed -e 's/ /\n/g'  | grep -v -x $substract )
298     fi
299
300     COMPREPLY=( $(compgen -W "$opts" -- $cur ) )
301
302     return 0
303 }
304 complete -F _koha-zebra koha-zebra
305
306 # Local variables:
307 # mode: shell-script
308 # sh-basic-offset: 4
309 # sh-indent-comment: t
310 # indent-tabs-mode: nil
311 # End:
312 # ex: ts=4 sw=4 et filetype=sh