#!/bin/sh
#
# setup bluetooth daemons
#
# runlevels: geexbox, debug

[ -e /etc/bluez ] && . /etc/bluez || exit 1

[ "$BLUETOOTH" = true ] || exit 1

NAME=bluetooth
DESC="Bluetooth subsystem"

HCID_EXEC=/usr/bin/hcid
RFCOMM_EXEC=/usr/bin/rfcomm
PAND_EXEC=/usr/bin/pand
HIDD_EXEC=/usr/bin/hidd

HCID_CONFIG="/etc/bluetooth/hcid.conf"
RFCOMM_CONFIG="/etc/bluetooth/rfcomm.conf"

# Prepare audio service files
if [ "$AUDIO_ENABLE" = true ] && [ -n "$HEADSET_BTMAC" -o -n "$A2DP_BTMAC" ] ; then
  sed -i 's/.*Autostart=.*/Autostart=true/' /etc/bluetooth/audio.service
  AUDIO_CONFIG=/etc/asound.conf
  echo "" > $AUDIO_CONFIG
  [ -n "$HEADSET_BTMAC" ] && echo -e "pcm.headset {\n\ttype bluetooth\n\tdevice $HEADSET_BTMAC\n}\n" >> $AUDIO_CONFIG
  [ -n "$HEADSET_BTMAC" ] && echo -e "ctrl.headset {\n\ttype bluetooth\n\tdevice $HEADSET_BTMAC\n}\n" >> $AUDIO_CONFIG
  [ -n "$A2DP_BTMAC" ] && echo -e "pcm.bluetooth {\n\ttype bluetooth\n\tdevice $A2DP_BTMAC\n}\n" >> $AUDIO_CONFIG
fi

if [ "$HCID_ENABLE" = true ] && [ -x "$HCID_EXEC" -a -f "$HCID_CONFIG" ] ; then
  $HCID_EXEC -s -f $HCID_CONFIG > /dev/null 2>&1&
  echo -n " hcid"
else 
  exit 1
fi

if [ "$HIDD_ENABLE" = true ] && [ -x "$HIDD_EXEC" ] ; then
  HIDD_OPTIONS=
  [ "$HIDD_SERVER" = true ] && HIDD_OPTIONS="$HIDD_OPTIONS --server"
  [ "$HIDD_SEARCH" = true ] && HIDD_OPTIONS="$HIDD_OPTIONS --search"
  [ "$HIDD_CONNECT" = true -a -n "$HIDD_CONNECT_BTMAC" ] && HIDD_OPTIONS="$HIDD_OPTIONS --connect $HIDD_CONNECT_BTMAC"
  [ "$HIDD_SWITCH2MASTER" = true ] && HIDD_OPTIONS="$HIDD_OPTIONS --master"
  $HIDD_EXEC $HIDD_OPTIONS > /dev/null 2>&1&
  echo -n " hidd"
fi

if [ "$RFCOMM_ENABLE" = true ] && [ -x "$RFCOMM_EXEC" -a -f "$RFCOMM_CONFIG" ] ; then
  [ -n "$RFCOMM_DEVICE_BTMAC" ] && sed -i "s/^.*bind.*;$/\tbind yes;/" $RFCOMM_CONFIG \
				&& sed -i "s/^.*device.*;$/\tdevice $RFCOMM_DEVICE_BTMAC;/" $RFCOMM_CONFIG
  [ -n "$RFCOMM_DEVICE_CHANNEL" ] && sed -i "s/^.*channel.*;$/\tchannel $RFCOMM_DEVICE_CHANNEL;/" $RFCOMM_CONFIG
  [ -n "$RFCOMM_DEVICE_COMMENT" ] && sed -i "s/^.*comment.*;$/\tcomment \"$RFCOMM_DEVICE_COMMENT\";/" $RFCOMM_CONFIG
  $RFCOMM_EXEC -f $RFCOMM_CONFIG bind all > /dev/null 2>&1&
  echo -n " rfcomm"
fi

if [ "$PAND_ENABLE" = true ] && [ -x "$PAND_EXEC" ] ; then
  PAND_OPTIONS=
  [ -n "$LOCAL_ROLE" ] && PAND_OPTIONS="$PAND_OPTIONS --role $LOCAL_ROLE"
  [ -n "$REMOTE_SERVICE" ] && PAND_OPTIONS="$PAND_OPTIONS --service $REMOTE_SERVICE"
  [ "$PAND_LISTEN" = true ] && PAND_OPTIONS="$PAND_OPTIONS --listen"
  [ "$PAND_SEARCH" = true ] && PAND_OPTIONS="$PAND_OPTIONS --search" && sleep 5 	# wait for hci device ready
  [ "$PAND_CONNECT" = true -a -n "$PAND_CONNECT_BTMAC" ] && PAND_OPTIONS="$PAND_OPTIONS --connect $PAND_CONNECT_BTMAC"
  [ "$LINK_ENCRYPTION" = true ] && PAND_OPTIONS="$PAND_OPTIONS --encrypt"
  [ "$SECURE_CONNECTION" = true ] && PAND_OPTIONS="$PAND_OPTIONS --secure"
  [ "$PAND_SWITCH2MASTER" = true ] && PAND_OPTIONS="$PAND_OPTIONS --master"
  PAND_OPTIONS="$PAND_OPTIONS --devup /usr/bin/bnep_up"
  $PAND_EXEC $PAND_OPTIONS > /dev/null 2>&1&
  echo -n " pand"
fi

echo "."

[ -n "$PASSKEY" ] && passkey-agent --default $PASSKEY > /dev/null 2>&1&

exit 0
