import sys
from wsgiref.simple_server import make_server


EMPLOYEES_XML = """\
<employees>
    <employee id="101" code="233">
        <firstName>Bob</firstName>
        <lastName>Costas</lastName>
    </employee>
    <employee id="102" code="233">
        <firstName>Bob</firstName>
        <lastName>Sagat</lastName>
    </employee>
    <employee id="103" code="233">
        <firstName>Harbor</firstName>
        <lastName>Oaks</lastName>
    </employee>
    <employee id="104" code="233">
        <firstName>Oak</firstName>
        <lastName>Barrel</lastName>
    </employee>
    <employee id="105" code="233">
        <firstName>Sag</firstName>
        <lastName>Harbor</lastName>
    </employee>
</employees>
"""
EMPLOYEES_XML_LEN = str(len(EMPLOYEES_XML))
CROSSDOMAIN_XML = """\
<cross-domain-policy>
  <allow-access-from domain="*"/>
</cross-domain-policy>
"""
CROSSDOMAIN_XML_LEN = str(len(CROSSDOMAIN_XML))


def main(sysargs=sys.argv[:]):
    server = make_server('0.0.0.0', 18080, employees_app)
    server.serve_forever()
    return 0


def employees_app(environ, start_response):
    path_info = environ.get('PATH_INFO', '/').strip(' /')
    if path_info == 'employees.xml':
        start_response('200 OK', [
            ('content-type', 'text/xml'),
            ('content-length', EMPLOYEES_XML_LEN),
        ])
        return [EMPLOYEES_XML]
    elif path_info == 'crossdomain.xml':
        start_response('200 OK', [
            ('content-type', 'text/xml'),
            ('content-length', CROSSDOMAIN_XML_LEN),
        ])
        return [CROSSDOMAIN_XML]
    else:
        start_response('404 Not Found', [('content-type', 'text/plain')])
        return ['sorry charlie']


if __name__ == '__main__':
    sys.exit(main())

# vim:filetype=python
