#!/bin/sh


echo "Bluetooth Router App - controller state"
echo "======================================="
echo
STATUS=`bluetoothctl show | grep -v -e '\(^\[.*\)\|\(^  [0-9a-f].*\)'`
RETVAL=$?
if [ $RETVAL = 0 ]; then
  echo "$STATUS"
else
  echo "Controller is OFF - no info available"
fi

echo

echo "Bluetooth Router App - paired devices"
echo "====================================="
echo
CONTROLLERS=`ls /var/data/bluetooth | grep -e '^[0-9A-F]\{2\}:[0-9A-F]\{2\}:[0-9A-F]\{2\}:[0-9A-F]\{2\}:[0-9A-F]\{2\}:[0-9A-F]\{2\}'`
if [ -z "$CONTROLLERS" ]; then
  echo "No paired device"
else 
  for CONTROLLER in $CONTROLLERS; do
    echo "Controller $CONTROLLER"
    echo "----------------------------"
    DEVICES=`ls "/var/data/bluetooth/$CONTROLLER" | grep -e '^[0-9A-F]\{2\}:[0-9A-F]\{2\}:[0-9A-F]\{2\}:[0-9A-F]\{2\}:[0-9A-F]\{2\}:[0-9A-F]\{2\}'`
    if [ -z "$DEVICES" ]; then
      echo "No paired device"
    else
      for DEVICE in $DEVICES; do
        echo "Device $DEVICE"
        cat /var/data/bluetooth/$CONTROLLER/$DEVICE/info | sed -e '/^$/d' -e 's/^Key=.*/Key=********************************/' -e 's/.*/  \0/'
      done
    fi
  done
fi