#!/usr/bin/python

from __future__ import absolute_import, print_function, unicode_literals

from gi.repository import GObject

import dbus
import dbus.mainloop.glib
from optparse import OptionParser, make_option

compact = False
devices = {}

def print_compact(path, address, properties):
	name = ""
	address = "<unknown>"

	for key, value in properties.iteritems():
		if type(value) is dbus.String:
			value = unicode(value).encode('ascii', 'replace')
		if (key == "Name"):
			name = value
		elif (key == "Address"):
			address = value

	if path in devices:
		flag = "*"
	else:
		flag = " "

	print("%s%s %s" % (flag, address, name))

def print_normal(path, address, properties):
	print("[ " + address + " ]")

	for key in properties.keys():
		value = properties[key]
		if type(value) is dbus.String:
			value = unicode(value).encode('ascii', 'replace')
		if (key == "Class"):
			print("    %s = 0x%06x" % (key, value))
		else:
			print("    %s = %s" % (key, value))

	print()

def skip_dev(path, new_dev):
	if not compact:
		return False;

	if not path in devices:
		return False

	old_dev = devices[path]

	if "Name" in old_dev:
		return True

	if not "Name" in new_dev:
		return True

	return False

def print_dev(path, props):
	if "Address" in props:
		address = props["Address"]
	else:
		address = "<unknown>"

	if compact:
		print_compact(path, address, props)
	else:
		print_normal(path, address, props)

def devices_found(found_devs):
	for path, properties in found_devs.iteritems():
		if (skip_dev(path, properties)):
			continue

		print_dev(path, properties)
		devices[path] = properties

def property_changed(name, value):
	if (name == "Discovering" and not value):
		mainloop.quit()

if __name__ == '__main__':
	dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

	bus = dbus.SystemBus()
	manager = dbus.Interface(bus.get_object("org.bluez", "/"),
							"org.bluez.Manager")

	option_list = [
			make_option("-i", "--device", action="store",
					type="string", dest="dev_id"),
			make_option("-c", "--compact",
					action="store_true", dest="compact"),
			]
	parser = OptionParser(option_list=option_list)

	(options, args) = parser.parse_args()

	if options.dev_id:
		adapter_path = manager.FindAdapter(options.dev_id)
	else:
		adapter_path = manager.DefaultAdapter()

	if options.compact:
		compact = True;

	adapter = dbus.Interface(bus.get_object("org.bluez", adapter_path),
							"org.bluez.Adapter")

	bus.add_signal_receiver(devices_found,
					dbus_interface = "org.bluez.Adapter",
					signal_name = "DevicesFound")

	bus.add_signal_receiver(property_changed,
					dbus_interface = "org.bluez.Adapter",
					signal_name = "PropertyChanged")

	adapter.StartDiscovery()

	mainloop = GObject.MainLoop()
	mainloop.run()
