#!/bin/bash
#Variables

gps_mac="00:0B:0D:6D:A1:E8";# holux
gpsd_bin="gpsd";
rfcomm_channel=1;

echo "*****************************************************************";
echo "** Wardriver ver. 1.0 | Written by amri from (www.mpetrov.net) **"
echo "*****************************************************************
";

do_start()
{
echo "Starting Bluetooth ..."
echo "
#Generated by Wardriver ver. 1.0 | Written by amri from (www.mpetrov.net) 
rfcomm0 {
        bind no;  # Automatically bind the device at startup
        device $gps_mac; # Bluetooth address of the device
        channel $rfcomm_channel; # RFCOMM channel for the connection
        comment \"My GPS device $gps_mac\"; # Description of the connection
}
" > /etc/bluetooth/rfcomm.conf;
/etc/init.d/bluetooth restart >/dev/null 2>/dev/null;
killall -9 $gpsd_bin >/dev/null 2>/dev/null;
hciconfig hci0 up >/dev/null 2>/dev/null

check_gps=0;
echo -n "Search device $gps_mac ...";
  while [ $check_gps -eq 0 ];
   do
   check_gps=`hcitool scan|grep "$gps_mac"|grep -v grep|wc -l`;
   echo -n ".";
   sleep 3;
  done;
 echo " ";echo "Found device $gps_mac";

 echo "Connecting to device $gps_mac..."
 hcitool cc "$gps_mac" > /dev/null 2> /dev/null;

echo "Binding to rfcomm0 ..."
sdptool add --channel=$rfcomm_channel OPUSH > /dev/null 2> /dev/null;
rfcomm bind /dev/rfcomm0 "$gps_mac" > /dev/null 2> /dev/null;

echo "Starting GPSD..."
$gpsd_bin /dev/rfcomm0

};#End of do_start();

do_stop()
 {
 echo "Stopping GPSD..."
 killall gpsd;
 echo "Releasing rfcomm0..."
 rfcomm release rfcomm0
 echo "Disconnecting from GPS Reciever..."
 hcitool dc $gps_mac
 echo "Stopping Bluetooth..."
 hciconfig hci0 down
 };#End of do_stop();

case "$1" in
  start)
        do_start
        ;;
  restart)
        do_stop;do_start;
        ;;
  stop)
        do_stop
        ;;
  *)
        echo "Usage: $0 start|stop|restart" >&2
        exit 3
        ;;
esac
