#!/usr/bin/env python
import sys
import re
import os.path

module_header = '''#ifndef XX__XX_H
#define XX__XX_H

#endif

'''

module_source = '''#include "xx__xx.h"

'''

module_test = '''extern "C" {
#include "xx__xx.h"
}

#include "CppUTest/TestHarness.h"

TEST_GROUP(xx__xx) {
    void setup()
    {
    }
    void teardown()
    {
    }
};

TEST(xx__xx, init)
{
    FAIL("Start here");
}

'''

templates = ( (module_header, '{0}.h')
        , (module_source, '{0}.c')
        , (module_test, 'tests/{0}_test.cpp'))

def process_template(template, output, name):
    source = template.replace('XX__XX', name.upper())
    source = source.replace('xx__xx', name)
    open(output, 'w').write(source)

def new_module(name):
    for tmplt, offmt in templates:
        of = offmt.format(name)
        if not os.path.exists(of):
            process_template(tmplt, of, name)

def usage():
    prg = os.path.basename(sys.argv[0])
    s = 'Usage: {0} <module name>\n'.format(prg)
    sys.stderr.write(s)

def main():
    try:
        name = sys.argv[1]
    except IndexError:
        usage()
        sys.exit(1)
    new_module(name)

if __name__ == '__main__':
    main()

