#!/bin/sh
#
# bluetooth    Bluetooth subsystem starting and stopping
#
# chkconfig: 345 25 90
# description: Bluetooth subsystem
#

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

# Source Bluetooth configuration.
#. /etc/sysconfig/bluetooth

HCID=/usr/sbin/hcid
SDPD=/usr/sbin/sdpd
[ -x "$HCID" ] || exit

LOCKFILE=/var/lock/subsys/bluetooth
RETVAL=0

prog="Bluetooth"

UART_CONF="/etc/bluetooth/uart"

start_uarts()
{
	[ -f /usr/sbin/hciattach -a -f $UART_CONF ] || return
	grep -v '^#' $UART_CONF | while read i; do
		/usr/sbin/hciattach $i
	done
}

stop_uarts()
{
	killall hciattach > /dev/null 2>&1
}

start() 
{
        echo -n $"Starting $prog: "
	start_daemon --lockfile "$LOCKFILE" $HCID
	[ -x $SDPD ] && start_daemon $SDPD
	RETVAL=$?
	/usr/bin/rfcomm bind all ||:
	start_uarts
	echo
}

stop() 
{
        echo -n $"Shutting down $prog: "
	/usr/bin/rfcomm release all ||:
	[ -x $SDPD ] && killall sdpd
	stop_daemon --lockfile "$LOCKFILE" $HCID
        RETVAL=$?
	stop_uarts
	rmmod rfcomm l2cap ||:
        echo
}

# See how we were called.
case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        reload|restart)
                restart
                ;;
        condstop)
                if [ -e "$LOCKFILE" ]; then
                        stop
                fi
                ;;
        condrestart)
                if [ -e "$LOCKFILE" ]; then
                        restart
                fi
                ;;
        status)
                status "$HCID"
                ;;
        *)
                echo "Usage: ${0##*/} {start|stop|reload|restart|condstop|condrestart|status}"
                RETVAL=1
esac

exit $RETVAL
