#!/bin/sh
### BEGIN INIT INFO
# Provides:				sipwitch
# Required-Start:		$named $network $syslog $remote_fs
# Required-Stop:		$named $network $syslog $remote_fs
# Default-Start:		2 3 4 5
# Default-Stop:			0 1 6
# Short-Description:	Start and stop sipwitch service daemon.
# Description:			This script manages startup and shutdown for
#						GNU SIP Witch, a SIP telephony service daemon.
### END INIT INFO
#
# Copyright (C) 2008 David Sugar, Tycho Softworks.  
#
# This file is free software; as a special exception the author gives
# unlimited permission to copy and/or distribute it, with or without
# modifications, as long as this notice is preserved.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# sipwitch        This shell script takes care of starting and stopping
#                 a sipwitch server running as a system service.
#
# chkconfig: - 95 15
# description: GNU SIP Witch telephony service.

DAEMON="/usr/sbin/sipw"
NAME="sipwitch"
DESC="sipwitch"
FIFO="/var/run/sipwitch/control"
OPTIONS="-d"
PRELOAD=""
PLUGINS=""
GROUP=""
CONCURRENCY=""
PRIORITY=""
DEFAULT="/etc/default/sipwitch"
START="1"

test -x "$DAEMON" || exit 0

set -e

if [ -f /etc/sysconfig/sipwitch ] ; then
	DEFAULT="/etc/sysconfig/sipwitch"
	. /etc/sysconfig/sipwitch
elif [ -f /etc/default/sipwitch ] ; then
	. /etc/default/sipwitch
elif [ -f /etc/conf.d/sipwitch ] ; then
	. /etc/conf.d/sipwitch
elif [ -f /etc/site-start.d/sipwitch ] ; then
	. /etc/site-start.d/sipwitch
fi

if [ ! -z "$PLUGINS" ] ; then
	export PLUGINS
else
	START="0"
fi

if [ ! -z "$GROUP" ] ; then
	export GROUP ; fi

if [ ! -z "$CONCURRENCY" ] ; then
	export CONCURRENCY ; fi

if [ ! -z "$PRIORITY" ] ; then
	export PRIORITY ; fi

if [ ! -z "$PRELOAD" ] ; then		
	if [ -z "$LD_PRELOAD" ] ; then
		LD_PRELOAD="$PRELOAD"
	else
		LD_PRELOAD="$LD_PRELOAD:$PRELOAD"
	fi
	export LD_PRELOAD
fi

case "$1" in
start)
	if [ "$START" = "0" ] ; then
		if test -z "$PLUGINS" ; then
			echo "You have to define PLUGINS in $DEFAULT before running sipwitch"
		else
			echo "You have to modify config and START in $DEFAULT before running"
		fi
		exit 0
	fi
	
	if [ -p $FIFO ] ; then
		echo "already started"
		exit 1
	fi

	echo -n "Starting $DESC: "
	$DAEMON $OPTIONS
	echo "$NAME."
	if test -d /var/lock/subsys ; then
		touch /var/lock/subsys/sipwitch ; fi
	;;
stop)
	if [ ! -p $FIFO ] ; then
		exit 0 ; fi

	echo -n "Stopping $DESC: "
	echo "down" >$FIFO
	echo "$NAME."
	if test -d /var/lock/subsys ; then
		rm -f /var/lock/subsys/sipwitch ; fi
	;;
status)
	sipwitch status
	;;
condrestart|try-restart)
	if [ -p $FIFO ] ; then
		echo "down" >$FIFO
		sleep 3
		$DAEMON $OPTIONS
	fi
	;;
reload)
	if [ ! -p $FIFO ] ; then
		exit 0 ; fi
	echo -n "Reloading $DESC: "
	echo "reload" >$FIFO
	echo "$NAME."
	;;
restart|force-reload)
	echo -n "Restarting $DESC: "
	if [ -p $FIFO ] ; then
		echo "down" >$FIFO
		sleep 3
	fi
	$DAEMON $OPTIONS
	echo "$NAME."
	;;
*)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0




