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