f6b0c0e0a7dd2c1527f232f98cdb559caa135a79
[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 die()
23 {
24     echo "$@" 1>&2
25     exit 1
26 }
27
28 is_enabled()
29 {
30     local instancename=$1
31
32     if grep '^[[:space:]]*Include /etc/koha/apache-shared-disable.conf' \
33             "/etc/apache2/sites-available/$instancename" > /dev/null
34     then
35         return 1
36     else
37         return 0
38     fi
39 }
40
41 is_email_enabled()
42 {
43     local instancename=$1
44
45     if [ -e /var/lib/koha/$instancename/email.enabled ]; then
46         return 0
47     else
48         return 1
49     fi
50 }
51
52 is_sip_enabled()
53 {
54     local instancename=$1
55
56     if [ -e /etc/koha/sites/$instancename/SIPconfig.xml ]; then
57         return 0
58     else
59         return 1
60     fi
61 }
62
63 get_instances()
64 {
65     find /etc/koha/sites -mindepth 1 -maxdepth 1\
66                          -type d -printf '%f\n' | sort
67 }
68
69 show_instances()
70 {
71     local show=$1
72     local show_email=$2
73     local show_sip=$3
74
75     for instance in $( get_instances ); do
76         case $show in
77           "all")
78               if instance_filter_email $instance $show_email && \
79                  instance_filter_sip $instance $show_sip; then
80                     echo $instance
81               fi ;;
82           "enabled")
83               if is_enabled $instance; then
84                   if instance_filter_email $instance $show_email && \
85                      instance_filter_sip $instance $show_sip; then
86                       echo $instance
87                   fi
88               fi ;;
89           "disabled")
90               if ! is_enabled $instance; then
91                   if instance_filter_email $instance $show_email && \
92                      instance_filter_sip $instance $show_sip; then
93                       echo $instance
94                   fi
95               fi ;;
96         esac
97     done
98 }
99
100
101 instance_filter_sip()
102 {
103     local instancename=$1
104     local show_sip=$2;
105
106     case $show_sip in
107         "all")
108             return 0 ;;
109         "enabled")
110             if is_sip_enabled $instancename; then
111                 return 0
112             fi ;;
113         "disabled")
114             if ! is_sip_enabled $instancename; then
115                 return 0
116             fi ;;
117     esac
118
119     # Didn't match any criteria
120     return 1
121 }
122
123 instance_filter_email()
124 {
125     local instancename=$1
126     local show_email=$2;
127
128     case $show_email in
129         "all")
130             return 0 ;;
131         "enabled")
132             if is_email_enabled $instancename; then
133                 return 0
134             fi ;;
135         "disabled")
136             if ! is_email_enabled $instancename; then
137                 return 0
138             fi ;;
139     esac
140
141     # Didn't match any criteria
142     return 1
143 }
144
145 set_show()
146 {
147     local show_param=$1
148
149     if [ "$show" = "all" ]; then
150         show=$show_param
151     else
152         die "Error: --enabled and --disabled are mutually exclusive."
153     fi
154 }
155
156 set_show_email()
157 {
158     local email_param=$1
159
160     if [ "$show_email" = "all" ]; then
161         show_email=$email_param
162     else
163         die "Error: --email and --noemail are mutually exclusive."
164     fi
165 }
166
167 set_show_sip()
168 {
169     local sip_param=$1
170
171     if [ "$show_sip" = "all" ]; then
172         show_sip=$sip_param
173     else
174         die "Error: --sip and --nosip are mutually exclusive."
175     fi
176 }
177
178 usage()
179 {
180     local scriptname=$0
181
182     cat <<EOH
183 Lists Koha instances, optionally only those that are enabled or have
184 email turned on.
185     
186 Usage: $scriptname [--enabled|--disabled] [--email|--noemail] [--sip|--nosip] [-h]
187 Options:
188     --enabled       Only show instances that are enabled
189     --disabled      Only show instances that are disabled
190     --email         Only show instances that have email enabled
191     --noemail       Only show instances that do not have email enabled
192     --sip           Only show instances that have sip enabled
193     --nosip         Only show instances that do not have sip enabled
194     --help | -h     Show this help
195
196 The filtering options can be combined, and you probably want to do this
197 (except --email and --noemail, or --enabled and --disabled, that's just silly.)
198 EOH
199 }
200
201 show="all"
202 show_email="all"
203 show_sip="all"
204
205 args=$(getopt -l help,enabled,disabled,email,noemail,sip,nosip -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   --enabled) set_show "enabled" ;;
217  --disabled) set_show "disabled" ;;
218           *) break;;
219     esac
220     shift
221 done
222
223 show_instances $show $show_email $show_sip
224
225 exit 0