#!/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 possible values for leveltype1 or
leveltype2 and the interpretation of the corresponding numerical
value l1 or l2.  Leveltype values in the range 0-255 can
be used for defining either a single level (leveltype1) or a surface
delimiting a layer (leveltype1 and leveltype2) with any meaningful
combination of leveltypes; values of leveltype >255 have a special use
for encoding cloud values in SYNOP reports and they do not strictly
define physical surfaces.

The idea is borrowed from the GRIB edition 2 fixed surface
concept and the values for leveltype coincide with the GRIB standard
where possible.

\verbatim
};
format DoxTop =
leveltype   Meaning                                unit/contents of l1/l2      

.
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(This table lists the possible values for {\em leveltype1} or
{\em leveltype2} and the interpretation of the corresponding numerical
value {\em l1} or {\em l2}.  Leveltype values in the range 0-255 can
be used for defining either a single level (leveltype1) or a surface
delimiting a layer (leveltype1 and leveltype2) with any meaningful
combination of leveltypes; values of leveltype >255 have a special use
for encoding cloud values in SYNOP reports and they do not strictly
define physical surfaces.

The idea is borrowed from the GRIB edition 2 {\em fixed surface}
concept and the values for leveltype coincide with the GRIB standard
where possible.

\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 leveltype} & {\em Meaning} & {\em unit/contents of l1/l2} \\\\
\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;
