Bug 11404: add support for Apache 2.4's config file convention
[koha.git] / debian / scripts / koha-start-zebra
1 #!/bin/sh
2 #
3 # koha-start-zebra - Start 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 start_zebra_instance()
76 {
77     local instancename=$1
78
79     if is_enabled $instancename; then
80         echo "Starting Zebra server for $instancename"
81         touch "/var/log/koha/$instancename/zebra-error.log" \
82             "/var/log/koha/$instancename/zebra.log" \
83             "/var/log/koha/$instancename/zebra-output.log"
84         chown "$instancename-koha:$instancename-koha" \
85             "/var/log/koha/$instancename/zebra-error.log" \
86             "/var/log/koha/$instancename/zebra.log" \
87             "/var/log/koha/$instancename/zebra-output.log"
88         daemon \
89             --name="$instancename-koha-zebra" \
90             --errlog="/var/log/koha/$instancename/zebra-error.log" \
91             --stdout="/var/log/koha/$instancename/zebra.log" \
92             --output="/var/log/koha/$instancename/zebra-output.log" \
93             --verbose=1 \
94             --respawn \
95             --delay=30 \
96             --user="$instancename-koha.$instancename-koha" \
97             -- \
98             zebrasrv \
99             -v none,fatal,warn \
100             -f "/etc/koha/sites/$instancename/koha-conf.xml" && \
101         return 0
102     else
103         return 1
104     fi
105 }
106
107 usage()
108 {
109     local scriptname=$0
110     cat <<EOF
111 Starts Zebra for Koha instances.
112
113 Usage: $scriptname instancename1 instancename2...
114
115 EOF
116 }
117
118 # Parse command line.
119 #[ $# -ge 1 ] || ( usage ; die "Missing instance name..." )
120
121 # Loop through the instance names
122 for name in "$@"
123 do
124     if is_instance $name ; then
125         if is_enabled $name ; then
126             if ! is_zebra_running $name; then
127                 if ! start_zebra_instance $name; then
128                     warn "Something went wrong starting Zebra for $name."
129                 fi
130             else
131                 warn "Zebra already running for instance $name."
132             fi
133         else
134             warn "Instance $name disabled. No action taken."
135         fi
136     else
137         warn "Unknown instance $name."
138     fi
139 done
140
141 exit 0