#!/bin/bash
# Rupert's Bluetooth monitoring progam to turn motion on and off
# v0.2 March 2009
# written with next to no knowledge of bash and loads of googling
# v0.2
# updated to control motion instead of zoneminder and added a second check before turning it on
MOBILE=""
SCAN=""

# lets scan for the presence of my mobile by scanning for its bluetooth MAC address
# and seeing if it's name is returned and assign it to a variable. To personalise, you must add your own phone's MAC address here.
# If you don't know your MAC address, then do the following:
# Turn bluetooth OFF on your phone, then in console, type hcitool scan - look at all the MAC addresses and names that are returned
# Now turn bluetooth on on your phone and ensure it is discoverable, now type hcitool scan again,
# A new MAC address and Name of phone should show up. Make notes of this. Type in your MAC address below
SCAN=( $(hcitool name MACADDRESSHERE) )

# lets assign the name of our mobile phone to a variable, this needs to be the first name returned when you scan for your phone with hcitool given above
MOBILE="MobileNameHere"

# now, lets do the main part of the script, if the name of my phone is returned then the phone is here, so turn off motion, it the name isn't returned
# then turn motion on!
# You can add additional commands here if you want, make sure you dont mess things up though.

if [ "$MOBILE" = "$SCAN" ]; then
	sudo /etc/init.d/motion stop
	echo Mobile present at `date "+%m/%d/%y %l:%M:%S %p"` >> /var/log/bluetoothscan.log
else 
	SCAN=( $(hcitool name 00:1D:FD:72:61:F8) )
	if [ "$MOBILE" = "$SCAN" ]; then
		sudo /etc/init.d/motion stop
		echo Mobile present on second try at `date "+%m/%d/%y %l:%M:%S %p"` >> /var/log/bluetoothscan.log
	else 
		sudo /etc/init.d/motion start
		echo Mobile absent and motion started at `date "+%m/%d/%y %l:%M:%S %p"` >> /var/log/bluetoothscan.log
	fi	
fi

