# Manifest of files to include in the simple HTTP server.
# Run this script to generate the httpdocs.cpp source file.

manifest = [
    ('/', 'index.html', 'text/html'),

    # Images
    ('/media/favicon.png', None, 'image/png'),
    ('/favicon.ico', 'media/favicon.ico', 'image/x-icon'),

    # Javascript and CSS
    ('/js/home.js', None, 'application/javascript'),
    ('/css/narrow.css', None, 'text/css'),

    # Redistributed libraries
    ('/dist/css/bootstrap.min.css', None, 'text/css'),
    ('/dist/js/bootstrap.min.js', None, 'application/javascript'),
    ('/dist/js/jquery-1.10.2.min.js', None, 'application/javascript'),

    # 404 error document must be last.
    (None, '404.html', 'text/html'),
]

import json, sys, os, zlib

sys.stdout.write("""/*
 * HTTP Document data.
 * Automatically generated by manifest.py
 */

#include "tcpnetserver.h"

TcpNetServer::HTTPDocument TcpNetServer::httpDocumentList[] = {
""")

def quote(buf):
    # Encode a byte buffer as a C++ string, and octal-escape any funny characters

    if buf is None:
        return 'NULL'
    output = ['"']

    allowedBytes = [c for c in range(ord(' '), ord('~') + 1) if chr(c) not in '"\\?']
    for c in buf:
        byte = c
        if isinstance(c, str):
          byte = ord(c)
        if byte in allowedBytes:
            output.append(chr(byte))
        else:
            output.append('\\%d%d%d' % (byte >> 6, (byte >> 3) & 7, byte & 7))

    output.append('"')
    return ''.join(output)

for path, filename, contentType in manifest:
    if filename is None:
        filename = '.' + path

    if contentType.startswith('text/'):
        raw = open(filename, 'r').read().encode('UTF-8')
    else:
        raw = open(filename, 'rb').read()
    data = zlib.compress(raw)
    sys.stdout.write("{ %s, %s, %s, %d },\n" %
        (quote(path), quote(data), quote(contentType), len(data)))

sys.stdout.write("};\n")
