Bug 13791: tab-completion for koha-plack in bash
[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_list_cmd()
47 {
48     local filter=$1
49
50     local cur substract instancelist
51     _get_comp_words_by_ref cur
52
53     # Build a list of the already used words
54     substract=`_build_substract_instances`
55
56     if [[ "$substract" != "" ]]; then
57         instancelist=$( koha-list $filter | grep -v -x $substract )
58     else
59         instancelist=$( koha-list $filter )
60     fi
61
62     COMPREPLY=( $(compgen -W "$instancelist" -- $cur ) )
63     return 0
64 }
65
66 _koha_email_disable()
67 {
68     _koha_list_cmd "--email"
69     return 0
70 }
71 complete -F _koha_email_disable koha-email-disable
72
73 _koha_email_enable()
74 {
75     _koha_list_cmd "--noemail"
76     return 0
77 }
78 complete -F _koha_email_enable koha-email-enable
79
80 _koha_sip_enabled_instances()
81 {
82     _koha_list_cmd "--sip"
83     return 0
84 }
85
86 # koha-*-sip autocomplete with sip-enabled instances
87 complete -F _koha_sip_enabled_instances koha-start-sip
88 complete -F _koha_sip_enabled_instances koha-restart-sip
89 complete -F _koha_sip_enabled_instances koha-stop-sip
90
91 _koha_sip_disabled()
92 {
93     _koha_list_cmd "--nosip"
94     return 0
95 }
96
97 # koha-enable-sip autocompletes with sip-disabled instances
98 complete -F _koha_sip_disabled koha-enable-sip
99
100 _koha_disabled_instances()
101 {
102     _koha_list_cmd "--disabled"
103     return 0
104 }
105
106 _koha_enabled_instances()
107 {
108     _koha_list_cmd "--enabled"
109     return 0
110 }
111
112 # koha-enable autocompletes with disabled instances
113 complete -F _koha_disabled_instances koha-enable
114
115 # koha-disable autocompletes with enabled instances
116 complete -F _koha_enabled_instances koha-disable
117
118 # koha-*-zebra autocomplete with enabled instances
119 complete -F _koha_enabled_instances koha-start-zebra
120 complete -F _koha_enabled_instances koha-restart-zebra
121 complete -F _koha_enabled_instances koha-stop-zebra
122
123 _koha_list()
124 {
125     local cur opts substract
126
127     COMPREPLY=()
128     _get_comp_words_by_ref cur
129     opts="--enabled --disabled --email --noemail --plack --noplack --sip --nosip --help -h"
130
131     # Build a list of the already used option switches
132     for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
133         if [[ ${COMP_WORDS[i]} == -* ]]; then
134             case ${COMP_WORDS[i]} in
135                 --disabled)
136                     substract="$substract -e --enabled"; ;;
137                 --enabled)
138                     substract="$substract -e --disabled"; ;;
139                 --email)
140                     substract="$substract -e --noemail"; ;;
141                 --noemail)
142                     substract="$substract -e --email"; ;;
143                 --plack)
144                     substract="$substract -e --noplack"; ;;
145                 --noplack)
146                     substract="$substract -e --plack"; ;;
147                 --sip)
148                     substract="$substract -e --nosip"; ;;
149                 --nosip)
150                     substract="$substract -e --sip"; ;;
151                 --help)
152                     substract="$substract -e -h"; ;;
153                 -h)
154                     substract="$substract -e --help"; ;;
155             esac
156             substract="$substract -e ${COMP_WORDS[i]}"
157         fi
158     done
159
160     if [[ "$substract" != "" ]]; then
161         opts=$( echo $opts | sed -e 's/ /\n/g'  | grep -v -x $substract )
162     fi
163
164     COMPREPLY=( $(compgen -W "$opts" -- $cur ) )
165
166     return 0
167 }
168 complete -F _koha_list koha-list
169
170 _koha_plack_instances()
171 {
172     _koha_list_cmd "--plack"
173     return 0
174 }
175
176 _koha_noplack_instances()
177 {
178     _koha_list_cmd "--noplack"
179     return 0
180 }
181
182 _koha-plack()
183 {
184     local cur opts substract
185
186     COMPREPLY=()
187     _get_comp_words_by_ref cur
188     opts="--start --stop --restart --enable --disable --quiet -q --help -h"
189
190     # Build a list of the already used option switches
191     for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
192         if [[ ${COMP_WORDS[i]} == -* ]]; then
193             case ${COMP_WORDS[i]} in
194                 --start) _koha_plack_instances ; return 0 ;;
195                  --stop) _koha_plack_instances ; return 0 ;;
196               --restart) _koha_plack_instances ; return 0 ;;
197                --enable) _koha_noplack_instances ; return 0 ;;
198               --disable) _koha_plack_instances ; return 0 ;;
199                  --help) COMPREPLY=() ; return 0 ;; # no more completions
200                      -h) COMPREPLY=() ; return 0 ;; # no more completions
201                 --quiet) # filter the other quiet switches and go on
202                     substract="$substract -e '--quiet' -e '-q'"; ;;
203                 -q)      # filter the other quiet switches and go on
204                     substract="$substract -e '--quiet' -e '-q'"; ;;
205             esac
206             substract="$substract -e ${COMP_WORDS[i]}"
207         fi
208     done
209
210     if [[ "$substract" != "" ]]; then
211         opts=$( echo $opts | sed -e 's/ /\n/g'  | grep -v -x $substract )
212     fi
213
214     COMPREPLY=( $(compgen -W "$opts" -- $cur ) )
215
216     return 0
217 }
218 complete -F _koha-plack koha-plack
219
220 # Local variables:
221 # mode: shell-script
222 # sh-basic-offset: 4
223 # sh-indent-comment: t
224 # indent-tabs-mode: nil
225 # End:
226 # ex: ts=4 sw=4 et filetype=sh