#!/usr/bin/perl -w

# ghettotooth.pl - by dual
# ghettodriving for bluetooth
# usage: perl ghettotooth.pl <hciX>

use strict;

# Get 'n check
help() unless defined(my $hcix = shift);
help() unless $hcix =~ /hci\d/;

# Provide assistance
sub help {
	print "ghettotooth.pl -\n";
	print "Ghettodriving for Bluetooth\n";
	print "Usage : perl ghettotooth.pl <hciX>\n";
	print "<hciX>: Bluetooth interface, ex. hci0\n";
	exit;
}

# Create log file
chomp(my $date = `date +%F-%R`);
open(LOG, ">ghettotooth-$date") or die "Can't create log: $!";

# Handle interrupt
sub INT_handle {
	close(STDOUT);
	close(LOG);
	exit;
}
$SIG{'INT'} = 'INT_handle';

# Filter output
sub filter {
	my $pid;
	return if $pid = open(STDOUT, "|-");
	die "Can't fork: $!" unless defined $pid;
	while(<STDIN>) {
		s/^\s+//;
		print if ($_ !~ /Scanning/);
		print LOG if ($_ !~ /Scanning/);
	}
}

# Print banner
print "\n>>> ghettotooth - ghettodriving for bluetooth <<<\n\n";
print "Ctrl+C to quit...\n\n";

# Scan
filter();
while (1) {
	my $scan = `hcitool -i $hcix scan`;
	print $scan;
}
