#! /bin/sh
# Copyright (C) 2005 Open Source Telecom Corporation.  
#
# 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.
#
# bayonne         This shell script takes care of starting and stopping
#                 a bayonne server running as a system service.
#
# chkconfig: 2345 98 10
# description: Bayonne is the telephony server of the GNU  \
# project and offers interactive voice response.   
#
# For LSB compatibility:
#
### BEGIN INIT INFO
# Provides: bayonne
# Required-Start: $remote_fs
# Required-Stop:  $remote_fs
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Description:    Bayonne is the telephony server of the GNU project and offers interactive voice response.
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON="/usr/sbin/bayonne"
NAME=bayonne
DESC=bayonne
FIFO="/var/run/bayonne/bayonne.ctrl"
DRIVER=""
CONFIG=""
LIBEXEC=""
TIMESLOTS=""
BINDING=""
OPTIONS=""

test -x $DAEMON || exit 0

DAEMON_OPTS="-p"

set -e
. /etc/rc.status

if test -f /etc/sysconfig/bayonne ; then
        source /etc/sysconfig/bayonne
elif test -f /etc/conf.d/bayonne ; then
        source /etc/conf.d/bayonne
elif test -f /etc/site-start.d/bayonne ; then
        source /etc/site-start.d/bayonne
elif test -f /etc/defaults/bayonne.conf ; then
        source /etc/defaults/bayonne.conf
fi

if test ! -z "$CONFIG" ; then
	FIFO=/var/run/bayonne-$CONFIG/bayonne.ctrl
	export CONFIG ; fi

if test ! -z "$DRIVER" ; then
	export DRIVER ; fi

if test ! -z "$LIBEXEC" ; then
	export LIBEXEC ; fi

if test ! -z "$TIMESLOTS" ; then
	export TIMESLOTS ; fi

if test ! -z "$BINDING" ; then
	export BINDING ; fi

case "$1" in
  start)

        if test "$DRIVER"x = "soundcard"x ; then
                exit 0 ; fi

        echo -n "Starting $DESC: "

	DAEMON_PRELOAD=""
	if test -f /usr/dialogic/lib/libgc.so ; then
		DAEMON_PRELOAD=/usr/lib64/libLiS.so:/usr/lib/libLiS.so:/usr/dialogic/lib/libgc.so:/usr/dialogic/lib/libdevmap.so 
	elif test -f /usr/lib64/libLiS.so ; then
		DAEMON_PRELOAD=/usr/lib64/libLiS.so
	elif test -f /usr/lib/libLiS.so ; then
		DAEMON_PRELOAD=/usr/lib/libLiS.so
	fi

	if test -d /usr/dialogic ; then
		export DRIVER_AUTODETECT="dialogic" ; fi

	if test ! -z "$DAEMON_PRELOAD" ; then
		if test -z "$LD_PRELOAD" ; then
			LD_PRELOAD="$DAEMON_PRELOAD"
		else
			LD_PRELOAD="$LD_PRELOAD:$DAEMON_PRELOAD"
		fi
		export LD_PRELOAD
	fi

	if test ! -z "$OPTIONS" ; then
		DAEMON_OPTS="$OPTIONS" ; fi

	startproc $DAEMON $DAEMON_OPTS
	RETVAL=$?
 	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/bayonne
	echo

	# Remember status and be verbose
	rc_status -v
	;;
  stop)
	echo -n "Shutting down service bayonne: "
	## Stop daemon with killproc(8) and if this fails
	## set echo the echo return value.

	killproc -TERM $bayonne_BIN
	RETVAL=$?
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/bayonne

	# Remember status and be verbose
	rc_status -v
	;;
    restart)
	## Stop the service and regardless of whether it was
	## running or not, start it again.
	$0 stop
	$0 start

	# Remember status and be quiet
	rc_status
	;;
    force-reload)
	## Signal the daemon to reload its config. Most daemons
	## do this on signal 1 (SIGHUP).
	## If it does not support it, restart.

	echo -n "Reload service bayonne"
	## if it supports it:
	killproc -HUP $bayonne_BIN
	#touch /var/run/bayonne.pid
	rc_status -v

	## Otherwise:
	#$0 stop  &&  $0 start
	#rc_status
	;;
  reload)
	## Like force-reload, but if daemon does not support
	## signalling, do nothing (!)

	# If it supports signalling:
	echo -n "Reload service bayonne"
	killproc -HUP $bayonne_BIN
	#touch /var/run/bayonne.pid
	rc_status -v
	
	## Otherwise if it does not support reload:
	#rc_failed 3
	#rc_status -v
	;;
  try-restart)
	## Stop the service and if this succeeds (i.e. the 
	## service was running before), start it again.
	## Note: try-restart is not (yet) part of LSB (as of 0.7.5)
	$0 status >/dev/null &&  $0 restart

	# Remember status and be quiet
	rc_status
	;;
    status)
	echo -n "Checking for bayonne: "
	## Check status with checkproc(8), if process is running
	## checkproc will return with exit status 0.

	# Status has a slightly different for the status command:
	# 0 - service running
	# 1 - service dead, but /var/run/  pid  file exists
	# 2 - service dead, but /var/lock/ lock file exists
	# 3 - service not running

	# NOTE: checkproc returns LSB compliant status values.
	checkproc $bayonne_BIN
	rc_status -v
	;;
    probe)
	## Optional: Probe for the necessity of a reload,
	## give out the argument which is required for a reload.

	test /etc/bayonne.conf -nt /var/run/bayonne.pid && echo reload
	;;
  *)
	N=/etc/init.d/$NAME
	# echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
	exit 1
	;;
esac

exit 0
