#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4

from config import Config
from router import Router
import os

def start (args):
    
    # if a specific conf has been provided (which it
    # will be), if we're inside the django reloaded
    if "RAPIDSMS_INI" in os.environ:
        ini = os.environ["RAPIDSMS_INI"]
    
    # use a local ini (for development)
    # if one exists, to avoid everyone
    # having their own rapidsms.ini
    elif os.path.isfile("local.ini"):
        ini = "local.ini"
    
    # otherwise, fall back
    else: ini = "rapidsms.ini"


    # add the ini path to the environment, so we can
    # access it globally, including any subprocesses
    # spawned by django
    os.environ["RAPIDSMS_INI"] = ini

    # read the config, which is shared
    # between the back and frontend
    conf = Config(ini)
    
    # import the webui settings, which builds the django
    # config from rapidsms.config, in a round-about way.
    # can't do it until env[RAPIDSMS_INI] is defined
    from rapidsms.webui import settings

    # whatever we're doing, we'll need to call
    # django's setup_environ, to configure the ORM
    os.environ["DJANGO_SETTINGS_MODULE"] = "rapidsms.webui.settings"
    from django.core.management import setup_environ, execute_manager
    setup_environ(settings)

    # if one or more arguments were passed, we're
    # starting up django -- copied from manage.py
    if len(args) > 1:
        execute_manager(settings)
    
    # no arguments passed, so perform
    # the default action: START RAPIDSMS
    else:
        router = Router()
        router.set_logger(conf["log"]["level"], conf["log"]["file"])
        router.info("RapidSMS Server started up")
        
        # add each application from conf
        for app_conf in conf["rapidsms"]["apps"]:
            router.add_app(app_conf)

        # add each backend from conf
        for backend_conf in conf["rapidsms"]["backends"]:
            router.add_backend(backend_conf)

        # wait for incoming messages
        router.start()

@@ -106,16 +106,637 @@
 mport os
+, sys%0A%0A# the Manager class is a bin for various RapidSMS specific management methods%0Aclass Manager (object):%0A    def route (conf, *args):%0A        router = Router()%0A        router.set_logger(conf%5B%22log%22%5D%5B%22level%22%5D, conf%5B%22log%22%5D%5B%22file%22%5D)%0A        router.info(%22RapidSMS Server started up%22)%0A        %0A        # add each application from conf%0A        for app_conf in conf%5B%22rapidsms%22%5D%5B%22apps%22%5D:%0A            router.add_app(app_conf)%0A%0A        # add each backend from conf%0A        for backend_conf in conf%5B%22rapidsms%22%5D%5B%22backends%22%5D:%0A            router.add_backend(backend_conf)%0A%0A        # wait for incoming messages%0A        router.start()
 %0A%0Adef st
@@ -747,21 +747,16 @@
 (args):%0A
-    %0A
     # if
@@ -1178,17 +1178,16 @@
 s.ini%22%0A%0A
-%0A
     # ad
@@ -2034,631 +2034,333 @@
  if 
-len(args) %3E 1:%0A        execute_manager(settings)%0A    %0A    # no arguments passed, so perform%0A    # the default action: START RAPIDSMS%0A    else:%0A        router = Router()%0A        router.set_logger(conf%5B%22log%22%5D%5B%22level%22%5D, conf%5B%22log%22%5D%5B%22file%22%5D)%0A        router.info(%22RapidSMS Server started up%22)%0A        %0A        # add each application from conf%0A        for app_conf in conf%5B%22rapidsms%22%5D%5B%22apps%22%5D:%0A            router.add_app(app_conf)%0A%0A        # add each backend from conf%0A        for backend_conf in conf%5B%22rapidsms%22%5D%5B%22backends%22%5D:%0A            router.add_backend(backend_conf)%0A%0A        # wait for incoming messages%0A        router.start(
+not args:%0A        print %22Commands: route, startproject %3Cname%3E, startapp %3Cname%3E%22%0A        sys.exit(1)%0A%0A    if args%5B0%5D in Manager:%0A        handler = getattr(Manager, args%5B0%5D)%0A        handler(conf, *args%5B1:%5D)%0A    else:%0A        # none of the commands were recognized,%0A        # so hand off to Django%0A        execute_manager(settings
 )%0A

Refactor manager to allow other mgmt functions.