#!/usr/bin/python

import re

print "Auto-generating menu_autogen.h..."

items=set([])

fd=file("menu.c")
for line in fd:
	m = re.search(r"menu_item_(\w+)\s*\(",line)
	if m:
		items.add( m.groups()[0])

fd.close()

items=list(items) # create clear ordering

fd=file("menu_autogen.h","wb")

fd.write("\n");
for i in range(len(items)):
	fd.write("extern int8_t menu_item_%s();\n" % items[i])

fd.write("\ntypedef int8_t (*item_handler_t)();\n\n")

fd.write("item_handler_t menu_item_handlers[] = {\n")
fd.write("\t\tNULL")

for i in range(len(items)):
	fd.write(",\n\t\t*menu_item_%s" % items[i])

fd.write("\n};\n\n")

for i in range(len(items)):
	fd.write("#define MENU_ITEM_%s \t%s\n" % (items[i].upper(), i+1))

fd.write("\n")
fd.write("#define MENU_MAX_ITEM %s\n\n"%len(items))




