#!/bin/bash

SYS_CONF="/etc/bluetooth.conf"
BOOT_CONF="/boot/bluetooth.conf"
CONF="/data/etc/bluetooth.conf"
PLATFORM_BTUART="/usr/libexec/btuart"
LOG="/var/log/btuart.log"

test -f ${CONF} || test -f ${BOOT_CONF} || test -f ${SYS_CONF} || exit 0

# Bluetooth explicitly disabled by empty bluetooth.conf
test -f ${CONF} && ! test -s ${CONF} && exit 0

test -s ${PLATFORM_BTUART} || exit 0

test -n "${OS_VERSION}" || source /etc/init.d/base

source ${PLATFORM_BTUART}

case "$1" in
    start)
        msg_begin "Attaching UART bluetooth device"
        btuart_start &> ${LOG} &
        msg_background
        ;;

    stop)
        msg_begin "Detaching UART bluetooth device"
        btuart_stop &>/dev/null
        test $? == 0 && msg_done || msg_fail
        ;;

    *)
        echo $"Usage: $0 {start|stop}"
        exit 1
esac

exit $?
