## -*- perl -*-
## ----------------------------------------------------------------------
## spec/latex2e: SGML conversion specification for LaTeX2e output
## ----------------------------------------------------------------------
## Copyright (C) 1998 Ardo van Rangelrooij
## Copyright (C) 1997 Christian Leutloff
##
## This is free software; see the GNU General Public Licence
## version 2 or later for copying conditions.  There is NO warranty.
## ----------------------------------------------------------------------

## ----------------------------------------------------------------------
use POSIX;

## ----------------------------------------------------------------------
$format = '.latex2e';

## ----------------------------------------------------------------------
## language definition
%languages = (
	      'da' => 'danish',
	      'de' => 'german',
	      'es' => 'spanish',
	      'fr' => 'french',
	      'it' => 'italian',
	      'nl' => 'dutch'
	      );
( $lang = POSIX::setlocale( LANG ) ) =~ s/_.*//;
if ( -r $ENV{ 'DEBIANDOCSGMLSPEC' } . '/i18n/' . $lang . $format )
{
    $language = $languages{ $lang };
    do $ENV{ 'DEBIANDOCSGMLSPEC' } . '/i18n/' . $lang . $format;
}
else
{
    $language = 'english';
    %lang = (
	     'abstract' => 'Abstract',
	     'copyright notice' => 'Copyright Notice',
	     );
}

