# find initial file
$file = shift(@ARGV);

# set output to binary mode
binmode(STDOUT);

# no output yet
$output = "";

# no entries yet
$entries = 0;

# parse the initial file
parse_file($file);

# output a header
print pack("a4LL", "SPRT", 1, $entries);

# output the entries
print $output;

# done!
exit 0;

# read and parse a sprite table file
sub parse_file
{
	local($file) = @_;

	print STDERR "reading $file:\n";

	local(@lines) = ();

	# open the file
	open(FILE, $file);

	while (<FILE>)
	{
		# read file line
		chop;
		push(@lines, $_);
	}

	# close the file
	close(FILE);

	for (@lines)
	{
		# if the line is blank...
		if (/^\s*$/)
		{
			next;
		}

		# if the line starts with a #...
		if (/^\s*#/)
		{
			next;
		}

		# if the line is an include...
		# [@include "<file>"]
		if (/^\s*\@include "(.+)"/)
		{
			# parse the included file
			parse_file($1);
			next;
		}

		# attempt to parse the line:
		# ["<label>" <texture> <size> <u0> <v0> <u1> <v1> <x0> <y0> <x1> <y1> <flags>]
		($l, $t, $s, $u0, $v0, $u1, $v1, $x0, $y0, $x1, $y1, $f) = split(' ');

		# strip quotes off the label
		$l =~ tr/"//d;

		# convert flag string into an integer
		$f = hex($flags);

		# add an entry to the output
		$output = $output . pack("a16a16SSSSSSSSSL", 
			$l, $t, $s, $u0, $v0, $u1, $v1, $x0, $y0, $x1, $y1, $f);

		# increment the entry count
		$entries++;
	}
}

