#! /bin/sh
#
# asterisk	start the asterisk PBX
# (c) Mark Purcell <msp@debian.org>
# May be distributed under the terms of this General Public License
#
# Based on:
#
# skeleton	example file to build /etc/init.d/ scripts.
#		This file should be used to construct scripts for /etc/init.d.
#
#		Written by Miquel van Smoorenburg <miquels@cistron.nl>.
#		Modified for Debian GNU/Linux
#		by Ian Murdock <imurdock@gnu.ai.mit.edu>.
#
# Version:	@(#)skeleton  1.9  26-Feb-2001  miquels@cistron.nl
#

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=asterisk
USER=$NAME
GROUP=$USER
DAEMON=/usr/sbin/$NAME
DESC="Asterisk PBX"
PIDFILE="/var/run/asterisk/asterisk.pid"
ASTSAFE_PIDFILE="/var/run/asterisk/asterisk_safe.pid"
UMASK=007 # by default


# by default: use real-time priority
PARAMS=""
AST_REALTIME="yes"
RUNASTERISK="yes"
if [ -r /etc/default/$NAME ]; then . /etc/default/$NAME; fi

if [ "$RUNASTERISK" != "yes" ];then
	echo "Asterisk not yet configured. Edit /etc/default/asterisk first."
	exit 0
fi

if [ "$AST_REALTIME" != "no" ]
then
  PARAMS="$PARAMS -p"
fi

if [ "x$USER" = "x" ]
then
  echo "Error: empty USER name"
  exit 1
fi
if [ `id -u "$USER"` = 0 ]
then
  echo "Starting as root not supported."
  exit 1
fi
PARAMS="$PARAMS -U $USER"

if [ "x$AST_DEBUG_PARAMS" = x ] 
then
  AST_DEBUG_PARAMS=-cvvvvvddddd
fi
if [ "$RUNASTSAFE" = "yes" ];then
	# The value of WRAPPER_DAEMON in can be set in /etc/default/asterisk
	WRAPPER_DAEMON=${WRAPPER_DAEMON:-/usr/sbin/safe_asterisk}
	REALDAEMON="$WRAPPER_DAEMON"
else
	REALDAEMON="$DAEMON"
fi

test -x $DAEMON || exit 0

set -e

if [ ! -e `dirname $PIDFILE` ];then
       mkdir `dirname $PIDFILE`
       chown $USER:$GROUP `dirname $PIDFILE`
fi

if [ "$UMASK" != '' ]
then
	umask $UMASK
fi

status() {
	plist=`ps auxw | grep "$DAEMON" | grep -v grep | awk '{print $2}' | tr '\012' ' '`
	if [ "$plist" = "" ]; then
		echo "$DESC is stopped"
		return 1
	else
		echo "$DESC is running: $plist"
		return 0
	fi
}

asterisk_rx() {
	if ! status >/dev/null; then return 0; fi
	$DAEMON -rx "$1"
}

case "$1" in
  debug)
	# we add too many special parameters that I don't want to skip
	# accidentally. I'm afraid that skipping -U once may cause
	# confusing results. I also want to maintain the user's choice
	# of -p
	echo "Debugging $DESC: "
	$DAEMON $PARAMS $AST_DEBUG_PARAMS
	exit 0
	;;
  start)
	if status > /dev/null; then
		echo "$DESC is already running. Use restart."
		exit 0
	fi
	echo -n "Starting $DESC: "
	if [ "$RUNASTSAFE" != "yes" ];then
		# TODO: what if we cought the wrapper just as its asterisk
		# was killed? status should check for the wrapper if we're in
		# "safe mode"
		if status > /dev/null; then
			echo "$DESC is already running. Use restart."
			exit 0
		fi
		start-stop-daemon --start --group $GROUP --pidfile "$PIDFILE" \
			--exec $REALDAEMON -- $PARAMS
	else
		start-stop-daemon --start --group $GROUP --make-pidfile \
			--pidfile "$ASTSAFE_PIDFILE" \
			--exec $REALDAEMON -- $PARAMS
	fi
		
	
	echo "$NAME."
	;;
  stop)
	echo -n "Stopping $DESC: "
	# safe_asterisk exits automatically if asterisk shuts down cleanly, but by killing it here, we avoid its own "Asterisk ended with exit status 0...Asterisk shutdown normally." message
	if [ "$RUNASTSAFE" = "yes" ]; then
	start-stop-daemon --stop --quiet --oknodo --name `basename $WRAPPER_DAEMON`
	fi
	# Try to gracefully shut down asterisk
	# If asterisk is not running, this command will exit with an error "Unable to connect to remote asterisk" (which is redirected to /dev/null)
	( $DAEMON -rx 'stop now' > /dev/null 2>&1 & ) &
	echo -n "$NAME"
	# give a small grace time for asterisk to shut down cleanly in the background
	sleep 1
	# now poke it with a stick and whack it again if it is still alive
	if status > /dev/null; then
	# KILL is necessary just in case there's an asterisk -r in the background
	start-stop-daemon --stop --quiet --oknodo --retry=0/2/TERM/2/KILL/5 --exec $DAEMON
	fi
	echo "."
	;;
  reload)
	echo "Reloading $DESC configuration files."
	asterisk_rx 'reload'
	;;
  logger-reload)
	asterisk_rx 'logger reload'
	;;
  extensions-reload)
	echo "Reloading $DESC configuration files."
	asterisk_rx 'extensions reload'
	;;
  restart|force-reload)
	$0 stop
	$0 start
	;;
  status)
	status
	exit $?
	;;
  zaptel-fix) 	 
	echo "Unloading and reloading loading Asterisk and Zaptel:" 	 
	$0 stop 	 
	/etc/init.d/zaptel unload 	 
	# load modules from /etc/modules. This will break if you count on 	 
	# discover/hotplug 	 
	/etc/init.d/module-init-tools 	 
	/etc/init.d/zaptel start 	 
	$0 start 	 
	;; 	 
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|reload|status|debug|logger-reload|extensions-reload|force-reload}" >&2
	exit 1
	;;
esac

exit 0
