2e7364e3dc7161e53136d3d1ce8b891a0af186e7
[citadel.git] / citadel / debian / citadel.init
1 #!/bin/sh
2 ### BEGIN INIT INFO
3 # Provides:          citadel
4 # Required-Start:    $local_fs $remote_fs
5 # Required-Stop:     $local_fs $remote_fs
6 # Default-Start:     2 3 4 5
7 # Default-Stop:      0 1 6
8 # Short-Description: control citadel server start at boot time
9 # Description:       control citadel server start at boot time
10 ### END INIT INFO
11
12 # uncomment this to create coredumps as described in
13 # http://www.citadel.org/doku.php/faq:mastering_your_os:gdb#how.do.i.make.my.system.produce.core-files
14 # ulimit -c unlimited
15
16 # Author: Wilfried Goesgens <citadel@outgesourced.org>
17
18 RUNDIR=/var/run/citadel
19 PATH=/sbin:/usr/sbin:/bin:/usr/bin
20 DESC="Citadel Groupware "
21 NAME=citserver
22 DAEMON=/usr/sbin/$NAME
23 PIDFILE=$RUNDIR/citadel.pid
24 DAEMON_ARGS=" -d -x3 -lmail -t/dev/null"
25 SCRIPTNAME=/etc/init.d/citadel
26 SENDCOMMAND=/usr/sbin/sendcommand
27
28 # Exit if the package is not installed
29 [ -x "$DAEMON" ] || exit 0
30
31 # Read configuration variable file if it is present
32 [ -r /etc/default/$NAME ] && . /etc/default/$NAME
33
34 MODERN=
35
36 # Load the VERBOSE setting and other rcS variables
37 if test -f /lib/init/vars.sh ; then
38     . /lib/init/vars.sh
39     MODERN=1
40 fi
41 # Define LSB log_* functions.
42 # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
43 if test -f /lib/lsb/init-functions; then
44     . /lib/lsb/init-functions
45     MODERN=1
46 fi
47
48 #
49 # Function that starts the daemon/service
50 #
51 do_start()
52 {
53         # for ubuntu: check our volatile dirs.
54         if test ! -d $RUNDIR; then
55             mkdir -p $RUNDIR
56             chmod go+rx $RUNDIR
57         fi
58         # Return
59         #   0 if daemon has been started
60         #   1 if daemon was already running
61         #   2 if daemon could not be started
62         if $DAEMON \
63                 $DAEMON_ARGS ; then
64             return 0
65         else
66             return 2
67         fi
68 }
69
70 #
71 # Function that stops the daemon/service
72 #
73 do_stop()
74 {
75         # Return
76         #   0 if daemon has been stopped
77         #   1 if daemon was already stopped
78         #   2 if daemon could not be stopped
79         #   other if a failure occurred
80         if $SENDCOMMAND "DOWN" >/dev/null 2>&1 ; then
81             if test ! -f "$PIDFILE"; then 
82                 echo Unable to find Citserver. Not running?
83                 return 1
84             fi
85             PID=`cat $PIDFILE`
86             rm -f $PIDFILE
87             count=0;
88             while test -S /var/run/citadel/citadel.socket -o -d /proc/$PID; do 
89                 count=$(($count+1))
90                 sleep 1
91                 echo -n "."
92                 if test "$count" = "10"; then
93                     kill $PID
94                 fi
95                 if test "$count" = "20"; then
96                     kill -9 $PID
97                     rm -rf /var/run/citadel/*
98                 fi
99             done
100                 return 0
101         else
102             rm -f $PIDFILE
103             return 2
104         fi
105 }
106
107 #
108 # Function that sends a SIGHUP to the daemon/service
109 #
110 do_reload() {
111         # Return
112         #   0 if daemon could not be restarted
113         #   1 if daemon has been restarted
114         #   other if a failure occurred
115         if $SENDCOMMAND "DOWN 1" 2>&1|grep '200 Restarting'>/dev/null ; then
116             return 1
117         fi
118         return 0
119 }
120
121 case "$1" in
122   start)
123         if test -n "$MODERN"; then
124             [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
125         else
126             echo "Starting $DESC" "$NAME"
127         fi
128
129         do_start
130         if test -n "$MODERN"; then
131             case "$?" in
132                 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
133                 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
134             esac
135         fi
136         ;;
137   stop)
138         if test -n "$MODERN"; then
139             [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
140         else
141             echo "Stopping $DESC" "$NAME"
142         fi
143         do_stop
144         if test -n "$MODERN"; then
145             case "$?" in
146                 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
147                 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
148             esac
149         fi
150         ;;
151   restart|force-reload)
152         if test -n "$MODERN"; then
153             log_daemon_msg "Restarting $DESC" "$NAME"
154         else
155             echo "Restarting $DESC" "$NAME"
156         fi
157
158         do_reload
159
160         if test -n "$MODERN"; then
161             case "$?" in
162                 0)
163                     log_end_msg 0
164                     ;;
165                 1)
166                 # Failed to stop
167                     log_end_msg 1
168                     ;;
169             esac
170         fi
171         ;;
172   *)
173         echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
174         exit 3
175         ;;
176 esac
177
178 exit 0