* evaluate the return of fgets() to silence some warnings
[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         fi
57         # Return
58         #   0 if daemon has been started
59         #   1 if daemon was already running
60         #   2 if daemon could not be started
61         if $DAEMON \
62                 $DAEMON_ARGS ; then
63             return 0
64         else
65             return 2
66         fi
67 }
68
69 #
70 # Function that stops the daemon/service
71 #
72 do_stop()
73 {
74         # Return
75         #   0 if daemon has been stopped
76         #   1 if daemon was already stopped
77         #   2 if daemon could not be stopped
78         #   other if a failure occurred
79         if $SENDCOMMAND "DOWN" >/dev/null 2>&1 ; then
80             if test ! -f "$PIDFILE"; then 
81                 echo Unable to find Citserver. Not running?
82                 return 1
83             fi
84             PID=`cat $PIDFILE`
85             rm -f $PIDFILE
86             count=0;
87             while test -S /var/run/citadel/citadel.socket -o -d /proc/$PID; do 
88                 count=$(($count+1))
89                 sleep 1
90                 echo -n "."
91                 if test "$count" = "10"; then
92                     kill $PID
93                 fi
94                 if test "$count" = "20"; then
95                     kill -9 $PID
96                     rm -rf /var/run/citadel/*
97                 fi
98             done
99                 return 0
100         else
101             rm -f $PIDFILE
102             return 2
103         fi
104 }
105
106 #
107 # Function that sends a SIGHUP to the daemon/service
108 #
109 do_reload() {
110         # Return
111         #   0 if daemon could not be restarted
112         #   1 if daemon has been restarted
113         #   other if a failure occurred
114         if $SENDCOMMAND "DOWN 1" 2>&1|grep '200 Restarting'>/dev/null ; then
115             return 1
116         fi
117         return 0
118 }
119
120 case "$1" in
121   start)
122         if test -n "$MODERN"; then
123             [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
124         else
125             echo "Starting $DESC" "$NAME"
126         fi
127
128         do_start
129         if test -n "$MODERN"; then
130             case "$?" in
131                 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
132                 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
133             esac
134         fi
135         ;;
136   stop)
137         if test -n "$MODERN"; then
138             [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
139         else
140             echo "Stopping $DESC" "$NAME"
141         fi
142         do_stop
143         if test -n "$MODERN"; then
144             case "$?" in
145                 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
146                 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
147             esac
148         fi
149         ;;
150   restart|force-reload)
151         if test -n "$MODERN"; then
152             log_daemon_msg "Restarting $DESC" "$NAME"
153         else
154             echo "Restarting $DESC" "$NAME"
155         fi
156
157         do_reload
158
159         if test -n "$MODERN"; then
160             case "$?" in
161                 0)
162                     log_end_msg 0
163                     ;;
164                 1)
165                 # Failed to stop
166                     log_end_msg 1
167                     ;;
168             esac
169         fi
170         ;;
171   *)
172         echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
173         exit 3
174         ;;
175 esac
176
177 exit 0