#!/bin/bash
#
# Bluetooth Adapter toggle
# Andrew Wyatt
# Tool to toggle Bluetooth device on / off

JUPITER_PATH="/usr/lib/jupiter/scripts"
JUPITER_VAR="/var/jupiter"

BT_RFKILL=$(for i in /sys/class/rfkill/*; do (echo -n $i/state:;cat $i/type 2>/dev/null) | grep bluetooth | sed "s#:.*##"; done | head -n 1)

if [[ "$*" =~ "silent" ]]; then
  NO_NOTIFY=1
fi

. /usr/lib/jupiter/scripts/notify

NICON="/usr/share/pixmaps/jupiter/bluetooth.png"

if [ ! "$BLUETOOTH_DEVICE" ] && [ -e "$BT_RFKILL" ]; then
  BLUETOOTH_DEVICE=$BT_RFKILL
elif [ -e "/sys/devices/platform/eeepc/bt" ]; then
  BLUETOOTH_DEVICE="/sys/devices/platform/eeepc/bt"
elif [ ! "$BLUETOOTH_DEVICE" ] && [ -e "/proc/acpi/asus/bt" ]; then
  BLUETOOTH_DEVICE="/proc/acpi/asus/bt"
fi

if [ ! "$BLUETOOTH_DEVICE" ]; then
  BLUETOOTH_RADIO="-1"
else
  BLUETOOTH_RADIO=$(cat $BLUETOOTH_DEVICE)
fi

if [ ! -d "$JUPITER_VAR" ]; then
  mkdir $JUPITER_VAR 2>/dev/null
fi

if [ -e "$JUPITER_VAR/bt_saved" ]; then
  BLUETOOTH_SAVED=$(cat $JUPITER_VAR/bt_saved)
else
  BLUETOOTH_SAVED=1
fi

function bt_status {
  if [ -e "$BT_RFKILL" ]; then
    if [ "$BLUETOOTH_RADIO" = "-1" ]; then
      echo "UNKNOWN"
    elif [ "$BLUETOOTH_RADIO" = "0" ]; then
      echo "OFF"
    else
      echo "ON"
    fi
  else
    echo "UNKNOWN"
  fi
}

function check_bt_installed {
  if [ "$BLUETOOTH_RADIO" = "-1" ]; then
    notify $"Bluetooth module not installed" $NICON
    exit 1
  fi
}

function bt_disable {
  check_bt_installed
  notify $"Bluetooth Disabled" $NICON
  echo 0 > $JUPITER_VAR/bt_saved
  echo 0 > $BLUETOOTH_DEVICE
  /sbin/modprobe -r bluetooth 2>/dev/null || true
  /sbin/modprobe -r btusb 2>/dev/null || true
}

function bt_poweroff {
  echo 0 > $BLUETOOTH_DEVICE
}

function bt_enable {
  check_bt_installed
  notify $"Bluetooth Enabled" $NICON
  echo 1 > $JUPITER_VAR/bt_saved
  echo 1 > $BLUETOOTH_DEVICE
  /sbin/modprobe btusb 2>/dev/null || true
  /sbin/modprobe bluetooth 2>/dev/null || true

  BT_RFKILLB=$(for i in /sys/class/rfkill/*; do (echo -n $i/state:;cat $i/type 2>/dev/null) | grep bluetooth | sed "s#:.*##"; done | tail -n 1)
  echo 1 > $BT_RFKILLB || true

}

function bt_toggle {
    check_bt_installed
    if [ "$BLUETOOTH_RADIO" = "1" ]; then
      bt_disable
    elif [ "$BLUETOOTH_RADIO" = "0" ]; then
      bt_enable
    fi
}

function bt_debug {
  echo "-- Device: [$BLUETOOTH_DEVICE]"
  echo "-- Radio: [$BLUETOOTH_RADIO]"
  echo "-- Turned off"
  echo 0 > $BLUETOOTH_DEVICE
  modprobe -r bluetooth || true
  cat $BLUETOOTH_DEVICE
  lsusb -v
  lspci -v
  echo "-- Turned on"
  echo 1 > $BLUETOOTH_DEVICE
  cat $BLUETOOTH_DEVICE
  modprobe btusb || true
  modprobe bluetooth || true
  lsusb -v
  lspci -v
  echo "-- Eee PC Info"
  dmidecode
}

function bt_restore {
  if [ "$BLUETOOTH_SAVED" = "1" ]; then
    if [ "$BLUETOOTH_RADIO" = "0" ]; then
      /sbin/modprobe bluetooth
      echo 1 > $BLUETOOTH_DEVICE
    fi
  elif [ "$BLUETOOTH_SAVED" = "0" ]; then
    if [ "$BLUETOOTH_RADIO" = "1" ]; then
      echo 0 > $BLUETOOTH_DEVICE
      /sbin/modprobe -r bluetooth 2>/dev/null
    fi
  fi
}

case $1 in
  restore)
    bt_restore
  ;;
  status)
    bt_status
  ;;
  on|enable)
    bt_enable
  ;;
  off|disable)
    bt_disable
  ;;
  poweroff)
    bt_poweroff
  ;;
  debug)
    bt_debug
  ;;
  *)
    bt_toggle
  ;;
esac
