#!/usr/bin/python

import sys
import dbus

if (len(sys.argv) < 2):
	print "Usage: %s <network>" % (sys.argv[0])
	sys.exit(1)

bus = dbus.SystemBus()

manager = dbus.Interface(bus.get_object("org.moblin.connman", "/"),
					"org.moblin.connman.Manager")

properties = manager.GetProperties()

for path in properties["Devices"]:
	device = dbus.Interface(bus.get_object("org.moblin.connman", path),
						"org.moblin.connman.Device")

	properties = device.GetProperties()

	if (properties["Type"] != "wifi" and properties["Type"] != "wimax"):
		continue

	print "[ %s ]" % (path)

	for path in properties["Networks"]:
		network = dbus.Interface(bus.get_object("org.moblin.connman", path),
						"org.moblin.connman.Network")

		properties = network.GetProperties()

		if "Name" not in properties.keys():
			continue

		if (properties["Connected"] == dbus.Boolean(1)):
			continue

		if (properties["Name"] == sys.argv[1]):
			print "Connecting %s" % (path)
			network.Connect()
