#!/usr/bin/python

import sys
import dbus

bus = dbus.SystemBus()

manager = dbus.Interface(bus.get_object('org.ofono', '/'),
						'org.ofono.Manager')

modems = manager.GetModems()

for path, properties in modems:
	if "org.ofono.ConnectionManager" not in properties["Interfaces"]:
		continue

	connman = dbus.Interface(bus.get_object('org.ofono', path),
					'org.ofono.ConnectionManager')

	contexts = connman.GetContexts()
	path = "";

	for i, properties in contexts:
		if properties["Type"] == "internet":
			path = i
			break

	if path == "":
		path = connman.AddContext("internet")
		print("Created new context %s" % (path))
	else:
		print("Found context %s" % (path))

	context = dbus.Interface(bus.get_object('org.ofono', path),
					'org.ofono.ConnectionContext')

	if len(sys.argv) > 1:
		context.SetProperty("AccessPointName", sys.argv[1])
		print("Setting APN to %s" % (sys.argv[1]))

	if len(sys.argv) > 2:
		context.SetProperty("Username", sys.argv[2])
		print("Setting username to %s" % (sys.argv[2]))

	if len(sys.argv) > 3:
		context.SetProperty("Password", sys.argv[3])
		print("Setting password to %s" % (sys.argv[3]))
