BEGIN {
	if (emit == "enum") {
		print "typedef enum bad {"
	} else if (emit == "msg") {
		print "#define kpw_emit_error_switch \\"
		code = 0
	}
}

function name(n) {
	if (NR > 2) {
		return "bad_" n
	} else {
		return n
	}
}

emit == "enum" {
	print "\t" name($1) ","
}

emit == "msg" {
	if (NF >= 4) {
		retval = $4
		code = retval + 1
	} else {
		retval = code++
	}

	if (NF >= 3) {
		level = "a_" $3
		if (level == "a_fatal") {
			level = level " | " retval
		}
	} else {
		level = "a_fatal | " retval
	}

	print "\t" \
		  "case " name($1)":" \
		  "level=" level ";" \
		  "msg=\""$2"\";" \
		  "rv=" retval";" \
		  "break; \\"
}

END {
	if (emit == "enum") {
		print "} bad;"
	} else if (emit == "msg") {
		print ""
	}
}
