#!/usr/bin/perl -w

use strict;
use warnings;

my $op = $ARGV[0];

# Skip the declaration section
while (<STDIN>)
{
	last if $_ eq "%%\n";
}

# Read the data
my @data;
my $pos = 0;
while (<STDIN>)
{
	chop();
	last if $_ eq '%%';

	my ($name, $val) = split(/,\s*/, $_, 2);

	$val =~ s/DBA_VAR\(0,\s*([0-9]+),\s*([0-9]+)\)/sprintf("B%02d%03d", $1, $2)/e;
	$name =~ s/_/\\_/g;

	push @data, [$name, $val];
}

@data = sort { $a->[0] cmp $b->[0] } @data;

our ($alias, $var);

our $DoxIntro = q{/**@defgroup dba_core_aliases Variable aliases
@ingroup tables

This table lists the aliases that can be used to refer to varcodes.

\verbatim
};
format DoxTop =
Alias         Variable
.
format Dox =
@<<<<<<<<<<<< @<<<<<
$alias,       $var,
.
our $DoxBottom = q{\endverbatim
*/
};

if ($op eq 'dox')
{
	print $DoxIntro;
	$^ = "DoxTop";
	$~ = "Dox";
	for my $d (@data)
	{
		($alias, $var) = @$d;
		write STDOUT;	
	}
	print $DoxBottom;
}
elsif ($op eq 'tex')
{
	print qq#{\\begin{scriptsize}
\\begin{tabular}{|l|l|}
\\hline
{\\em Alias} & {\\em Variable} \\\\
\\hline
#;

	for my $d (@data)
	{
		print join(' & ', @$d), "\\\\\n";
	}
	
	print q#\hline
\end{tabular}
\end{scriptsize}
}
#;
}
else
{
	die "unknown output format ".$ARGV[0];
}


