#!/usr/bin/perl -w

# ghettotooth.pl - by dual
#
# Ghettodriving for Bluetooth
#
# Usage: perl ghettotooth.pl <hciX>
#
###################################

chomp(my $date = `date +%F-%R`);
my $hcix = $ARGV[0];

# Create log file
open(LOG, ">ghettotooth-$date") or die "Cannot 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 "Cannot fork: $!" unless defined $pid;
	while(<STDIN>) {
		s/^\s+//;
		print $_ if ($_ !~ /Scanning/);
		print LOG $_ if ($_ !~ /Scanning/);
	}
}

# Provide assistance
unless ("$hcix" =~ /hci\d/) {
	print "ghettotooth.pl -\n";
	print "Ghettodriving for Bluetooth\n";
	print "Usage      : perl ghettotooth.pl <hciX>\n";
	print "hciX       : Bluetooth interface, ex. hci0\n";
	exit;
}

# 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;
}
