#! /bin/sh
# $Id: rc.debian.asterisk 119301 2008-05-30 16:44:39Z mvanbaak $
#
# Mon Jun 04 2007 Iñaki Baz Castillo <ibc@in.ilimit.es>
# - Eliminated SAFE_ASTERISK since it doesn't work as LSB script (it could require a independent "safe_asterisk" init script).
# - Load and use the standar "/lib/lsb/init-functions".
# - Addded "--oknodo" to "start-stop-daemon" for compatibility with LSB:
#   http://www.linux-foundation.org/spec/refspecs/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
#
# Thu Nov 17 2005 Gregory Boehnlein <damin@nacs.net>
# - Reversed behavior of LD_ASSUME_KERNEL=2.4.1
# - Added detailed failure messages
#
# Sun Jul 18 2004 Gregory Boehnlein <damin@nacs.net>
# - Added test for safe_asterisk
# - Changed "stop gracefully" to "stop now"
# - Added support for -U and -G command line options
# - Modified "reload" to call asterisk -rx 'reload'


### BEGIN INIT INFO
# Provides:            asterisk
# Required-Start:    $network $syslog $named $local_fs $remote_fs
# Required-Stop:     $network $syslog $named $local_fs $remote_fs
# Should-Start:      mysql
# Should-Stop:       mysql
# Default-Start:       2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:   Asterisk PBX
# Description:         the Asterisk Open Source PBX
### END INIT INFO


PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=asterisk
DESC="Asterisk PBX"
# Full path to asterisk binary
DAEMON=/usr/sbin/asterisk
TRUE=/bin/true
DEFAULTS=/etc/default/$NAME
# Uncomment this ONLY if you know what you are doing.
# export LD_ASSUME_KERNEL=2.4.1

# Uncomment the following and set them to the user/groups that you
# want to run Asterisk as. NOTE: this requires substantial work to
# be sure that Asterisk's environment has permission to write the
# files required  for  its  operation, including logs, its comm
# socket, the asterisk database, etc.
#AST_USER="asterisk"
#AST_GROUP="asterisk"

PARAMS=""
CHDIR_PARM=""
AST_DUMPCORE="no"
AST_DUMPCORE_DIR="/var/spool/asterisk" # only used if AST_DUMPCORE != no

# do not start by default
RUN_DAEMON=no

set -e

if ! [ -x $DAEMON ] ; then
        echo "ERROR: $DAEMON not found"
        exit 0
fi

if ! [ -d /etc/asterisk ] ; then
        echo "ERROR: /etc/asterisk directory not found"
        exit 0
fi

# Load startup options if available
if [ -f $DEFAULTS ]; then
   . $DEFAULTS || true
fi

if [ "$RUN_DAEMON" != "yes" ]; then
    echo "$DESC not yet configured. Edit $DEFAULTS first."
    exit 0
fi

if [ "$AST_DUMPCORE" != "no" ]; then
    PARAMS="$PARAMS -g"
    if [ -d "$AST_DUMPCORE_DIR" ]; then
        CHDIR_PARM="--chdir $AST_DUMPCORE_DIR"
    fi
fi

# Use the LSB standar functions for services management
. /lib/lsb/init-functions


status() {
        status_of_proc  "$DAEMON" "$NAME" && return 0 || return $?
}

case "$1" in
  start)
	if status > /dev/null; then
                echo "$DESC is already running. Use restart."
                exit 0
        fi
	log_begin_msg "Starting $DESC: $NAME"
        if [ $AST_USER ] ; then
                PARAMS="$PARAMS -U $AST_USER"
        fi
        if [ $AST_GROUP ] ; then
                PARAMS="$PARAMS -G $AST_GROUP"
        fi
	PARAMS="$PARAMS -n"
	# "start-stop-daemon --oknodo" returns 0 even if Asterisk was already running (as LSB expects):
	start-stop-daemon --start --oknodo $CHDIR_PARM --exec $DAEMON -- $PARAMS
	log_end_msg $?
	;;
  stop)
	log_begin_msg "Stopping $DESC: $NAME"
	# "start-stop-daemon --oknodo" returns 0 even if Asterisk was already stopped (as LSB expects):
	start-stop-daemon --stop --oknodo --exec $DAEMON
	log_end_msg $?
	;;
  reload)
	echo "Reloading $DESC configuration files."
	$DAEMON -nrx 'reload' > /dev/null 2> /dev/null
	;;
  restart|force-reload)
	$0 stop
	sleep 2  # It needs some time to really be stopped.
	$0 start
	# "restart|force-reload" starts Asterisk and returns 0 even if Asterisk was stopped (as LSB expects).
	;;
  status)
        status
        exit $?
       ;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|reload|force-reload|status}" >&2
	exit 1
	;;
esac
