#!/usr/bin/python

import sys
import dbus
import re
from optparse import OptionParser, make_option

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"),
		]
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()

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

if (len(args) < 1):
	print "Usage: %s <command>" % (sys.argv[0])
	print ""
	print "  list"
	print "  create <address>"
	print "  remove <address|path>"
	print "  discover <address> [pattern]"
	print "  class <address>"
	print "  name <address>"
	print "  alias <address> [alias]"
	print "  trusted <address> [yes/no]"
	print "  blocked <address> [yes/no]"
	sys.exit(1)

if (args[0] == "list"):
	for path in adapter.ListDevices():
		device = dbus.Interface(bus.get_object("org.bluez", path),
							"org.bluez.Device")
		properties = device.GetProperties()
		print "%s %s" % (properties["Address"], properties["Alias"])

	sys.exit(0)

if (args[0] == "create"):
	if (len(args) < 2):
		print "Need address parameter"
	else:
		device = adapter.CreateDevice(args[1])
		print device
	sys.exit(0)

if (args[0] == "remove"):
	if (len(args) < 2):
		print "Need address or object path parameter"
	else:
		try:
			path = adapter.FindDevice(args[1])
		except:
			path = args[1]
		adapter.RemoveDevice(path)
	sys.exit(0)

if (args[0] == "discover"):
	if (len(args) < 2):
		print "Need address parameter"
	else:
		path = adapter.FindDevice(args[1])
		device = dbus.Interface(bus.get_object("org.bluez", path),
							"org.bluez.Device")
		if (len(args) < 3):
			pattern = ""
		else:
			pattern = args[2]
		services = device.DiscoverServices(pattern);
		for key in services.keys():
			p = re.compile(">.*?<")
			xml = p.sub("><", services[key].replace("\n", ""))
			print "[ 0x%5x ]" % (key)
			print xml
			print
	sys.exit(0)

if (args[0] == "class"):
	if (len(args) < 2):
		print "Need address parameter"
	else:
		path = adapter.FindDevice(args[1])
		device = dbus.Interface(bus.get_object("org.bluez", path),
							"org.bluez.Device")
		properties = device.GetProperties()
		print "0x%06x" % (properties["Class"])
	sys.exit(0)

if (args[0] == "name"):
	if (len(args) < 2):
		print "Need address parameter"
	else:
		path = adapter.FindDevice(args[1])
		device = dbus.Interface(bus.get_object("org.bluez", path),
							"org.bluez.Device")
		properties = device.GetProperties()
		print properties["Name"]
	sys.exit(0)

if (args[0] == "alias"):
	if (len(args) < 2):
		print "Need address parameter"
	else:
		path = adapter.FindDevice(args[1])
		device = dbus.Interface(bus.get_object("org.bluez", path),
							"org.bluez.Device")
		if (len(args) < 3):
			properties = device.GetProperties()
			print properties["Alias"]
		else:
			device.SetProperty("Alias", args[2])
	sys.exit(0)

if (args[0] == "trusted"):
	if (len(args) < 2):
		print "Need address parameter"
	else:
		path = adapter.FindDevice(args[1])
		device = dbus.Interface(bus.get_object("org.bluez", path),
							"org.bluez.Device")
		if (len(args) < 3):
			properties = device.GetProperties()
			print properties["Trusted"]
		else:
			if (args[2] == "yes"):
				value = dbus.Boolean(1)
			elif (args[2] == "no"):
				value = dbus.Boolean(0)
			else:
				value = dbus.Boolean(args[2])
			device.SetProperty("Trusted", value)
	sys.exit(0)

if (args[0] == "blocked"):
	if (len(args) < 2):
		print "Need address parameter"
	else:
		path = adapter.FindDevice(args[1])
		device = dbus.Interface(bus.get_object("org.bluez", path),
							"org.bluez.Device")
		if (len(args) < 3):
			properties = device.GetProperties()
			print properties["Blocked"]
		else:
			if (args[2] == "yes"):
				value = dbus.Boolean(1)
			elif (args[2] == "no"):
				value = dbus.Boolean(0)
			else:
				value = dbus.Boolean(args[2])
			device.SetProperty("Blocked", value)
	sys.exit(0)

print "Unknown command"
sys.exit(1)