#!/bin/sh

module_id() {
    awk 'BEGIN { FS=": " } /Hardware/ { print $2 } ' </proc/cpuinfo
}

if [ -f /etc/sysconfig/bluetooth ]; then

  . /etc/sysconfig/bluetooth

  if [ $BLUETOOTH = "yes" ]; then
    # Turn echo off; if the bluetooth chip emits characters on power up,
    # they would be echoed back to the chip, confusing it. This should only
    # be necessary to do once, as hciattach turns echo off too (but too
    # late the first time, since the bluetooth chip is powered up when the
    # port is opened, before hciattach gets a chance to turn echo off).
    echo "Turning echo off on ${BLUETOOTH_PORT}"
    stty -echo < $BLUETOOTH_PORT
  fi

else
  echo -n "Checking for built-in Bluetooth: "
  case `module_id` in
    "HP iPAQ H2200")
	BLUETOOTH=yes
	PORT=/dev/tts/3
	SPEED=921600
        PROTO=any
	PROBE=no
	;;
    "HP iPAQ H5400")
	BLUETOOTH=yes
	PORT=/dev/tts/1
	SPEED=921600
        PROTO=any
	PROBE=yes
	;;
    "HP iPAQ H3900")
	BLUETOOTH=yes
	PORT=/dev/tts/1
	SPEED=921600
        PROTO=bcsp
	PROBE=yes
	;;
    "HP iPAQ H3800")
	BLUETOOTH=yes
	PORT=/dev/ttySB0
	SPEED=230400
        PROTO=bcsp
	PROBE=yes
	;;
    "HP iPAQ HX4700")
	BLUETOOTH=yes
	PORT=/dev/ttyS1
	SPEED=921600
	PROTO=texas
	SCRIPT=/etc/bluetooth/TIInit_3.2.26.bts
	PROBE=no
	;;
    "HP iPAQ H6300")
	BLUETOOTH=yes
	PORT=/dev/ttyS0
	SPEED=115200
	PROTO=texas
	PROBE=no
	;;
    *)
	BLUETOOTH=no
        ;;
  esac

  if [ $BLUETOOTH = "yes" ]; then
    stty -echo < $PORT
    if [ $PROBE = "yes" ]; then
      if ! blueprobe $PORT $SPEED; then
        BLUETOOTH=no
      fi
    fi
  fi

  echo $BLUETOOTH
  echo "BLUETOOTH=$BLUETOOTH" >/etc/sysconfig/bluetooth
  if [ $BLUETOOTH = "yes" ]; then
    echo "BLUETOOTH_PORT=$PORT" >>/etc/sysconfig/bluetooth
    echo "BLUETOOTH_SPEED=$SPEED" >>/etc/sysconfig/bluetooth
    echo "BLUETOOTH_PROTOCOL=$PROTO" >>/etc/sysconfig/bluetooth
    if [ "$SCRIPT" != "" ] && [ -f "$SCRIPT" ]; then
      echo "BLUETOOTH_SCRIPT=$SCRIPT" >>/etc/sysconfig/bluetooth
    fi
  fi
fi
