#! /bin/sh
#
# Author:	Tzafrir Cohen <tzafrir.cohen@xorcom.com>.
#	 	op_panel developers.	
#
# Version:	@(#)skeleton  1.9.4  21-Mar-2004  miquels@cistron.nl
#

### BEGIN INIT INFO
# Provides:          op-panel
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Flash Asterisk Operator Panel
# Description:       A flash operator panel for
#                    the Asterisk PBX
### END INIT INFO

set -e

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/op_server
NAME=op-panel
DESC="Flash Operator Panel"

PIDDIR=/var/run/$NAME
PIDFILE=$PIDDIR/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
USER=asterisk
GROUP=asterisk

LOGDIR=/var/log/$NAME
CONFDIR=/etc/$NAME

OPTIONS="-d -p $PIDFILE -l $LOGDIR -c $CONFDIR"

# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0

if [ ! -d $PIDDIR ]; then
        mkdir $PIDDIR
        chown -R $USER.$GROUP $PIDDIR
        chmod -R g+rw $PIDDIR
fi

case "$1" in
  start)
	echo -n "Starting $DESC: $NAME"
	sleep 1
	start-stop-daemon --chuid asterisk --start --oknodo --pidfile $PIDFILE --startas $DAEMON -- $OPTIONS
	echo "."
	;;
  stop)
	echo -n "Stopping $DESC: $NAME"
	sleep 1
	start-stop-daemon --stop --oknodo --pidfile $PIDFILE
	echo "."
	;;
  restart|force-reload)
	#
	#	If the "reload" option is implemented, move the "force-reload"
	#	option to the "reload" entry above. If not, "force-reload" is
	#	just the same as "restart".
	#
	echo -n "Restarting $DESC: $NAME"
	start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
	sleep 2
	start-stop-daemon --chuid asterisk --start --quiet --pidfile $PIDFILE --startas $DAEMON -- $OPTIONS
	echo "."
	;;
  *)
	echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
