#! /bin/sh
# Copyright (c) 1995-2003 SuSE Linux AG, Nuernberg, Germany.
# All rights reserved.
#
# Author: Stefan Behlert <feedback@suse.de>, based on
#         proposals and templates from Marcel Holtmann
#         and Stefan Reinauer <feedback@suse.de>
#
# /etc/init.d/bluetooth
#   and its symbolic link
# /(usr/)sbin/rcbluetooth
#
### BEGIN INIT INFO
# Provides:          bluetooth
# Required-Start:    $syslog
# X-UnitedLinux-Should-Start: dbus
# Required-Stop:     $syslog
# X-UnitedLinux-Should-Stop:
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: Bluetooth protocol stack services
# Description:       Bluetooth protocol stack services
#	This services is needed to use bluetooth devices
### END INIT INFO
# 

# Check for existence of needed config file and read it
BLUETOOTH_CONFIG=/etc/sysconfig/bluetooth
test -r $BLUETOOTH_CONFIG || exit 6
. $BLUETOOTH_CONFIG

. /etc/rc.status

# Reset status of this service
rc_reset
	 
#	check_service "hcid" "$HCI_START" "$HCI_DAEMON"
function check_service()
{
	if [ "$2" = "yes" ]; then
		printf "%10s  (%s)" "$1" "activated"
		/sbin/checkproc $3 || rc_failed 7
		rc_status -v
	else
		printf "%10s" "$1"
		/sbin/checkproc $3 || rc_failed 3
		rc_status -v
	fi
}

# we use '/var/lib/bluetooth/opd' as default-path for the opd. we make sure, the dir
# exists and we don't want to have the dir growing and growing
function check_opd_default_path()
{
	if [ ! -e /var/lib/bluetooth/opd ] && [ ! -d /var/lib/bluetooth/opd ]; then
		return
	fi
	# I don't like -exec
	# remove all files older than 3 days
	find /var/lib/bluetooth/opd/ -type f -a -mtime +3 |xargs -I{} /bin/rm {};
}


case "$1" in
    start)
	echo -n "Starting bluetooth: "

	# not generic, because that was to much overhead
	if [ $HCI_START = "yes" ] && [ -x "$HCI_DAEMON" ] ; then
		/sbin/checkproc $HCI_DAEMON || $HCI_DAEMON  $HCI_DAEMON_ARGS
		echo -n " hcid"
	fi
	if [ $SDP_START = "yes" ] && [ -x "$SDP_DAEMON" ] ; then
		/sbin/checkproc $SDP_DAEMON || $SDP_DAEMON $SDP_DAEMON_ARGS
		echo -n " sdpd"
	fi
	if [ $HID_START = "yes" ] && [ -x "$HID_DAEMON" ] ; then
		/sbin/checkproc $HID_DAEMON || $HID_DAEMON $HID_DAEMON_ARGS
		echo -n " hidd"
	fi
	if [ $HID2HCI_START = "yes" ] && [ -x "$HID2HCI_DAEMON" ] ; then
		$HID2HCI_DAEMON $HID2HCI_DAEMON_ARGS > /dev/null 2>&1
		echo -n " hid2hci"
	fi
	if [ $OPD_START = "yes" ] && [ -x "$OPD_DAEMON" ] ; then
		check_opd_default_path
		/sbin/checkproc $OPD_DAEMON || $OPD_DAEMON $OPD_DAEMON_ARGS
		echo -n " opd"
	fi
	if [ $RFCOMM_START = "yes" ] && [ -x "$RFCOMM_DAEMON" ] ; then
		$RFCOMM_DAEMON $RFCOMM_DAEMON_ARGS
		echo -n " rfcomm"
	fi
	if [ $PAND_START = "yes" ] && [ -x "$PAND_DAEMON" ] ; then
		/sbin/checkproc $PAND_DAEMON || $PAND_DAEMON $PAND_DAEMON_ARGS
		echo -n " pand"
	fi
	if [ $DUND_START = "yes" ] && [ -x "$DUND_DAEMON" ] ; then
		/sbin/checkproc $DUND_DAEMON || $DUND_DAEMON $DUND_DAEMON_ARGS
		echo -n " dund"
	fi

	rc_status -v
	;;
    stop)
	echo -n "Shutting down bluetooth ("

	if [ -x $DUND_DAEMON ]; then
		/sbin/killproc -TERM $DUND_DAEMON && echo -n " dund"
	fi
	if [ -x $PAND_DAEMON ]; then
		/sbin/killproc -TERM $PAND_DAEMON && echo -n " pand"
	fi
	if [ -x $RFCOMM_DAEMON ]; then
		/sbin/killproc -TERM $RFCOMM_DAEMON && echo -n " rfcomm"
	fi
	if [ -x $OPD_DAEMON ]; then
		/sbin/killproc -TERM $OPD_DAEMON && echo -n " opd"
	fi
	if [ -x $HID2HCI_DAEMON ]; then
		/sbin/killproc -TERM $HID2HCI_DAEMON && echo -n " hid2hci"
	fi
	if [ -x $HID_DAEMON ]; then
		/sbin/killproc -TERM $HID_DAEMON && echo -n " hidd"
	fi
	if [ -x $SDP_DAEMON ]; then
		/sbin/killproc -TERM $SDP_DAEMON && echo -n " sdp"
	fi
	if [ -x $HCI_DAEMON ]; then
		/sbin/killproc -TERM $HCI_DAEMON && echo -n " hcid"
	fi
	echo -n ")"
	rc_status -v
	;;
    try-restart)
	$0 status >/dev/null &&  $0 restart
	rc_status
	;;
    restart)
	$0 stop
	$0 start
	rc_status
	;;
    force-reload)
	echo -n "Reload service BLUETOOTH "
	$0 stop  &&  $0 start
	rc_status
	;;
    reload)
	rc_failed 3
	rc_status -v
	;;
    status)
	echo -n "Checking service bluetooth "
	if [ $START_SERVICES = 'no' ] ; then
		echo "(disabled) :"
	else
		echo "(enabled) :"
	fi;
	check_service "hcid"	"$HCI_START"	"$HCI_DAEMON"
	check_service "hidd"	"$HID_START"	"$HID_DAEMON"
	check_service "hid2hci"	"$HID2HCI_START"	"$HID2HCI_DAEMON"
	check_service "sdpd"	"$SDP_START"	"$SDP_DAEMON"
	check_service "opd"	"$OPD_START"	"$OPD_DAEMON"
	check_service "rfcomm"	"$RFCOMM_START"	"$RFCOMM_DAEMON"
	check_service "pand"	"$PAND_START"	"$PAND_DAEMON"
	check_service "dund"	"$DUND_START"	"$DUND_DAEMON"
	if [ $START_SERVICES = 'no' ] ; then
		rc_failed 3
	else
		rc_failed 0
	fi;

	;;
    probe)
	## Optional: Probe for the necessity of a reload, print out the
	## argument to this init script which is required for a reload.
	## Note: probe is not (yet) part of LSB (as of 1.2)
	# test /etc/FOO/FOO.conf -nt /var/run/FOO.pid && echo reload
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
	exit 1
	;;
esac
rc_exit

