bug 10548: fix count of missing required dependencies by koha_perl_deps.pl
[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 get_instances()
53 {
54     find /etc/koha/sites -mindepth 1 -maxdepth 1\
55                          -type d -printf '%f\n' | sort
56 }
57
58 show_instances()
59 {
60     local show=$1
61     local show_email=$2
62
63     for instance in $( get_instances ); do
64         case $show in
65           "all")
66               show_instance_filter_email $instance $show_email;;
67           "enabled")
68               if is_enabled $instance; then
69                   show_instance_filter_email $instance $show_email
70               fi ;;
71           "disabled")
72               if ! is_enabled $instance; then
73                   show_instance_filter_email $instance $show_email
74               fi ;;
75         esac
76     done
77 }
78
79 show_instance_filter_email()
80 {
81     local instancename=$1
82     local show_email=$2;
83
84     case $show_email in
85         "all")
86             echo $instancename ;;
87         "enabled")
88             if is_email_enabled $instancename; then
89                 echo $instancename
90             fi ;;
91         "disabled")
92             if ! is_email_enabled $instancename; then
93                 echo $instancename
94             fi ;;
95     esac
96 }
97
98 set_show()
99 {
100     local show_param=$1
101
102     if [ "$show" = "all" ]; then
103         show=$show_param
104     else
105         die "Error: --enabled and --disabled are mutually exclusive."
106     fi
107 }
108
109 set_show_email()
110 {
111     local email_param=$1
112
113     if [ "$show_email" = "all" ]; then
114         show_email=$email_param
115     else
116         die "Error: --email and --noemail are mutually exclusive."
117     fi
118 }
119
120 usage()
121 {
122     local scriptname=$0
123
124     echo <<eoh
125 Lists Koha instances, optionally only those that are enabled or have
126 email turned on.
127     
128 Usage: $scriptname [--enabled|--disabled] [--email|--noemail] [-h]
129 Options:
130     --enabled       only show instances that are enabled
131     --disabled      only show instances that are disabled
132     --email         only show instances that have email enabled
133     --noemail       only show instances that do not have email enabled
134     -h              this help
135
136 The filtering options can be combined, and you probably want to do this
137 (except --email and --noemail, or --enabled and --disabled, that's just silly.)
138 eoh
139 }
140
141 show="all"
142 show_email="all"
143
144 args=$(getopt -l enabled,disabled,email,noemail -o h -n $0 -- "$@")
145 set -- $args
146
147 while [ ! -z "$1" ]
148 do
149     case "$1" in
150          -h) usage; exit;;
151     --email) set_show_email "enabled" ;;
152   --noemail) set_show_email "disabled" ;;
153   --enabled) set_show "enabled" ;;
154  --disabled) set_show "disabled" ;;
155           *) break;;
156     esac
157     shift
158 done
159
160 show_instances $show $show_email
161
162 exit 0