#! /usr/bin/awk -f
#
# Bug header file definition generator
#
# Copyright (C) 2005-2006 Intel Corporation
# Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version
# 2 as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
#
# Silly script for generating a header file with bug definitions from
# an input file (bugs).

BEGIN {
	print "";
	print "#ifndef __UWB_BUGS_H__";
	print "#define __UWB_BUGS_H__";
	print "";
	print "#include <linux/uwb/bugs-macros.h>";
	print "";
	error = 0;
	bugs = 0;
	str = "";
}

# Eat comments and whitespace

/^[ \t]*(\#.*)?$/ {
	next;
}

NF == 2 || NF == 3 {
	number=$1;
	state=$2;
	qualifier=$3;
	# Check bug number
	if (!(number ~ /^[0-9]+$/)) {
		print FILENAME ":" FNR ": bad bug number '" number \
			"'(has to be numbers)" > "/dev/stderr";
		error = 1;
	}
	# Check state
	if (state == "enabled") {
		state = 1;
	}
	else if (state == "disabled") {
		state = 0;
	}
	else if (state ~ /^[0-9a-zA-Z_]+$/) {
		state = state;	// dah
	}
	else {
		print FILENAME ":" FNR ": bad state name '" state \
			"'(has to be 'disabled' or 'enabled')" > "/dev/stderr";
		error = 1;
	}
	if (NF > 2) {
		if (!(qualifier ~ /^[0-9a-zA-Z_]+$/)) {
			print FILENAME ":" FNR ": workaround qualifier '" qualifier \
				"' contains bad characters (only 0-9a-zA-Z_ allowed)" \
				> "/dev/stderr";
			error = 1;
		}
		qualifier_symbol = "_" qualifier;
		qualifier_str = "_" qualifier;
	}
	else {
		qualifier_symbol = "";
		qualifier_str = "";
	}
	if (error == 0) {
		print "#define UWB_BUG_" number qualifier_symbol " " state;
		if (state == 1)
			str = str " " number qualifier_str;
		bugs++;
	}
	next;
}


{
	print FILENAME ":" FNR ": bad format in line " \
		"(format is: NUMBER {enabled,disabled})" > "/dev/stderr";
	error = 1;
}


END {
	if (error == 0) {
		print "";
		print "#define UWB_BUGS_ENABLED \"" str "\"";
		print "";
		print "enum { UWB_BUG_COUNT = " bugs " };";
		print "";
		print "#endif /* #ifndef __UWB_BUGS_H__ */";
	}
	exit error;
}
