Bug 14361: koha-restart-zebra fails and probably breaks upgrade
[koha.git] / debian / scripts / koha-functions.sh
1 #!/bin/sh
2 #
3 # koha-functions.sh -- shared library of helper functions for koha-* scripts
4 # Copyright 2014 - Tomas Cohen Arazi
5 #                  Universidad Nacional de Cordoba
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20
21 die()
22 {
23     echo "$@" 1>&2
24     exit 1
25 }
26
27 warn()
28 {
29     echo "$@" 1>&2
30 }
31
32 get_apache_config_for()
33 {
34     local site=$1
35     local sitefile="/etc/apache2/sites-available/$site"
36
37     if is_instance $site; then
38         if [ -f "$sitefile.conf" ]; then
39             echo "$sitefile.conf"
40         elif [ -f "$sitefile" ]; then
41             echo "$sitefile"
42         fi
43     fi
44 }
45
46 is_enabled()
47 {
48     local site=$1
49     local instancefile=$(get_apache_config_for $site)
50
51     if [ "$instancefile" = "" ]; then
52         return 1
53     fi
54
55     if grep -q '^[[:space:]]*Include /etc/koha/apache-shared-disable.conf' \
56             "$instancefile" ; then
57         return 1
58     else
59         return 0
60     fi
61 }
62
63 is_instance()
64 {
65     local instancename=$1
66
67     if find /etc/koha/sites -mindepth 1 -maxdepth 1 \
68                          -type d -printf '%f\n'\
69           | grep -q -x "$instancename" ; then
70         return 0
71     else
72         return 1
73     fi
74 }
75
76 is_email_enabled()
77 {
78     local instancename=$1
79
80     if [ -e /var/lib/koha/$instancename/email.enabled ]; then
81         return 0
82     else
83         return 1
84     fi
85 }
86
87 is_sip_enabled()
88 {
89     local instancename=$1
90
91     if [ -e /etc/koha/sites/$instancename/SIPconfig.xml ]; then
92         return 0
93     else
94         return 1
95     fi
96 }
97
98 is_zebra_running()
99 {
100     local instancename=$1
101
102     if daemon --name="$instancename-koha-zebra" \
103             --pidfiles="/var/run/koha/$instancename/" \
104             --user="$instancename-koha.$instancename-koha" \
105             --running ; then
106         return 0
107     else
108         return 1
109     fi
110 }
111
112 is_indexer_running()
113 {
114     local instancename=$1
115
116     if daemon --name="$instancename-koha-indexer" \
117             --pidfiles="/var/run/koha/$instancename/" \
118             --user="$instancename-koha.$instancename-koha" \
119             --running ; then
120         return 0
121     else
122         return 1
123     fi
124 }
125
126 is_plack_enabled()
127 {
128     local site=$1
129     local instancefile=$(get_apache_config_for $site)
130
131     if [ "$instancefile" = "" ]; then
132         return 1
133     fi
134
135     if grep -q '^[[:space:]]*Include /etc/koha/apache-shared-opac-plack.conf' \
136             "$instancefile" && \
137        grep -q '^[[:space:]]*Include /etc/koha/apache-shared-intranet-plack.conf' \
138             "$instancefile" ; then
139         return 0
140     else
141         return 1
142     fi
143 }
144
145 is_plack_running()
146 {
147     local instancename=$1
148
149     if start-stop-daemon --pidfile "/var/run/koha/${instancename}/plack.pid" \
150             --status ; then
151         return 0
152     else
153         return 1
154     fi
155 }
156
157 get_instances()
158 {
159     find /etc/koha/sites -mindepth 1 -maxdepth 1\
160                          -type d -printf '%f\n' | sort
161 }