Bug 15939: Action logs - Do not default dates to today
[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-*-zebra autocomplete with enabled instances
134 complete -F _koha_enabled_instances koha-start-zebra
135 complete -F _koha_enabled_instances koha-restart-zebra
136 complete -F _koha_enabled_instances koha-stop-zebra
137
138 # koha-mysql autocompletes with a single instance name
139 complete -F _koha_single_instance koha-mysql
140
141 _koha_list()
142 {
143     local cur opts substract
144
145     COMPREPLY=()
146     _get_comp_words_by_ref cur
147     opts="--enabled --disabled --email --noemail --plack --noplack --sip --nosip --help -h"
148
149     # Build a list of the already used option switches
150     for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
151         if [[ ${COMP_WORDS[i]} == -* ]]; then
152             case ${COMP_WORDS[i]} in
153                 --disabled)
154                     substract="$substract -e --enabled"; ;;
155                 --enabled)
156                     substract="$substract -e --disabled"; ;;
157                 --email)
158                     substract="$substract -e --noemail"; ;;
159                 --noemail)
160                     substract="$substract -e --email"; ;;
161                 --plack)
162                     substract="$substract -e --noplack"; ;;
163                 --noplack)
164                     substract="$substract -e --plack"; ;;
165                 --sip)
166                     substract="$substract -e --nosip"; ;;
167                 --nosip)
168                     substract="$substract -e --sip"; ;;
169                 --help)
170                     substract="$substract -e -h"; ;;
171                 -h)
172                     substract="$substract -e --help"; ;;
173             esac
174             substract="$substract -e ${COMP_WORDS[i]}"
175         fi
176     done
177
178     if [[ "$substract" != "" ]]; then
179         opts=$( echo $opts | sed -e 's/ /\n/g'  | grep -v -x $substract )
180     fi
181
182     COMPREPLY=( $(compgen -W "$opts" -- $cur ) )
183
184     return 0
185 }
186 complete -F _koha_list koha-list
187
188 _koha_plack_instances()
189 {
190     _koha_list_cmd "--plack"
191     return 0
192 }
193
194 _koha_noplack_instances()
195 {
196     _koha_list_cmd "--noplack"
197     return 0
198 }
199
200 _koha-plack()
201 {
202     local cur opts substract
203
204     COMPREPLY=()
205     _get_comp_words_by_ref cur
206     opts="--start --stop --restart --enable --disable --quiet -q --help -h"
207
208     # Build a list of the already used option switches
209     for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
210         if [[ ${COMP_WORDS[i]} == -* ]]; then
211             case ${COMP_WORDS[i]} in
212                 --start) _koha_plack_instances ; return 0 ;;
213                  --stop) _koha_plack_instances ; return 0 ;;
214               --restart) _koha_plack_instances ; return 0 ;;
215                --enable) _koha_noplack_instances ; return 0 ;;
216               --disable) _koha_plack_instances ; return 0 ;;
217                  --help) COMPREPLY=() ; return 0 ;; # no more completions
218                      -h) COMPREPLY=() ; return 0 ;; # no more completions
219                 --quiet) # filter the other quiet switches and go on
220                     substract="$substract -e -q"; ;;
221                 -q)      # filter the other quiet switches and go on
222                     substract="$substract -e --quiet"; ;;
223             esac
224             substract="$substract -e ${COMP_WORDS[i]}"
225         fi
226     done
227
228     if [[ "$substract" != "" ]]; then
229         opts=$( echo $opts | sed -e 's/ /\n/g'  | grep -v -x $substract )
230     fi
231
232     COMPREPLY=( $(compgen -W "$opts" -- $cur ) )
233
234     return 0
235 }
236 complete -F _koha-plack koha-plack
237
238 _koha-indexer()
239 {
240     local cur opts substract
241
242     COMPREPLY=()
243     _get_comp_words_by_ref cur
244     opts="--start --stop --restart --quiet -q --help -h"
245
246     # Build a list of the already used option switches
247     for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
248         if [[ ${COMP_WORDS[i]} == -* ]]; then
249             case ${COMP_WORDS[i]} in
250                 --start) _koha_list_cmd ; return 0 ;;
251                  --stop) _koha_list_cmd ; return 0 ;;
252               --restart) _koha_list_cmd ; return 0 ;;
253                  --help) COMPREPLY=() ; return 0 ;; # no more completions
254                      -h) COMPREPLY=() ; return 0 ;; # no more completions
255                 --quiet) # filter the other quiet switches and go on
256                     substract="$substract -e -q"; ;;
257                 -q)      # filter the other quiet switches and go on
258                     substract="$substract -e --quiet"; ;;
259             esac
260             substract="$substract -e ${COMP_WORDS[i]}"
261         fi
262     done
263
264     if [[ "$substract" != "" ]]; then
265         opts=$( echo $opts | sed -e 's/ /\n/g'  | grep -v -x $substract )
266     fi
267
268     COMPREPLY=( $(compgen -W "$opts" -- $cur ) )
269
270     return 0
271 }
272 complete -F _koha-indexer koha-indexer
273
274 # Local variables:
275 # mode: shell-script
276 # sh-basic-offset: 4
277 # sh-indent-comment: t
278 # indent-tabs-mode: nil
279 # End:
280 # ex: ts=4 sw=4 et filetype=sh