Bug 11404: add support for Apache 2.4's config file convention
[koha.git] / debian / scripts / koha-stop-zebra
1 #!/bin/sh
2 #
3 # koha-stop-zebra - Stop Zebra for named 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 set -e
20
21 die()
22 {
23     echo "$@" 1>&2
24     exit 1
25 }
26
27 warn()
28 {
29     echo "$@" 1>&2
30 }
31
32 is_enabled()
33 {
34     local instancename=$1
35     local instancefile="/etc/apache2/sites-available/$instancename.conf"
36
37     if ! is_instance $instancename; then
38         return 1
39     fi
40
41     if grep -q '^[[:space:]]*Include /etc/koha/apache-shared-disable.conf' \
42             "$instancefile" ; then
43         return 1
44     else
45         return 0
46     fi
47 }
48
49 is_instance()
50 {
51     local instancename=$1
52
53     if find /etc/koha/sites -mindepth 1 -maxdepth 1 \
54                          -type d -printf '%f\n'\
55           | grep -q -x $instancename ; then
56         return 0
57     else
58         return 1
59     fi
60 }
61
62 is_zebra_running()
63 {
64     local instancename=$1
65
66     if daemon --name="$instancename-koha-zebra" \
67             --user="$instancename-koha.$instancename-koha" \
68             --running ; then
69         return 0
70     else
71         return 1
72     fi
73 }
74
75 stop_zebra_instance()
76 {
77     local instancename=$1
78
79     if is_zebra_running $instancename; then
80         echo "Stopping Zebra server for $instancename"
81         daemon \
82             --name="$instancename-koha-zebra" \
83             --errlog="/var/log/koha/$instancename/zebra-error.log" \
84             --stdout="/var/log/koha/$instancename/zebra.log" \
85             --output="/var/log/koha/$instancename/zebra-output.log" \
86             --verbose=1 \
87             --respawn \
88             --delay=30 \
89             --user="$instancename-koha.$instancename-koha" \
90             --stop \
91             -- \
92             zebrasrv \
93             -v none,fatal,warn \
94             -f "/etc/koha/sites/$instancename/koha-conf.xml" && \
95         return 0
96     else
97         return 1
98     fi
99 }
100
101 usage()
102 {
103     local scriptname=$0
104     cat <<EOF
105 Stops Zebra for Koha instances.
106
107 Usage: $scriptname instancename1 instancename2...
108
109 EOF
110 }
111
112 # Parse command line.
113 #[ $# -ge 1 ] || ( usage ; die "Missing instance name..." )
114
115 # Loop through the instance names
116 for name in "$@"
117 do
118     if is_instance $name ; then
119         if is_enabled $name ; then
120             if is_zebra_running $name; then
121                 if ! stop_zebra_instance $name; then
122                     warn "Something went wrong stopping Zebra for $name."
123                 fi
124             else
125                 warn "Zebra already stopped for instance $name."
126             fi
127         else
128             warn "Instance $name disabled. No action taken."
129         fi
130     else
131         warn "Unknown instance $name."
132     fi
133 done
134
135 exit 0