#!/usr/local/bin/perl
$new_map=1;
$count=1;
%funcs=();

print "/* Automatically generated from Keymap by gen_keymap.  DO NOT EDIT! */\n\n";
while (<>) {
	next if /^#/;
	if ($new_map) {
		chop;
		@block=();
		push(@block, "\n#ifndef KEYMAP_C\nextern struct binding_t $_\[\];\n#else\n");
		push(@block, "struct binding_t $_\[\] = {\n");
		$new_map=0;
	}
	elsif (/^$/) {
		print @block;
		print "  { 0, 0, 0 }\n};\n#endif\n\n";
		$new_map = 1;
	}
	else {
		chop;
		@parts = split(/\s/, $_);
		if (!defined($funcs{$parts[1]})) {
			print "#define $parts[1] $count\n";
			$funcs{$parts[1]}=$count;
			$count++;
		}

		push(@block, "  { $parts[0], $parts[1], ");
		shift @parts;
		shift @parts;
		push(@block, join(' ', @parts) . " },\n");
	}
}
