#!/bin/sh
# Copyright (C) 2000-2001 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.

# Source function library.
. /etc/rc.d/init.d/functions

RETVAL=0

# See how we were called.
case "$1" in
  start)
        # Auto-detect runtime driver
	FLAGS="-D"

	sym=X`lsmod | awk '{print $1}' | grep ixj`X
	if test "$sym" != "XX" ; then
		export BAYONNE_DRIVER=phonedev
		chown bayonne.bayonne /dev/phone*
	fi

	sym=X`lsmod | grep pika`X
	if test "$sym" != "XX" ; then
		export BAYONNE_DRIVER=pika
		if test -d /usr/pika ; then
			FLAGS="-D -S"
			chown -R bayonne.bayonne /usr/pika
			chown bayonne.bayonne /dev/pika*
		fi
	fi

	sym=X`lsmod | grep dlgnDriver`X
	if test "$sym" != "XX" ; then
		export BAYONNE_DRIVER=dialogic
	fi

	sym=X`lsmod | grep vpb`X
	if test "$sym" != "XX" ; then
		export BAYONNE_DRIVER=vpb
		chown bayonne.bayonne /dev/vpb*
	fi

	# Start daemons.

        gprintf "Starting bayonne: "
        daemon /usr/sbin/bayonne $FLAGS
	RETVAL=$?
 	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/bayonne
	echo
        ;;
  stop)
        # Stop daemons.
        gprintf "Shutting down bayonne: "
	killproc bayonne
	RETVAL=$?
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/bayonne
        echo
        ;;
  status)
	status bayonne
	exit $?
	;;
  restart)
	$0 stop
	$0 start
	;;
  *)
        gprintf "Usage: bayonne {start|stop|status|restart}\n"
        exit 1
esac

exit $RETVAL

