#!/bin/bash
#set -o verbose sh -v
# Copied from Steven on http://gentoo-wiki.com/Talk:TIP_Bluetooth_Proximity_Monitor
# Modified By Jamie Paton <jamie.paton@googlemail.com>
# Modified By Michele Marcucci http://www.michelem.org

# These are the sections you'll need to edit

# You'll need to use the MAC address of your phone here
# Use "hcitool scan" to find the MAC of your device
DEVICE="00:16:4E:D0:B0:5E"

# How often to check the distance between phone and computer in seconds
CHECK_INTERVAL=10

# The RSSI threshold at which a phone is considered far or near
THRESHOLD=-2

# The command to run when your phone gets too far away
FAR_CMD='/usr/bin/gnome-screensaver-command --activate -l'

# The command to run when your phone is close again
#NEAR_CMD='/opt/gnome/bin/gnome-screensaver-command --poke'
NEAR_CMD='/usr/bin/gnome-screensaver-command --deactivate'

HCITOOL="sudo /usr/bin/hcitool"
STARTX_PID=0
DEBUG="/tmp/btproximity.log"

connected=0
state="far"
active=0

function msg {
    echo "$1" >> "$DEBUG"
# Uncomment line below if you want debug on console too instead only to file $DEBUG
#    echo "$1"
}

function check_connection {
    connected=0;
    found=0
    for s in `$HCITOOL con`; do
        if [[ "$s" == "$DEVICE" ]]; then
            found=1;
        fi
    done
    if [[ $found == 1 ]]; then
        connected=1;
    else
       msg 'Attempting connection...'
        if [ -z "`$HCITOOL cc $DEVICE 2>&1`" ]; then
            msg 'Connected'
            connected=1;
        else
                if [ -z "`l2ping -c 1 -t 2 -i $DEVICE 2>&1`" ]; then
                        if [ -z "`$HCITOOL cc $DEVICE 2>&1`" ]; then
                            msg 'Ping is good!'
                            connected=1;
                        else
                        msg "ERROR: Could not connect to device $DEVICE."
                        connected=0;
                        fi
                fi
        fi
    fi
if [[ $connected -eq 0 ]]; then
	$FAR_CMD > /dev/null 2>&1
	state="far"
	active=1
     fi

}

check_connection

while [[ $connected -eq 0 ]]; do
    check_connection
    sleep $CHECK_INTERVAL
done

name=`$HCITOOL name $DEVICE`
msg "Monitoring proximity of \"$name\" [$DEVICE]";

while /bin/true; do
	state="far"
    check_connection

    if [[ $connected -eq 1 ]]; then
        rssi=$($HCITOOL rssi $DEVICE | sed -e 's/RSSI return value: //g')

        if [[ $rssi -le $THRESHOLD ]]; then
            if [[ "$state" == "near" ]]; then
                msg "*** Device \"$name\" [$DEVICE] has left proximity"
                state="far"
		active=1;
                $FAR_CMD > /dev/null 2>&1
            fi
        else
            if [[ "$state" == "far" && $rssi -ge $[$THRESHOLD+2] ]]; then
                msg "*** Device \"$name\" [$DEVICE] is within proximity"
                state="near"
                $NEAR_CMD > /dev/null 2>&1
                STARTX_PID=$(pgrep startx)
		if [[ $active -eq 1 ]]; then
			zenity --info --text='Welcome back Master!'
			active=0;
		fi
            fi
        fi
        msg "state = $state, RSSI = $rssi"
    fi
    sleep $CHECK_INTERVAL
done 
