Bug 22516: Remove remaining calls to lastincrement
[koha.git] / debian / scripts / koha-sip
1 #!/bin/bash
2
3 # koha-sip - Manage SIP server for Koha instances
4 #              Copyright 2019 Theke Solutions
5 #              Copyright 2012 Catalyst IT, Ltd
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 set -e
21
22 . /lib/lsb/init-functions
23
24 # Read configuration variable file if it is present
25 [ -r /etc/default/koha-common ] && . /etc/default/koha-common
26
27 # include helper functions
28 if [ -f "/usr/share/koha/bin/koha-functions.sh" ]; then
29     . "/usr/share/koha/bin/koha-functions.sh"
30 else
31     echo "Error: /usr/share/koha/bin/koha-functions.sh not present." 1>&2
32     exit 1
33 fi
34
35 usage()
36 {
37     local scriptname=$(basename $0)
38
39     cat <<EOF
40 $scriptname
41
42 This script lets you manage the SIP server for your Koha instances.
43
44 Usage:
45 $scriptname [--start|--stop|--restart] instancename1 [instancename2...]
46 $scriptname -h|--help
47
48     --start               Start the SIP server for the specified instance(s)
49     --stop                Stop the SIP server for the specified instance(s)
50     --restart             Restart the SIP server for the specified instance(s)
51     --status              Show the status of the SIP server for the specified instance(s)
52     --verbose|-v          Display progress and actions messages
53     --help|-h             Display this help message
54
55 EOF
56 }
57
58 start_sip()
59 {
60     local name=$1
61
62     _check_and_fix_perms $name
63
64     if ! is_sip_running $name; then
65
66         adjust_paths_dev_install $name
67         export KOHA_HOME PERL5LIB
68
69         if [ "$DEV_INSTALL" = "" ]; then
70             LIBDIR=$KOHA_HOME/lib
71         else
72             LIBDIR=$KOHA_HOME
73         fi
74
75         DAEMONOPTS="--name=${name}-koha-sip \
76                     --errlog=/var/log/koha/${name}/sip-error.log \
77                     --stdout=/var/log/koha/${name}/sip.log \
78                     --output=/var/log/koha/${name}/sip-output.log \
79                     --verbose=1 \
80                     --respawn \
81                     --delay=30 \
82                     --pidfiles=/var/run/koha/${name} \
83                     --user=${name}-koha.${name}-koha"
84
85         SIP_PARAMS="$LIBDIR/C4/SIP/SIPServer.pm \
86                     /etc/koha/sites/${name}/SIPconfig.xml"
87
88         [ "$verbose" != "no" ] && \
89             log_daemon_msg "Starting SIP server for ${name}"
90
91         if daemon $DAEMONOPTS -- perl $SIP_PARAMS; then
92             ([ "$verbose" != "no" ] && \
93                 log_end_msg 0) || return 0
94         else
95             ([ "$verbose" != "no" ] && \
96                 log_end_msg 1) || return 1
97         fi
98     else
99         if [ "$verbose" != "no" ]; then
100             log_daemon_msg "Warning: SIP server already running for ${name}"
101             log_end_msg 0
102         else
103             return 0
104         fi
105     fi
106 }
107
108 stop_sip()
109 {
110     local name=$1
111
112     if is_sip_running $name; then
113
114         DAEMONOPTS="--name=${name}-koha-sip \
115                     --errlog=/var/log/koha/${name}/sip-error.log \
116                     --stdout=/var/log/koha/${name}/sip.log \
117                     --output=/var/log/koha/${name}/sip-output.log \
118                     --verbose=1 \
119                     --respawn \
120                     --delay=30 \
121                     --pidfiles=/var/run/koha/${name} \
122                     --user=${name}-koha.${name}-koha"
123
124         [ "$verbose" != "no" ] && \
125             log_daemon_msg "Stopping SIP server for ${name}"
126
127         if daemon $DAEMONOPTS --stop; then
128             ([ "$verbose" != "no" ] && \
129                 log_end_msg 0) || return 0
130         else
131             ([ "$verbose" != "no" ] && \
132                 log_end_msg 1) || return 1
133         fi
134     else
135         if [ "$verbose" != "no" ]; then
136             log_daemon_msg "Warning: SIP server not running for ${name}"
137             log_end_msg 0
138         else
139             return 0
140         fi
141     fi
142 }
143
144 restart_sip()
145 {
146     local name=$1
147
148     if is_sip_running ${name}; then
149         local noLF="-n"
150         [ "$verbose" != "no" ] && noLF=""
151         echo $noLF `stop_sip ${name}`
152         echo $noLF `start_sip ${name}`
153     else
154         if [ "$verbose" != "no" ]; then
155             log_daemon_msg "Warning: SIP server not running for ${name}"
156             log_end_msg 0
157         else
158             return 0
159         fi
160     fi
161 }
162
163 sip_status()
164 {
165     local name=$1
166
167     if is_sip_running ${name}; then
168         log_daemon_msg "SIP server running for ${name}"
169         log_end_msg 0
170     else
171         log_daemon_msg "SIP server not running for ${name}"
172         log_end_msg 3
173     fi
174 }
175
176 enable_sip()
177 {
178     local name=$1
179
180     sipfile=/etc/koha/sites/${name}/SIPconfig.xml
181
182     if is_sip_enabled ${name}; then
183         echo "Warning: SIP server already enabled for ${name}"
184     else
185         echo "Enabling SIP server for ${name} - edit ${sipfile} to configure"
186         cp -v /etc/koha/SIPconfig.xml ${sipfile}
187         chown ${name}-koha:${name}-koha ${sipfile}
188         chmod 600 ${sipfile}
189     fi
190 }
191
192 _check_and_fix_perms()
193 {
194     local name=$1
195
196     local files="/var/log/koha/${name}/sip-error.log \
197                  /var/log/koha/${name}/sip.log \
198                  /var/log/koha/$name/sip-output.log"
199
200     for file in ${files}
201     do
202         if [ ! -e "${file}" ]; then
203             touch ${file}
204         fi
205         chown "${name}-koha":"${name}-koha" ${file}
206     done
207 }
208
209 set_action()
210 {
211     if [ "$op" = "" ]; then
212         op=$1
213     else
214         die "Error: only one action can be specified."
215     fi
216 }
217
218 op=""
219 verbose="no"
220
221 # Backwards compatible with old koha-*-sip scripts
222 # TODO: Remove once there's consensus to remove the legacy scripts
223 used_script_name=$(basename $0)
224
225 if [ "$used_script_name" != "koha-sip" ]; then
226     warn "Deprecated script used (${used_script_name})"
227
228     case "$used_script_name" in
229         koha-start-sip)
230             set_action "start" ;;
231         koha-stop-sip)
232             set_action "stop" ;;
233         koha-enable-sip)
234             set_action "enable" ;;
235         *)
236             break ;;
237     esac
238 fi
239 # / Backwards compatible handling code
240
241 # Read command line parameters
242 while [ $# -gt 0 ]; do
243
244     case "$1" in
245         -h|--help)
246             usage ; exit 0 ;;
247         -v|--verbose)
248             verbose="yes"
249             shift ;;
250         --start)
251             set_action "start"
252             shift ;;
253         --stop)
254             set_action "stop"
255             shift ;;
256         --restart)
257             set_action "restart"
258             shift ;;
259         --status)
260             set_action "status"
261             shift ;;
262         --enable)
263             set_action "enable"
264             shift ;;
265         -*)
266             die "Error: invalid option switch ($1)" ;;
267         *)
268             # We expect the remaining stuff are the instance names
269             break ;;
270     esac
271
272 done
273
274 if [ $# -gt 0 ]; then
275     # We have at least one instance name
276     for name in "$@"; do
277
278         if is_instance $name; then
279
280             export KOHA_CONF="/etc/koha/sites/$name/koha-conf.xml"
281
282             case $op in
283                 "start")
284                     start_sip $name
285                     ;;
286                 "stop")
287                     stop_sip $name
288                     ;;
289                 "restart")
290                     restart_sip $name
291                     ;;
292                 "status")
293                     sip_status $name
294                     ;;
295                 "enable")
296                     enable_sip $name
297             esac
298
299         else
300             if [ "$verbose" != "no" ]; then
301                 log_daemon_msg "Error: Invalid instance name $name"
302                 log_end_msg 1
303             fi
304         fi
305
306     done
307 else
308     if [ "$verbose" != "no" ]; then
309         warn "Error: you must provide at least one instance name"
310     fi
311 fi
312
313 exit 0