#!/usr/bin/perl -w

# John Cooper 06/04/03
# Released under the GNU GPL license
# tested under 2.4.20-mh6 + bluez 
# linux redhat 7.3
# bluenfs automates bluetooth nfs mount 

# restart bluetooth support ala redhat
`/etc/init.d/bluetooth restart`;

# set my bluetooth phone address
# change it for your one!
# I'd rather use hcitool inq - but it wont work for me.
$bdaddr="00:0A:D9:14:2B:35";

# discover if its up and what the channel number is
@disc=`sdptool browse $bdaddr`;
if (@disc < 2){print "$disc[0]\nSwitch Phone Bluetooth on ??\n";exit (-1);}
$ch=0;$ro=0;
foreach $line(@disc){
  if($line=~ /Channel/){$chan[$ch]=$line;$ch++;}
  if($line=~ /Service Name/){$name[$ro]=$line;$ro++;}
}
$co=0;
foreach $line(@name){
  if($line=~ /Dial-up Networking/){$channel=$chan[$co];}
  $co++;
}
$channel=~ s/^\s+Channel: (\d+)/$1/;
print "Channel is $channel\n";

# associate the rfcomm channel for nfs
# fixed at 4 , dont know where that can be found??
print "Info: rfcomm connect 0 $bdaddr 4\n";
if($pid=fork){
  print "launched rfcomm as pid number $pid\n";
  }
elsif(defined $pid){
  exec "rfcomm connect 0 $bdaddr 4";
  }
else {
  die "Failed to fork rfcomm: $!\n";
  }
sleep 1;

# kick off the pppd  
print "Info: nfs startup \n";
if($pid=fork){
  print "launched nfs pid number $pid\n";
  }
elsif(defined $pid){
  exec "p3nfsd -p800 -tty /dev/rfcomm0";
  }
else {
  die "Failed to fork nfs: $!\n";
  }

sleep 1;
print "Mounted at /mnt/psion\n";
