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