Merge branch 'master' of ssh://jolly@www.mISDN.org/var/git/lcr
[lcr.git] / debian / lcr.init
1 #! /bin/sh
2 #
3 # lcr   start the lcr isdn PBX
4 # (c) Joerg Dorchain <joerg@dorchain.net>
5 #
6 #   This package is free software; you can redistribute it and/or
7 #   modify
8 #   it under the terms of the GNU General Public License as
9 #   published by
10 #   the Free Software Foundation; either version 2 of the
11 #   License, or
12 #   (at your option) any later version.
13 #
14 # Based on:
15 #
16 # skeleton      example file to build /etc/init.d/ scripts.
17 #               This file should be used to construct scripts for
18 #               /etc/init.d.
19 #
20 #               Written by Miquel van Smoorenburg
21 #               <miquels@cistron.nl>.
22 #               Modified for Debian GNU/Linux
23 #               by Ian Murdock <imurdock@gnu.ai.mit.edu>.
24 #
25 # Version:      @(#)skeleton  1.9  26-Feb-2001
26 # miquels@cistron.nl
27 #
28
29 ### BEGIN INIT INFO
30 # Provides:          lcr
31 # Required-Start:    $remote_fs
32 # Required-Stop:     $remote_fs
33 # Default-Start:     2 3 4 5
34 # Default-Stop:      0 1 6
35 # Short-Description: lcr PBX
36 # Description:       Controls the Linux Call Router ISDN PBX
37 ### END INIT INFO
38
39 # PATH should only include /usr/* if it runs after the mountnfs.sh script
40 PATH=/sbin:/usr/sbin:/bin:/usr/bin
41 DESC="lcr PBX"
42 NAME=lcr
43 DAEMON=/usr/sbin/$NAME
44 DAEMON_ARGS="fork"
45 PIDFILE=/var/run/$NAME.pid
46 SCRIPTNAME=/etc/init.d/$NAME
47
48 RUNLCR=yes
49
50 # Exit if the package is not installed
51 [ -x "$DAEMON" ] || exit 0
52
53 # Read configuration variable file if it is present
54 [ -r /etc/default/$NAME ] && . /etc/default/$NAME
55
56 if [ "$RUNLCR" != "yes" ];then
57   echo "lcr not yet configured. Edit /etc/default/lcr first."
58   exit 0
59 fi
60
61 # Load the VERBOSE setting and other rcS variables
62 . /lib/init/vars.sh
63
64 # Define LSB log_* functions.
65 # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
66 . /lib/lsb/init-functions
67
68 status() {
69         plist=`ps ax | awk "{ if (\\$5 == \"$DAEMON\") print \\$1
70 }"`
71         if [ "$plist" = "" ]; then
72                 echo "$DESC is stopped"
73                 return 1
74         else
75                 echo "$DESC is running: $plist"
76                 return 0
77         fi
78 }
79
80 #
81 # Function that starts the daemon/service
82 #
83 do_start()
84 {
85         # Return
86         #   0 if daemon has been started
87         #   1 if daemon was already running
88         #   2 if daemon could not be started
89         if status > /dev/null; then
90                 echo "$DESC is already running. Use restart."
91                 exit 1
92         fi
93
94         start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
95                 || return 1
96         start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
97                 $DAEMON_ARGS \
98                 || return 2
99         # Add code here, if necessary, that waits for the process to be ready
100         # to handle requests from services started subsequently which depend
101         # on this one.  As a last resort, sleep for some time.
102 }
103
104 #
105 # Function that stops the daemon/service
106 #
107 do_stop()
108 {
109         # Return
110         #   0 if daemon has been stopped
111         #   1 if daemon was already stopped
112         #   2 if daemon could not be stopped
113         #   other if a failure occurred
114         start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
115         RETVAL="$?"
116         [ "$RETVAL" = 2 ] && return 2
117         # Wait for children to finish too if this is a daemon that forks
118         # and if the daemon is only ever run from this initscript.
119         # If the above conditions are not satisfied then add some other code
120         # that waits for the process to drop all resources that could be
121         # needed by services started subsequently.  A last resort is to
122         # sleep for some time.
123         start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
124         [ "$?" = 2 ] && return 2
125         # Many daemons don't delete their pidfiles when they exit.
126         rm -f $PIDFILE
127         return "$RETVAL"
128 }
129
130 #
131 # Function that sends a SIGHUP to the daemon/service
132 #
133 do_reload() {
134         #
135         # If the daemon can reload its configuration without
136         # restarting (for example, when it is sent a SIGHUP),
137         # then implement that here.
138         #
139         #start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
140         lcradmin route
141         lcradmin interface
142         return 0
143 }
144
145 case "$1" in
146   start)
147         [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
148         do_start
149         case "$?" in
150                 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
151                 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
152         esac
153         ;;
154   stop)
155         [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
156         do_stop
157         case "$?" in
158                 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
159                 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
160         esac
161         ;;
162   reload)
163         #
164         # If do_reload() is not implemented then leave this commented out
165         # and leave 'force-reload' as an alias for 'restart'.
166         #
167         log_daemon_msg "Reloading $DESC" "$NAME"
168         do_reload
169         log_end_msg $?
170         ;;
171   status)
172         status
173         ;;
174   restart|force-reload)
175         #
176         # If the "reload" option is implemented then remove the
177         # 'force-reload' alias
178         #
179         log_daemon_msg "Restarting $DESC" "$NAME"
180         do_stop
181         case "$?" in
182           0|1)
183                 do_start
184                 case "$?" in
185                         0) log_end_msg 0 ;;
186                         1) log_end_msg 1 ;; # Old process is still running
187                         *) log_end_msg 1 ;; # Failed to start
188                 esac
189                 ;;
190           *)
191                 # Failed to stop
192                 log_end_msg 1
193                 ;;
194         esac
195         ;;
196   *)
197         #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
198         echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
199         exit 3
200         ;;
201 esac
202
203 :