#!/usr/bin/perl -w

use strict;
use warnings;

my $op = $ARGV[0];

sub n($) {
	return $_[0] if defined $_[0];
	return '';
}

# Read the data
my @items;
while (<STDIN>)
{
	chop();
	my @l = split(/\t+/);
#	if ($l[0] =~ /^(\d+)-(\d+)$/)
#	{
#		for my $i ($1..$2)
#		{
#			push @items, [$i, $l[1] or '', $l[2] or '', $l[3] or ''];
#		}
#	}
#	else
#	{
		push @items, [$l[0], $l[1], n($l[2])];
#	}
}


# Formatting setup for Doxygen output

my ($ltype, $desc, $l1);

our $DoxIntro = q{/**@defgroup level_table Level type values
@ingroup tables

This table lists the various level type and l1 value combinations that can be
used to specify a vertical level.

They values closely correspond to how a level is specified in the GRIB file
format.

\verbatim
};
format DoxTop =
Level type  Description                            L1 value                    

.
format Dox =
@<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<< 
$ltype,     $desc,                                 $l1,                        
~           ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<< 
            $desc,                                 $l1,                        
~           ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<< 
            $desc,                                 $l1,                        
~           ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<< 
            $desc,                                 $l1,                        
~           ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<< 
            $desc,                                 $l1,                        

.
our $DoxBottom = q{\endverbatim
*/
};



if ($op eq 'dox')
{
	print $DoxIntro;
	$^L = "\n\n";
	$^ = "DoxTop";
	$~ = "Dox";
	for my $l (@items)
	{
		($ltype, $desc, $l1) = @$l;
		write STDOUT;	
	}
	print $DoxBottom;
}
elsif ($op eq 'tex')
{
	print q(\begin{scriptsize}
\begin{longtable}{|@{\hspace{0.5mm}}l@{\hspace{0.5mm}}|@{\hspace{0.5mm}}p{4.0cm}@{\hspace{0.5mm}}|@{\hspace{0.5mm}}p{4.0cm}@{\hspace{0.5mm}}|@{\hspace{0.5mm}}p{4.0cm}@{\hspace{0.5mm}}|}
\hline
{\em Level type} & {\em Meaning} & {\em Contents ({\tt l1})} \\\\
\hline
\endhead
\hline
\endfoot
);

	for my $l (@items)
	{
		print join(' & ', @$l), "\\\\\n";
	}

	print q(\hline
\end{longtable}
\end{scriptsize}
);
} else {
	print STDERR "Unknown operation: $op\n";
	exit 1;
}

exit 0;
