Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[koha-equinox.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_sip         $instance $show_sip; then
43                     echo $instance
44               fi ;;
45           "enabled")
46               if is_enabled $instance; then
47                   if instance_filter_email       $instance $show_email && \
48                      instance_filter_letsencrypt $instance $show_letsencrypt && \
49                      instance_filter_plack       $instance $show_plack && \
50                      instance_filter_sip         $instance $show_sip; then
51                       echo $instance
52                   fi
53               fi ;;
54           "disabled")
55               if ! is_enabled $instance; then
56                   if instance_filter_email       $instance $show_email && \
57                      instance_filter_letsencrypt $instance $show_letsencrypt && \
58                      instance_filter_plack       $instance $show_plack && \
59                      instance_filter_sip         $instance $show_sip; then
60                       echo $instance
61                   fi
62               fi ;;
63         esac
64     done
65 }
66
67
68 instance_filter_sip()
69 {
70     local instancename=$1
71     local show_sip=$2;
72
73     case $show_sip in
74         "all")
75             return 0 ;;
76         "enabled")
77             if is_sip_enabled $instancename; then
78                 return 0
79             fi ;;
80         "disabled")
81             if ! is_sip_enabled $instancename; then
82                 return 0
83             fi ;;
84     esac
85
86     # Didn't match any criteria
87     return 1
88 }
89
90 instance_filter_plack()
91 {
92     local instancename=$1
93     local show_plack=$2;
94
95     case $show_plack in
96         "all")
97             return 0 ;;
98         "enabled")
99             if is_plack_enabled $instancename; then
100                 return 0
101             fi ;;
102         "disabled")
103             if ! is_plack_enabled $instancename; then
104                 return 0
105             fi ;;
106     esac
107
108     # Didn't match any criteria
109     return 1
110 }
111
112 instance_filter_letsencrypt()
113 {
114     local instancename=$1
115     local show_letsencrypt=$2;
116
117     case $show_letsencrypt in
118         "all")
119             return 0 ;;
120         "enabled")
121             if is_letsencrypt_enabled $instancename; then
122                 return 0
123             fi ;;
124         "disabled")
125             if ! is_letsencrypt_enabled $instancename; then
126                 return 0
127             fi ;;
128     esac
129
130     # Didn't match any criteria
131     return 1
132 }
133
134 instance_filter_email()
135 {
136     local instancename=$1
137     local show_email=$2;
138
139     case $show_email in
140         "all")
141             return 0 ;;
142         "enabled")
143             if is_email_enabled $instancename; then
144                 return 0
145             fi ;;
146         "disabled")
147             if ! is_email_enabled $instancename; then
148                 return 0
149             fi ;;
150     esac
151
152     # Didn't match any criteria
153     return 1
154 }
155
156 set_show()
157 {
158     local show_param=$1
159
160     if [ "$show" = "all" ]; then
161         show=$show_param
162     else
163         die "Error: --enabled and --disabled are mutually exclusive."
164     fi
165 }
166
167 set_show_email()
168 {
169     local email_param=$1
170
171     if [ "$show_email" = "all" ]; then
172         show_email=$email_param
173     else
174         die "Error: --email and --noemail are mutually exclusive."
175     fi
176 }
177
178 set_show_letsencrypt()
179 {
180     local letsencrypt_param=$1
181
182     if [ "$show_letsencrypt" = "all" ]; then
183         show_letsencrypt=$letsencrypt_param
184     else
185         die "Error: --letsencrypt and --noletsencrypt are mutually exclusive."
186     fi
187 }
188
189 set_show_plack()
190 {
191     local plack_param=$1
192
193     if [ "$show_plack" = "all" ]; then
194         show_plack=$plack_param
195     else
196         die "Error: --plack and --noplack are mutually exclusive."
197     fi
198 }
199
200 set_show_sip()
201 {
202     local sip_param=$1
203
204     if [ "$show_sip" = "all" ]; then
205         show_sip=$sip_param
206     else
207         die "Error: --sip and --nosip are mutually exclusive."
208     fi
209 }
210
211 usage()
212 {
213     local scriptname=$0
214
215     cat <<EOH
216 Lists Koha instances, optionally only those that are enabled or have
217 email turned on.
218     
219 Usage: $scriptname [--enabled|--disabled] [--email|--noemail] [--sip|--nosip] [-h]
220 Options:
221     --enabled       Show enabled instances
222     --disabled      Show disabled instances
223     --email         Show instances with email enabled
224     --noemail       Show instances with email disabled
225     --sip           Show instances with SIP enabled
226     --nosip         Show instances with SIP disabled
227     --plack         Show instances with Plack enabled
228     --noplack       Show instances with Plack disabled
229     --letsencrypt   Show instances with letsencrypt enabled
230     --noletsencrypt Show instances with letsencrypt disabled
231     --help | -h     Show this help
232
233 The filtering options can be combined, and you probably want to do this
234 (except --email and --noemail, or --enabled and --disabled, that's just silly.)
235 EOH
236 }
237
238 show="all"
239 show_email="all"
240 show_sip="all"
241 show_plack="all"
242 show_letsencrypt="all"
243
244 args=$(getopt -l help,enabled,disabled,email,noemail,sip,nosip,plack,noplack,letsencrypt,noletsencrypt -o h -n $0 -- "$@")
245 set -- $args
246
247 while [ ! -z "$1" ]
248 do
249     case "$1" in
250       -h|--help) usage; exit;;
251         --email) set_show_email "enabled" ;;
252       --noemail) set_show_email "disabled" ;;
253           --sip) set_show_sip "enabled" ;;
254         --nosip) set_show_sip "disabled" ;;
255         --plack) set_show_plack "enabled" ;;
256       --noplack) set_show_plack "disabled" ;;
257   --letsencrypt) set_show_letsencrypt "enabled" ;;
258 --noletsencrypt) set_show_letsencrypt "disabled" ;;
259       --enabled) set_show "enabled" ;;
260      --disabled) set_show "disabled" ;;
261               *) break;;
262     esac
263     shift
264 done
265
266 show_instances $show $show_email $show_sip
267
268 exit 0