#!/bin/bash
# 
# buu - Bluetooth Uplink Utility
# Øyvind Kolås pippin@gimp.org (c) 2007, released to public domain.
#
# This is the bluetooth uplink utility used by me personally, it works for
# me with the other changes mentioned before the actual shell script code
# applied to an ubuntu system.
#
# Usage:
#  $ buu [ppp peer to use]
#
# buu tries connecting until it finds the phone and manages to connect to
# the provider.
#
# To force a reconnect you can disable bluetooth, power cycle the phone or
# use another terminal and enter
#  $ killall pppd
#

#----------------------------------------------------------------------------
#/etc/bluetooth/rfcomm.conf
#----------------------------------------------------------------------------
                                                                        FOO='
rfcomm0 {
  bind yes;
  
  # The bluetoothaddress of the device to use
  # can be found with:
  #   $ hcitool scan
  device 00:11:22:33:44:55:66; 

  # The channel to be used, can be found by the following query:
  #   $ sdptool search DUN
  channel 3;      

  comment "Nokia N70";
}
                                                                            '
#----------------------------------------------------------------------------
#/etc/ppp/peers/bluetooth
#----------------------------------------------------------------------------
                                                                        FOO="
rfcomm0
connect '/usr/sbin/chat -v -f /etc/ppp/chat-gprs'
debug
noauth
silent
lock
noipdefault
defaultroute
replacedefaultroute
ipcp-accept-local
usepeerdns
deflate 10,10
bsdcomp 10,10
115200
                                                                            "


# The chat script to use might differ from provider to provider,
# check with your provider if the chat script should be different.

#----------------------------------------------------------------------------
#/etc/ppp/chat-gprs
#----------------------------------------------------------------------------
                                                                        FOO="
'TIMEOUT' '15'
'ABORT' 'BUSY'
'ABORT' 'ERROR'
'ABORT' 'NO ANSWER'
'ABORT' 'NO CARRIER'
'ABORT' 'NO DIALTONE'
'ABORT' 'Invalid Login'
'ABORT' 'Login incorrect'
''   'ATZ'
'OK' 'ATDT*99#'
'~--'
                                                                            "


#----------------------------------------------------------------------------
DEFAULT_PEER=bluetooth

printstatus () {
        echo "time:      "`date`
        echo "connected: $CONNECTED_TIME"
        echo "peer:      $1"
        echo ""
        ifconfig ppp
        route -n
}

cleanup () {
    echo -n " - Cleanup: "
    killall pppd rfcomm ssh > /dev/null 2>&1
    echo -n "."
    rfcomm release 0 > /dev/null 2>&1  # release any potentially existing
                                       # use of rfcomm0
    echo -n "."
    sleep 1
    echo ""
}


bluetooth_is_connected ()
{
    if rfcomm | grep rfcomm0 | grep connected > /dev/null 2>&1; then
        return 0
    else
        return -1
    fi
}

pppd_is_connected ()
{
    if ifconfig | grep ppp > /dev/null 2>&1; then
        return 0
    else
        return -1
    fi
}

connect_bluetooth () {
     echo -n " - Connecting to phone: "
     sh -c "rfcomm connect 0" > /dev/null 2>&1 & 
     for i in {1..10};do  # wait up to 10s for phone connection
       if bluetooth_is_connected; then
           echo -n "" # we're fastforwarding without sleep
       else
           echo -n "."
           sleep 1    # spending time when we're not connected
       fi
     done
     echo ""
}

connect_pppd () {
  if pppd call $1 > /dev/null 2>&1; then
     echo -n " - Connecting via ppp to peer $1: "
     for i in {1..15};do  # wait up to 15s waiting for ppp connection
       if pppd_is_connected; then
           CONNECTED_TIME=`date` # not spending time
       else
           echo -n "."
           sleep 1 # spending time when we're not connected
       fi
     done
  else
      echo " - failed to initiate ppp connection to $1"
  fi
  echo ""
}


buu () {
while true; do   # repeat until ^C
    clear
    echo "BUU - Bluetooth Uplink Utility"
    echo ""
    if pppd_is_connected; then
        printstatus $1
        sleep 5
    else                                      # attempt to connect
        echo ""
        cleanup
        connect_bluetooth
        if bluetooth_is_connected; then
            connect_pppd $1
        fi
        if pppd_is_connected; then
           echo "connected"
        else
           echo -n " - NOT connected, waiting 3s"
           sleep 3
           echo ""
        fi
    fi
done
}

if [ $1x != ""x ];then
    buu $1
else
    buu $DEFAULT_PEER
fi
