#!/usr/bin/env python
#
# Copyright 2003 by Object Craft P/L, Melbourne, Australia.
#
# LICENCE - see LICENCE file distributed with this software for details.
#
# AUTHOR(S)
#       Matt Goodall <matt AT pollenation DOT net>

import os
import sys
from albatross import httpdapp

def usage():
    sys.exit('usage: %s main-module.app-object port [static-url file-path] ...'
             % os.path.basename(sys.argv[0]))


if __name__ == '__main__':
    # Append the current working directory to the path.
    sys.path.append(os.getcwd())

    # Parse the command line.
    try:
        app_module, app_object = sys.argv[1].split('.')
        port = int(sys.argv[2])
    except:
        usage()

    static_resources = []
    pairs = sys.argv[3:]
    while pairs:
        try:
            uri, fs = pairs[:2]
        except ValueError:
            usage()
        pairs = pairs[2:]
        static_resources.append((uri, fs))

    # Load the application's module and get application instance.
    module = __import__(app_module)
    app = getattr(module, app_object)
    app._Application__base_url = '/'

    # Create the HTTP server and serve requests.
    httpd = httpdapp.HTTPServer(app, port, static_resources)
    httpd.serve_forever()
