#!/bin/sh

# This file is part of asterisk-phonepatch

# Copyright (C) 2006 Arnau Sanchez
#
# Asterisk-phonepatch is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License or any later version.

CONF="/etc/asterisk/phonepatch.conf"
DAEMON="/usr/sbin/asterisk-phonepatch"
DAEMON_OPTIONS="--background"
DAEMON_NAME="Asterisk-Phonepatch daemon"
PIDFILEDIR="/var/run/asterisk"

test -f $DAEMON || exit 0
test -f $CONF || exit 0

##########
#############
case "$1" in

#############
start)
	echo -n "Starting $DAEMON_NAME: "
	$DAEMON $DAEMON_OPTIONS >/dev/null
	echo $(basename $DAEMON)
	;;

#############
stop)
	echo -n "Stopping $DAEMON_NAME: "
	RESPONSE="not found"
	for PHONEPATCH in $(cat $CONF | grep "^\[.*\]$" | tr -d '[]' | grep -v "^general$" | grep -v "^[[:digit:]]"); do
		PIDFILE=$PIDFILEDIR/$PHONEPATCH.pid
		PID=$(cat $PIDFILE   2>/dev/null)
		if [ "$PID" = "" ]; then continue; fi
		if kill $PID &>/dev/null; then RESPONSE="done"; fi
	done
	echo $RESPONSE
	;;
	
#############
restart|force-reload)
	$0 stop
	$0 start
	;;

#############
*)
	echo "usage: $(basename $0) start|stop|restart|force-reload"
	exit 1
	;;
esac

exit 0
