#!/bin/bash

# sixad-bin wrapper
# written by falkTX

DEBUG=0
LEGACY=0
BLUETOOTHD=/usr/sbin/bluetoothd

. /etc/default/sixad

if [ -f /usr/bin/sudo ]; then
  SUDO="/usr/bin/sudo"
else
  SUDO=""
fi

bt_device_check () {
if (which hciconfig > /dev/null); then
  while (! hciconfig dev &> /dev/null); do
    echo "No bluetooth adapters found on the system; will try again in 1 second."
    sleep 1;
  done

  VER=`hciconfig default version | grep "HCI Ver" | awk '{print$3}'`
  if [ "$VER" == "1.1" ]; then
    echo "***** NOTICE *****"
    echo "You're using a very old bluetooth dongle,"
    echo "the Sixaxis will not work properly!"
  elif [ "$VER" == "1.0" ]; then
    echo "***** WARNING *****"
    echo "You're using a _really_ old bluetooth dongle,"
    echo "the Sixaxis will just not work!"
  fi
fi
}

sixad_running_check () {
ps -e | grep sixad-bin > /dev/null
}

bluetoothd_running_check () {
ps -e | grep bluetoothd > /dev/null
}

modprobe_check () {
$SUDO /sbin/modprobe uinput
}

bt_start () {
$SUDO chmod 755 "$BLUETOOTHD"
if [ -f /lib/udev/rules.d/97-bluetooth-hid2hci.rules ]; then
  $SUDO /usr/sbin/bluetoothd --udev
elif [ -f /etc/rc.d/bluetooth ]; then
  $SUDO /etc/rc.d/bluetooth start
else
  $SUDO /etc/init.d/bluetooth start
fi
}

bt_stop() {
$SUDO chmod 644 "$BLUETOOTHD"
if (bluetoothd_running_check); then
  $SUDO pkill -KILL bluetoothd
fi
}

bt_start_and_stop () {
bt_start
env sleep 2
bt_stop
}

case $1 in

  --start|-start|start|-s)
REMOTE=0
bt_device_check
if (sixad_running_check); then
  echo "sixad is already running."
  echo "run '$0 --stop' to stop it"
else
 if (modprobe_check); then  #Check for root access before running, If NO access, quit
  bt_start_and_stop
  $SUDO /usr/sbin/sixad-bin $DEBUG $LEGACY $REMOTE
 else
  echo "You need admin/root access to run this application"
 fi
fi
  ;;

  --stop|-stop|stop)
$SUDO pkill -KILL sixad-sixaxis
$SUDO pkill -KILL sixad-remote
$SUDO pkill -TERM sixad-bin
bt_start
  ;;

  --remote|-remote|remote)
REMOTE=1
bt_device_check
if (modprobe_check); then  #Check for root access before running, If NO access, quit
  bt_start_and_stop
  $SUDO /usr/sbin/sixad-bin $DEBUG $LEGACY $REMOTE
else
  echo "You need admin/root access to run this application"
fi
  ;;

  --restore|-restore|restore|-r)
bt_start
  ;;

  --boot-yes)
# ArchLinux
if [ -f /etc/arch-release ]; then
  $SUDO sed '/DAEMONS=/ s/)/ sixad)/g' -i /etc/rc.conf
# Gentoo
elif [ -f /etc/gentoo-release ]; then
  $SUDO rc-update add sixad
# Debian (default)
else
  if [ -f /etc/rc2.d/S90sixad ]; then true; else $SUDO ln -s /etc/init.d/sixad /etc/rc2.d/S90sixad; fi
  if [ -f /etc/rc3.d/S90sixad ]; then true; else $SUDO ln -s /etc/init.d/sixad /etc/rc3.d/S90sixad; fi
  if [ -f /etc/rc4.d/S90sixad ]; then true; else $SUDO ln -s /etc/init.d/sixad /etc/rc4.d/S90sixad; fi
  if [ -f /etc/rc5.d/S90sixad ]; then true; else $SUDO ln -s /etc/init.d/sixad /etc/rc5.d/S90sixad; fi
fi
  ;;

  --boot-no)
# ArchLinux
if [ -f /etc/arch-release ]; then
  $SUDO sed "s/ sixad//" -i /etc/rc.conf
# Gentoo
elif [ -f /etc/gentoo-release ]; then
  $SUDO rc-update delete sixad
# Debian (default)
else
  if [ -f /etc/rc2.d/S90sixad ]; then $SUDO rm /etc/rc2.d/S90sixad; fi
  if [ -f /etc/rc3.d/S90sixad ]; then $SUDO rm /etc/rc3.d/S90sixad; fi
  if [ -f /etc/rc4.d/S90sixad ]; then $SUDO rm /etc/rc4.d/S90sixad; fi
  if [ -f /etc/rc5.d/S90sixad ]; then $SUDO rm /etc/rc5.d/S90sixad; fi
fi
  ;;

  --help|-help|help|-h)
echo "[Qt]SixA Daemon"
$0
  ;;

  --version|-version|version|-v)
echo "[Qt]SixA Daemon - version 1.5.1"
  ;;

  *)
echo "usage: $0 <command>

command can be:
    -h, --help          Show help (this message)
    -v, --version       Show sixad version

    -s, --start         Start sixad
        --stop          Stop sixad
        --remote        BD Remote mode

    -r, --restore       Restore regular bluetooth

        --boot-yes      Auto-starts sixad at boot time
        --boot-no       Does not auto-start sixad at boot time

You can also check: sixad-raw, sixad-notify"
  ;;

esac
