7fb213df9fc502c5c0587e28fe78c16ee251fd94
[citadel.git] / citadel / debian / citadel.init
1 #!/bin/sh
2 ### BEGIN INIT INFO
3 # Provides:          citadel
4 # Required-Start:    $remote_fs $syslog
5 # Required-Stop:     $remote_fs $syslog
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 -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" ] || { echo "$DAEMON not installed"; exit 1; }
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         # 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         if test -S $RUNDIR/citadel.socket; then 
76             sendcommand "DOWN" || :
77         fi
78
79         PID=`cat $PIDFILE 2>/dev/null || :`
80         if [ ! -z $PID ]; then
81             rm -f $PIDFILE
82             count=0;
83             while test -S /var/run/citadel/citadel.socket -o -d /proc/$PID; do 
84                 count=$(($count+1))
85                 sleep 1
86                 echo -n "."
87                 if test "$count" = "10"; then
88                     kill $PID
89                 fi
90                 if test "$count" = "20"; then
91                     kill -9 $PID
92                     rm -rf /var/run/citadel/*
93                 fi
94             done
95                 return 0
96         fi
97
98         return 0
99 }
100
101 #
102 # Function that sends a SIGHUP to the daemon/service
103 #
104 do_reload() {
105         # Return
106         #   0 if daemon could not be restarted
107         #   1 if daemon has been restarted
108         #   other if a failure occurred
109         if $SENDCOMMAND "DOWN 1" 2>&1|grep '200 Restarting'>/dev/null ; then
110             return 1
111         fi
112         return 0
113 }
114
115 case "$1" in
116   start)
117         if test -n "$MODERN"; then
118             [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
119         else
120             echo "Starting $DESC" "$NAME"
121         fi
122
123         do_start
124         if test -n "$MODERN"; then
125             case "$?" in
126                 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
127                 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
128             esac
129         fi
130         ;;
131   stop)
132         if test -n "$MODERN"; then
133             [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
134         else
135             echo "Stopping $DESC" "$NAME"
136         fi
137         do_stop
138         if test -n "$MODERN"; then
139                 [ "$VERBOSE" != no ] && log_end_msg 0
140         fi
141         ;;
142   restart)
143         $0 stop
144         $0 start
145         ;;
146   force-reload)
147         if test -n "$MODERN"; then
148             log_daemon_msg "Restarting $DESC" "$NAME"
149         else
150             echo "Restarting $DESC" "$NAME"
151         fi
152
153         do_reload
154
155         if test -n "$MODERN"; then
156             case "$?" in
157                 0)
158                     log_end_msg 0
159                     ;;
160                 1)
161                 # Failed to stop
162                     log_end_msg 1
163                     ;;
164             esac
165         fi
166         ;;
167
168   status)
169         status_of_proc "$DAEMON" $NAME
170         ;;
171
172   *)
173         echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
174         exit 3
175         ;;
176 esac
177
178 exit 0