#!/bin/bash

# sixad-bin wrapper
# written by falkTX

. /etc/default/sixad


start_by_udev_q  () {
ADDR=`hcitool con | head -n 2 | tail -n 1 | awk '{printf$3}'`
hcitool name $ADDR | grep -e 'PLAYSTATION(R)3 Controller' -e 'Wireless Keypad'
}

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

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

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

modprobe_check () {
sudo /sbin/modprobe uinput
}

bt_start () {
if [ -f /lib/udev/rules.d/97-bluetooth.rules ]; then sudo /usr/sbin/bluetoothd --udev; else sudo /etc/init.d/bluetooth start; fi
}

case $1 in

  --boot-no)
if [ -f /etc/arch-release ]; then
{ #1 - ArchLinux Fix
sudo sed "s/ sixad//" -i /etc/rc.conf
} #1
else
{ #2
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
} #2
fi
  ;;

  --boot-yes)
if [ -f /etc/arch-release ]; then
{ #1 - ArchLinux Fix
sudo sed '/DAEMONS=/ s/)/ sixad)/g' -i /etc/rc.conf
} #1
else
{ #2
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
} #2
fi
  ;;

  --force|-force|force|-f)  #Used for QtSixA GUI
echo "sixad has been forced to start.
bluetooth will not be operational in this mode"
if (bluetoothd_check "$1"); then sudo pkill -KILL bluetoothd; fi
sudo /usr/sbin/hcid
if (sixad_already_running_check "$1"); then true
else
  using_old_bt_ver
  /sbin/modprobe uinput
  /usr/sbin/sixad-bin $Enable_leds $LED_js_n $LED_n $LED_plus $LED_anim $Legacy
fi
  ;;

  --stop|-stop|stop)
sudo pkill -KILL sixad-uinput-sixaxis
sudo pkill -TERM sixad-bin
sudo pkill -KILL hcid
if (bluetoothd_check "$1"); then true; else bt_start; fi
  ;;

  --udev)  #Used for udev
# Only start udev action if a Sixaxis or Keypad is detected
if (start_by_udev_q "$1"); then
{ #Full
if (bluetoothd_check "$1"); then pkill -KILL bluetoothd; fi
if (sixad_already_running_check "$1"); then true
else
  /sbin/modprobe uinput
  /usr/sbin/sixad-bin $Enable_leds $LED_js_n $LED_n $LED_plus $LED_anim $Legacy &
fi
/usr/sbin/hcid
if [ -f /etc/rc2.d/S90sixad ]; then true; else
  sleep 10 # wait for device to connect
  pkill -KILL hcid
fi
bt_start
} #Full

fi
  ;;

  --raw|-raw|raw)  #Also for hidraw devices!
echo "The hidraw driver of sixad can now be runned without sixad.
So please use: sixad-raw <hidraw device>"
  ;;

  --start|-start|start|-s)  #Running from terminal without arguments (default)
if (sixad_already_running_check "$1"); then
echo "sixad is already running.
run 'sixad --stop' to stop it"
else
{ #2
 if (modprobe_check "$1"); then  #Check for root access before running, If NO access, quit
  { #3
  if (bluetoothd_check "$1"); then sudo pkill -KILL bluetoothd; fi
  using_old_bt_ver
  sudo /usr/sbin/hcid
  sudo /usr/sbin/sixad-bin $Enable_leds $LED_js_n $LED_n $LED_plus $LED_anim $Legacy
   } #3
 else
  echo "You need admin/root access to run this application"
 fi
} #2
fi
  ;;

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

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

    *)
echo "usage: sixad <command>

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

    -s, --start		Start sixad
    -f, --force		Force connection (and starts sixad if not running)
	--stop		Stop sixad

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


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

esac
