#!/bin/bash
# PROXIMITY DETECTION MUST BE RUN ON SAME BOX
# AS YOUR PRIMARY ASTERISK SERVER WITH THIS SCRIPT
# Null result means Bluetooth MAC Address below not found
# CopyReqd means a change has occurred and needs to be posted
# On a clunker PC it takes about 20 seconds for a test to fail
# and it takes about 5 seconds for a test to succeed, i.e. BT MAC found
# Assign a name (deviceuser) to the device we will be monitoring
# Enter the MAC addr (devicemac) of the device to be monitored
# Enter the extension on your Asterisk server to be transferred
# Enter the cellphone number for the transfer heeding your dialplan syntax
# Don't change anything else
deviceuser=WARD
devicemac=00:4B:63:D5:62:AB
myextension=200
mycellphone=6783456789
# ---------- DON'T CHANGE ANYTHING BELOW THIS LINE ------------------
deviceuser2=$deviceuser.now
hcitool name $devicemac > /tmp/$deviceuser2
if [ -s /tmp/$deviceuser2 ] ; then
 if [ -s /tmp/$deviceuser.last ] ; then
   echo $deviceuser2 IN - No Update Required ;
   date
 else
   echo $deviceuser2 IN - Update Required  ;
   date
   cp /root/notnull.file /tmp/$deviceuser.last
   cp /root/notnull.file /tmp/$deviceuser
   asterisk -rx "DATABASE DEL CF $myextension"
 fi
else 
 if [ -s /tmp/$deviceuser.last ] ; then
   echo $deviceuser2 OUT - Update Required ;
   date
   cp /root/null.file /tmp/$deviceuser.last
   cp /root/null.file /tmp/$deviceuser
   asterisk -rx "DATABASE PUT CF $myextension $mycellphone"
 else
   echo $deviceuser2 OUT - No Update Required ;
   date
 fi
fi