## ----------------------------------------------------------------------
## paper size definition
my @paper = split( /\s/, `2>/dev/null paperconf -N` );
if ( $#paper >= 0 )
{
    if ( $paper[0] =~ m/[ABC][0-9]/ )
    {
	$pagespec = $paper[0];
    }
    elsif ( $paper[0] =~ m/[letter|legal|executive]/ )
    {
	$paper[0] =~ tr/A-Z/a-z/;
	$pagespec = "US" . $paper[0];
    }
    else
    {
	$pagespec = "A4";
    }
}
else
{
    $pagespec = "A4";
}

## ----------------------------------------------------------------------
## tag processing
## ----------------------------------------------------------------------

## ----------------------------------------------------------------------
sub start_chapt
{
    _sect( -1, @_ );
}
sub end_chapt
{
}

## ----------------------------------------------------------------------
sub start_appendix
{
    _sect( -2, @_ );
}
sub end_appendix
{
}

## ----------------------------------------------------------------------
sub start_heading
{
    push( @elements, $element );
    push_output( 'string' );
}
sub end_heading
{
    my $heading = pop_output;
    $heading =~ s/^\s+//;
    $heading =~ s/\s+$//;
    $heading =~ s/\s+/ /;

    $element = pop( @elements );
    $pagemark = _a( 'SRID' );

    output( "\n" );
    if ( $headinglevel == -2 )
    {
	if ( $first_appendix == 0 )
	{
	    $first_appendix = 1;
	    output( "\\appendix\n" );
	    output( "\n" );
	}
    }
    if ( $headinglevel < 0 )
    {
	output( "\\chapter" );
    }
    elsif ( $headinglevel == 0 )
    {
	output( "\\section" );
    }
    elsif ( $headinglevel == 1 )
    {
	output( "\\subsection" );
    }
    elsif ( $headinglevel == 2 )
    {
	output( "\\subsubsection" );
    }
    elsif ( $headinglevel == 3 )
    {
	output( "\\paragraph" );
    }
    else
    {
	output( "\\subparagraph" );
    }
    output( "{" );
    output( "$heading" );
    output( "}\n" );
    output( "\\label{$pagemark}\n" ) if length( $pagemark );
    if ( ! $setarabic )
    {
	$setarabic = 1;
	output( "\\pagenumbering{arabic}\n" );
    }
    output( "\n" );
}

## ----------------------------------------------------------------------
sub start_list
{
    _start_list( 'Bullet', @_ );
}
sub end_list
{
    _end_list();
}

## ----------------------------------------------------------------------
sub start_enumlist
{
    _start_list( 'Enum', @_ );
}
sub end_enumlist
{
    _end_list();
}

## ----------------------------------------------------------------------
sub start_taglist
{
    _start_list( 'Tagged', @_ );
}
sub end_taglist
{
    _end_list();
}

## ----------------------------------------------------------------------
sub start_item
{
    if ( $ltype ne 'Tagged' )
    {
        output( "\\item " );
    }
}
sub end_item
{
}

## ----------------------------------------------------------------------
## output definitions
## ----------------------------------------------------------------------

## ----------------------------------------------------------------------
sub _output_start_book
{
    output( "\\documentclass[11pt,$language]{report}\n" );
    output( "\\usepackage{babel}\n" );
    output( "\n" );
    output( "\\usepackage{vmargin}\n" );
    output( "\\setpapersize{$pagespec}\n" );
    output( "\\setmarginsrb{25.4mm}{25.4mm}{25.4mm}{25.4mm}{15pt}{11mm}{0pt}{11mm}\n" );
    output( "\n" );
    output( "\\usepackage{t1enc}\n" );
    output( "\\usepackage[latin1]{inputenc}\n" );
    output( "\\usepackage{helvet}\n" );
    output( "\\usepackage{times}\n" );
    output( "\n" );
    output( "\\usepackage{fancyhdr}\n" );
    output( "\\pagestyle{fancy}\n" );
    output( "\n" );
    output( "\\renewcommand{\\chaptermark}[1]{%\n" );
    output( "\\markboth{\\chaptername\\ \\thechapter.\\ #1}{}}\n" );
    output( "\n" );
    output( "\\lhead{\\leftmark}\n" );
    output( "\\chead{}\n" );
    output( "\\rhead{\\thepage}\n" );
    output( "\\lfoot{}\n" );
    output( "\\cfoot{}\n" );
    output( "\\rfoot{}\n" );
    output( "\\renewcommand{\\headrulewidth}{0.4pt}\n" );
    output( "\\renewcommand{\\footrulewidth}{0pt}\n" );
    output( "\n" );
    output( "\\fancypagestyle{plain}{%\n" );
    output( "\\lhead{}\n" );
    output( "\\chead{}\n" );
    output( "\\rhead{\\thepage}\n" );
    output( "\\lfoot{}\n" );
    output( "\\cfoot{}\n" );
    output( "\\rfoot{}\n" );
    output( "\\renewcommand{\\headrulewidth}{0.4pt}\n" );
    output( "\\renewcommand{\\footrulewidth}{0pt}}\n" );
    output( "\n" );
    output( "\\usepackage{alltt}\n" );
    output( "\\usepackage{url}\n" );
    output( "\\usepackage{varioref}\n" );
    output( "\n" );
    output( "\\parindent=0pt\n" );
    output( "\\setlength{\\parskip}{%\n" );
    output( "0.5\\baselineskip plus0.1\\baselineskip minus0.1\\baselineskip}\n" );
    output( "\n" );
    output( "\\begin{document}\n" );
    output( "\n" );
}
sub _output_end_book
{
    output( "\\end{document}\n");
    output( "\n" );
}

## ----------------------------------------------------------------------
sub _output_titlepag
{
    $headinglevel = 0;
    output( "\\begin{titlepage}\n" );
    output( "\\thispagestyle{empty}\n" );
    output( "\n" );
    output( "\\begin{center}\n" );
    output( "{\\Huge \\vspace*{2ex} $title \\\\[2ex]}\n" );
    grep( output( "{\\large $_ } \\\\\n" ), @authors );
    if ( length( $version ) )
    {
	output( "\\vspace*{1ex} $version \\\\\n" );
    }
    output( "\\end{center}\n" ); 
    output( "\n" );
    if ( length( $abstract ) )
    {
	output( "\\vspace*{1ex}\n" );
	output( "\\vfill\n" );
	output( "\\begin{center}\n" );
	output( "\\section*{$lang{ 'abstract' }}\n" );
	output( "\\end{center}\n" ); 
	output( "\n" );
	output( "$abstract\n" );
	output( "\\vspace*{2ex}\n" );
	output( "\n" );
    }
    if ( length($copyright ) )
    {
	output( "\\vspace*{1ex}\n" );
	output( "\\vfill\n" );
	output( "\\section*{$lang{ 'copyright notice' }}\n" );
	output( "\n" );
	output( "$copyright\n" );
	output( "\\vspace*{2ex}\n" );
	output( "\n" );
    }
    output( "\\end{titlepage}\n" ); 
    output( "\n" );
}

## ----------------------------------------------------------------------
sub _output_copyrightsummaries
{
    my ( @copyrightsummaries ) = @_;
    output( join( " \\\\\n", @copyrightsummaries ), "\n" );
    output( "\n" );
}

## ----------------------------------------------------------------------
sub _output_toc
{
    my ( $toc ) = @_;
    output( "\\pagenumbering{roman}\n" );
    output( "\\tableofcontents\n" );
    output( "\n" );
}

## ----------------------------------------------------------------------
sub _output_tocentry
{
    my ( $tocentry ) = @_;
}

## ----------------------------------------------------------------------
sub _output_p
{
    my ( $paragraph ) = @_;
    output( "$paragraph\n" );
    if ( ( ! $compact ) && $was_compact )
    {
	$was_compact = 0;
	output( "\\setlength{\\parskip}{%\n" );
	output( "0.5\\baselineskip plus0.1\\baselineskip minus0.1\\baselineskip}\n" );
    }
    output( "\n" );
}

## ----------------------------------------------------------------------
sub _output_example
{
    my ( $example ) = @_;
    output( "\\begin{alltt}\n" );
    output( "$example\n" );
    output( "\\end{alltt}" );
}

## ----------------------------------------------------------------------
sub _output_footnotes
{
}

## ----------------------------------------------------------------------
sub _output_footnote
{
    my ( $footnote ) = @_;
    if ( $stat =~ m/f$/ )
    {
	output( "\$^,\$" );
    }
    output( "\\footnote{" );
    output( $footnote );
    output( "}" );
}

## ----------------------------------------------------------------------
sub _output_footnotebody
{
}

## ----------------------------------------------------------------------
sub _output_footnoteref
{
}

## ----------------------------------------------------------------------
sub _output_tag
{
    my ( $tag ) = @_;
    output( "\\item[\\textbf{" );
    output( "$tag" );
    output( "}]\n" );
}

## ----------------------------------------------------------------------
sub _output_em
{
    my ( $em ) = @_;
    output( "\\textit{" );
    output( "$em" );
    output( '}' );
}

## ----------------------------------------------------------------------
sub _output_strong
{
    my ( $strong ) = @_;
    output( "\\textbf{" );
    output( "$strong" );
    output( '}' );
}

## ----------------------------------------------------------------------
sub _output_var
{
    my ( $var ) = @_;
    output( "\\textit{" );
    output( "$var" );
    output( '}' );
}

## ----------------------------------------------------------------------
sub _output_package
{
    my ( $package ) = @_;
    output( "\\texttt{" );
    output( "$package" );
    output( '}' );
}

## ----------------------------------------------------------------------
sub _output_prgn
{
    my ( $prgn ) = @_;
    output( "\\texttt{" );
    output( "$prgn" );
    output( '}' );
}

## ----------------------------------------------------------------------
sub _output_file
{
    my ( $file ) = @_;
    output( "\\path{" );
    output( "$file" );
    output( "}" );
}

## ----------------------------------------------------------------------
sub _output_tt
{
    my ( $tt ) = @_;
    output( "\\texttt{" );
    output( "$tt" );
    output( '}' );
}

## ----------------------------------------------------------------------
sub _output_qref
{
    my ( $qref ) = @_;
    output( $qref );
}

## ----------------------------------------------------------------------
sub _output_ref
{
    my ( $ref ) = @_;
    my $refname = _a( 'SRID' );
    _odata( '`' );
    output( $ref );
    _odata( "' " );
    output( "\\vpageref{$refname}" );
}

## ----------------------------------------------------------------------
sub _output_manref
{
    my ( $name, $section ) = @_;
    output( "\\texttt{" );
    _odata( $name . '(' . $section . ')' );
    output( '}' );
}

## ----------------------------------------------------------------------
sub _output_email
{
    my ( $email ) = @_;
    output( " " ) if ( $in_author );
    output( "\\texttt{" );
    _odata( '<' );
    output( $email );
    _odata( '>' );
    output( '}' );
}

## ----------------------------------------------------------------------
sub _output_ftpsite
{
    my ( $ftpsite ) = @_;
    output( "\\url{" );
    output( $ftpsite );
    output( '}' );
}

## ----------------------------------------------------------------------
sub _output_ftppath
{
    my ( $ftpsite, $ftppath ) = @_;
    output( "\\path{" );
    output( $ftppath );
    output( '}' );
}

## ----------------------------------------------------------------------
sub _output_httpsite
{
    my ( $httpsite ) = @_;
    output( "\\url{" );
    output( $httpsite );
    output( '}' );
}

## ----------------------------------------------------------------------
sub _output_httppath
{
    my ( $httpsite, $httppath ) = @_;
    output( "\\path{" );
    output( $httppath );
    output( '}' );
}

## ----------------------------------------------------------------------
sub _output_url
{
    my ( $id, $name ) = @_;
    _odata( $name ) if $name ne "";
    output( " (" ) if $name ne "";
    output( "\\url{" );
    output( $id );
    output( "}" );
    output( ")" ) if $name ne "";
}

## ----------------------------------------------------------------------
sub _odata
{
    ( $data, $event ) = @_;
    $stat =~ s/f$//;
    $_ = $data;
    if ( $stat =~ m/x/ )
    {
        output( _sani( $_, 1 ) );
    }
    else
    {
        s/\s+/ /g;
        output( _sani( $_, ( $stat =~ m/c[^R]*$/ ) ) );
    }
}

## ----------------------------------------------------------------------
## helper definitions
## ----------------------------------------------------------------------

## ----------------------------------------------------------------------
sub _sect
{
    ( $headinglevel, $element, $event ) = @_;
}

## ----------------------------------------------------------------------
sub _start_list
{
    $stat =~ s/f$//;
    push( @stats, $stat );
    push( @ltypes, $ltype );
    ( $ltype, $element, $event ) = @_;
    $compact++ if ( $compact 
		    || $element->attribute( 'COMPACT' )->type eq 'TOKEN' );
    if ( $compact )
    {
	$was_compact = 1;
	output( "\\setlength{\\parskip}{0ex}\n" );
    }
    if ( $ltype ne 'Tagged' )
    {
	if ( $ltype eq 'Enum' )
	{
	    $counter++;
	    output( "\\newcounter{enum$counter}" );
	    output( "\\begin{list}%\n" );
	    output( "{\\arabic{enum$counter}.}%\n" );
	    output( "{\\usecounter{enum$counter}%\n" );
	}
	else
	{
	    output( "\\begin{list}%\n" );
	    output( "{\\labelitemi}%\n" );
	    output( "{%\n" );
	}
    }
    else
    {
	output( "\\begin{list}%\n" );
	output( "{}%\n" );
	output( "{%\n" );
    }
    if ( $compact )
    {
	output( "\\setlength{\\topsep}{0ex}%\n" );
	output( "\\setlength{\\parskip}{0ex}%\n" );
	output( "\\setlength{\\partopsep}{0ex}%\n" );
	output( "\\setlength{\\itemsep}{0ex}%\n" );
	output( "\\setlength{\\parsep}{0ex}%\n" );
    }
    output( "}\n" );
    output( "\n" );
}
sub _end_list
{
    output( "\\end{list}\n" );
    $compact-- if $compact;
    $ltype = pop( @ltypes );
    $stat = pop( @stats );
}

## ----------------------------------------------------------------------
sub _sani
{
    ( $_, $hyphens ) = @_;

    # replace backslash character
    s/\\/\\(\\backslash\\)/g;

    # escape command characters
    s/{/\\{/g;
    s/}/\\}/g;

    # no further replacement in examples
    return $_ if $stat =~ m/x/;

    # hyphens
    s/-/--/g if $hyphens;
    s/----/---/g if $hyphens;

    # escape command characters
    s/_/\\_/g;
    s/&/\\&/g;
    s/\#/\\\#/g;
    s/\$/\\\$/g;
    s/\%/\\\%/g;
    s/\^/\\^{}/g;
    s/~/\\~{}/g;
    s/\xa9/\\copyright/g;

#    s/\"/\"\"/g;
    s/\`\`/\"\`/g;
    s/\'\'/\"\'/g;

    # missing `\ ' after "xxx" ->
    s/u\.a\./u\.a\.\\ /g;
    s/etc\./etc\.\\ /g;
    s/usw\./usw\.\\ /g;
#    s/\./\.\\ /g;

    # dots should be ellipsis "..."
    s/\.\.\./\\dots /g;

    $_;
}

## ----------------------------------------------------------------------
## SGML definitions
## ----------------------------------------------------------------------

use DebianDoc_SGML;

## ----------------------------------------------------------------------
