Bug 30511: Don't lock up entire database while running koha-dump
[koha.git] / debian / scripts / koha-list
1 #!/bin/sh
2 #
3 # koha-list -- List all Koha instances.
4 # Copyright 2010  Catalyst IT, Ltd
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
20 set -e
21
22 # include helper functions
23 if [ -f "/usr/share/koha/bin/koha-functions.sh" ]; then
24     . "/usr/share/koha/bin/koha-functions.sh"
25 else
26     echo "Error: /usr/share/koha/bin/koha-functions.sh not present." 1>&2
27     exit 1
28 fi
29
30 show_instances()
31 {
32     local show=$1
33     local show_email=$2
34     local show_sip=$3
35
36     for instance in $( get_instances ); do
37         case $show in
38           "all")
39               if instance_filter_email       $instance $show_email && \
40                  instance_filter_letsencrypt $instance $show_letsencrypt && \
41                  instance_filter_plack       $instance $show_plack && \
42                  instance_filter_z3950       $instance $show_z3950 && \
43                  instance_filter_sip         $instance $show_sip; then
44                     echo $instance
45               fi ;;
46           "enabled")
47               if is_enabled $instance; then
48                   if instance_filter_email       $instance $show_email && \
49                      instance_filter_letsencrypt $instance $show_letsencrypt && \
50                      instance_filter_plack       $instance $show_plack && \
51                      instance_filter_z3950       $instance $show_z3950 && \
52                      instance_filter_sip         $instance $show_sip; then
53                       echo $instance
54                   fi
55               fi ;;
56           "disabled")
57               if ! is_enabled $instance; then
58                   if instance_filter_email       $instance $show_email && \
59                      instance_filter_letsencrypt $instance $show_letsencrypt && \
60                      instance_filter_plack       $instance $show_plack && \
61                      instance_filter_z3950       $instance $show_z3950 && \
62                      instance_filter_sip         $instance $show_sip; then
63                       echo $instance
64                   fi
65               fi ;;
66         esac
67     done
68 }
69
70
71 instance_filter_sip()
72 {
73     local instancename=$1
74     local show_sip=$2;
75
76     case $show_sip in
77         "all")
78             return 0 ;;
79         "enabled")
80             if is_sip_enabled $instancename; then
81                 return 0
82             fi ;;
83         "disabled")
84             if ! is_sip_enabled $instancename; then
85                 return 0
86             fi ;;
87     esac
88
89     # Didn't match any criteria
90     return 1
91 }
92
93 instance_filter_plack()
94 {
95     local instancename=$1
96     local show_plack=$2;
97
98     case $show_plack in
99         "all")
100             return 0 ;;
101         "enabled")
102             if is_plack_enabled $instancename; then
103                 return 0
104             fi ;;
105         "disabled")
106             if ! is_plack_enabled $instancename; then
107                 return 0
108             fi ;;
109     esac
110
111     # Didn't match any criteria
112     return 1
113 }
114
115 instance_filter_letsencrypt()
116 {
117     local instancename=$1
118     local show_letsencrypt=$2;
119
120     case $show_letsencrypt in
121         "all")
122             return 0 ;;
123         "enabled")
124             if is_letsencrypt_enabled $instancename; then
125                 return 0
126             fi ;;
127         "disabled")
128             if ! is_letsencrypt_enabled $instancename; then
129                 return 0
130             fi ;;
131     esac
132
133     # Didn't match any criteria
134     return 1
135 }
136
137 instance_filter_email()
138 {
139     local instancename=$1
140     local show_email=$2;
141
142     case $show_email in
143         "all")
144             return 0 ;;
145         "enabled")
146             if is_email_enabled $instancename; then
147                 return 0
148             fi ;;
149         "disabled")
150             if ! is_email_enabled $instancename; then
151                 return 0
152             fi ;;
153     esac
154
155     # Didn't match any criteria
156     return 1
157 }
158
159 instance_filter_z3950()
160 {
161     local instancename=$1
162     local show_z3950=$2;
163
164     case $show_z3950 in
165         "all")
166             return 0 ;;
167         "enabled")
168             if is_z3950_enabled $instancename; then
169                 return 0
170             fi ;;
171         "disabled")
172             if ! is_z3950_enabled $instancename; then
173                 return 0
174             fi ;;
175     esac
176
177     # Didn't match any criteria
178     return 1
179 }
180
181 set_show()
182 {
183     local show_param=$1
184
185     if [ "$show" = "all" ]; then
186         show=$show_param
187     else
188         die "Error: --enabled and --disabled are mutually exclusive."
189     fi
190 }
191
192 set_show_email()
193 {
194     local email_param=$1
195
196     if [ "$show_email" = "all" ]; then
197         show_email=$email_param
198     else
199         die "Error: --email and --noemail are mutually exclusive."
200     fi
201 }
202
203 set_show_letsencrypt()
204 {
205     local letsencrypt_param=$1
206
207     if [ "$show_letsencrypt" = "all" ]; then
208         show_letsencrypt=$letsencrypt_param
209     else
210         die "Error: --letsencrypt and --noletsencrypt are mutually exclusive."
211     fi
212 }
213
214 set_show_plack()
215 {
216     local plack_param=$1
217
218     if [ "$show_plack" = "all" ]; then
219         show_plack=$plack_param
220     else
221         die "Error: --plack and --noplack are mutually exclusive."
222     fi
223 }
224
225 set_show_sip()
226 {
227     local sip_param=$1
228
229     if [ "$show_sip" = "all" ]; then
230         show_sip=$sip_param
231     else
232         die "Error: --sip and --nosip are mutually exclusive."
233     fi
234 }
235
236 set_show_z3950()
237 {
238     local z3950_param=$1
239
240     if [ "$show_z3950" = "all" ]; then
241         show_z3950=$z3950_param
242     else
243         die "Error: --z3950 and --noz3950 are mutually exclusive."
244     fi
245 }
246
247 usage()
248 {
249     local scriptname=$0
250
251     cat <<EOH
252 Lists Koha instances, optionally only those that are enabled or have
253 email turned on.
254     
255 Usage: $scriptname [--enabled|--disabled] [--email|--noemail] [--sip|--nosip] [-h]
256 Options:
257     --enabled       Show enabled instances
258     --disabled      Show disabled instances
259     --email         Show instances with email enabled
260     --noemail       Show instances with email disabled
261     --sip           Show instances with SIP enabled
262     --nosip         Show instances with SIP disabled
263     --plack         Show instances with Plack enabled
264     --noplack       Show instances with Plack disabled
265     --letsencrypt   Show instances with letsencrypt enabled
266     --noletsencrypt Show instances with letsencrypt disabled
267     --z3950         Show instances with Z39.50/SRU enabled
268     --noz3950       Show instances with Z39.50/SRU disabled
269     --help | -h     Show this help
270
271 The filtering options can be combined, and you probably want to do this
272 (except --email and --noemail, or --enabled and --disabled, that's just silly.)
273 EOH
274 }
275
276 show="all"
277 show_email="all"
278 show_sip="all"
279 show_plack="all"
280 show_letsencrypt="all"
281 show_z3950="all"
282
283 args=$(getopt -l help,enabled,disabled,email,noemail,sip,nosip,plack,noplack,letsencrypt,noletsencrypt,z3950,noz3950 -o h -n $0 -- "$@")
284 set -- $args
285
286 while [ ! -z "$1" ]
287 do
288     case "$1" in
289       -h|--help) usage; exit;;
290         --email) set_show_email "enabled" ;;
291       --noemail) set_show_email "disabled" ;;
292           --sip) set_show_sip "enabled" ;;
293         --nosip) set_show_sip "disabled" ;;
294         --plack) set_show_plack "enabled" ;;
295       --noplack) set_show_plack "disabled" ;;
296   --letsencrypt) set_show_letsencrypt "enabled" ;;
297 --noletsencrypt) set_show_letsencrypt "disabled" ;;
298         --z3950) set_show_z3950 "enabled" ;;
299       --noz3950) set_show_z3950 "disabled" ;;
300       --enabled) set_show "enabled" ;;
301      --disabled) set_show "disabled" ;;
302               *) break;;
303     esac
304     shift
305 done
306
307 show_instances $show $show_email $show_sip
308
309 exit 0