#!/usr/bin/python2.4 -t

import re, os, string, sys

if len(sys.argv) < 3 or len(sys.argv) > 4:
    print >>sys.stderr, '''\
Usage: %s NAMESPACE:LOCALNAME [TypeName]
Generates the header file for TypeName.

NAMESPACE  - namespace of the type, for instance 'GTK'
LOCAL_NAME - local name of the type, for instance 'TREE_VIEW'
TypeName   - optionally the type name, for instance 'GtkTreeView
''' % os.path.basename(sys.argv[0])

    raise SystemExit, 1

namespace, localname = map(string.upper, sys.argv[1:3])
fnprefix = '_'.join((namespace, localname)).lower()

typename = (
    len(sys.argv) > 3 and sys.argv[3] or
    re.subn('(^|_)(\w)', lambda m: m.group(2).upper(), fnprefix)[0])

template = '''\
#ifndef %(namespace)s_%(localname)s_INCLUDED
#define %(namespace)s_%(localname)s_INCLUDED

#include <glib-object.h>

#define %(namespace)s_TYPE_%(localname)s           (%(fnprefix)s_get_type())
#define %(namespace)s_%(localname)s(obj)           (G_TYPE_CHECK_INSTANCE_CAST(obj, %(namespace)s_TYPE_%(localname)s, %(typename)s))
#define %(namespace)s_%(localname)s_CLASS(klass)   (G_TYPE_CHECK_CLASS_CAST(klass, %(namespace)s_TYPE_%(localname)s, %(typename)sClass))
#define %(namespace)s_IS_%(localname)s(obj)        (G_TYPE_CHECK_INSTANCE_TYPE(obj, %(namespace)s_TYPE_%(localname)s))
#define %(namespace)s_IS_%(localname)s_CLASS(obj)  (G_TYPE_CHECK_CLASS_TYPE(obj, %(namespace)s_TYPE_%(localname)s))
#define %(namespace)s_%(localname)s_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), %(namespace)s_TYPE_%(localname)s, %(typename)sClass))

GType %(fnprefix)s_get_type();

#endif // vim: et sta sw=4'''

print template % vars()
