#! /usr/bin/perl -w

use strict;

my $input;
my $header;
my $comment;

@ARGV == 2 or die "Usage: $0 <infile> <outfile>\n";
$input = $ARGV[0];
$header = $ARGV[1];
open(IF, "$input") or die "Failed to write '$input': $!\n";
open(HF, ">$header") or die "Failed to write '$header': $!\n";

while(<IF>) {
	chomp;
	undef $comment;
	s/\r//;				# CRLF -> LF
	if(s/\s*[;#]\s*(.*?)$//) {	# Comments
		$comment = $1;
	}
	if(/^\s*$/) {		# Empty lines
		next;
	}
	my ($slic0, $slic1, $slic2, $slic3, $len, @data) = split;
	die "Bad input (len=$len)" if hex($len) != @data;
	my $slic = "$slic3$slic2$slic1$slic0";
	die "Bad slic address '$slic'" if length($slic) != 8;
	grep(s/\w+/0x$&/g, ($slic, $len, @data));
	my $str = join(", ", @data);
	print HF "S_($slic,\t$len,\t$str),\t";
} continue {
	if(defined($comment)) {
		print HF "// $comment";
	}
	print HF "\n";
}
