#!/usr/bin/perl
#=======================================================================
# Copyright (C) 2003 Daniele Giacomini daniele@swlibero.org
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#=======================================================================
# program < OLD_ALML_SOURCE > NEW_ALML_SOURCE
#
# Convert verbatim environments to listings.
#=======================================================================

# Record letto.
$riga = "";

# Subito dopo un comando
$dopo_un_comando = 0;

$workinfo = 0;


# Salta blocco.
sub SaltaBlocco
{
    local ($conclusione) = $_[0];

    #print STDERR "\n"; #!!!!!!!!!!!

    # Cerca di saltare il blocco.
    while ($riga !~ m|$conclusione|i)
      {

	#print STDERR "saltato: $riga"; #!!!!!!!!!!!

	# Trasferisce il record letto, e legge il successivo.
	print STDOUT "$riga";
	$riga = <STDIN>;
      }

    #print STDERR "saltato: $riga"; #!!!!!!!!!!!

    # Trasferisce anche l'ultimo record letto.
    print STDOUT "$riga";
}    

# Elabora verbatimpre.
sub ElaboraVerbatimpre
{
    local ($contenuto) = "";
    local ($big) = 0;

    # Cerca di accumulare il blocco.
    $riga = <STDIN>;
    while ($riga !~ m|</verbatimpre>|i)
      {
	$contenuto = $contenuto . $riga;
	$riga = <STDIN>;
      }

    if (length ($contenuto) > 800)
      {
        $big = 1;
      }

    if ($dopo_un_comando)
      {

        if ($big)
	  {
	    print STDOUT "<listing sep=\"&STYLE.LISTING.SEP.OUTPUT.COMMAND;\" pos=\"fixed\" split=\"1\">\n";
	  }
	else	  
	  {
	    print STDOUT "<listing sep=\"&STYLE.LISTING.SEP.OUTPUT.COMMAND;\" pos=\"fixed\" split=\"0\">\n";
	  }
	print STDOUT "<verbatimpre border=\"&STYLE.PRE.BORDER.OUTPUT.COMMAND;\">\n";
      }	  
    else
      {
        if ($big)
	  {
	    print STDOUT "<listing sep=\"&STYLE.LISTING.SEP.FILE.CONTENT;\" pos=\"fixed\" split=\"1\">\n";
	  }
	else	  
	  {
	    print STDOUT "<listing sep=\"&STYLE.LISTING.SEP.FILE.CONTENT;\" pos=\"fixed\" split=\"0\">\n";
	  }
	print STDOUT "<verbatimpre border=\"&STYLE.PRE.BORDER.FILE.CONTENT;\">\n";
      }	  

    print STDOUT "$contenuto";
    # Trasferisce anche l'ultimo record letto.
    print STDOUT "$riga";
    print STDOUT "</listing>\n";
}    

# Inizio del programma.

# Scandisce le righe.
while ($riga = <STDIN>)
  {
    # Se si incontra un commento deve essere saltato.
    if ($riga =~ m|<!--|)
      {
	&SaltaBlocco( "-->" );
      }
    elsif ($riga =~ m|<command|)
      {
	$dopo_un_comando = 1;
        print STDOUT ("$riga");
      }
    elsif ($riga =~ m|<workinfo|)
      {
	&SaltaBlocco( "</workinfo" );
      }
    elsif ($riga =~ m|<listing[^r]|)
      {
	&SaltaBlocco( "</listing" );
      }
    elsif ($riga =~ m|<figure[^r]|)
      {
	&SaltaBlocco( "</figure" );
      }
    elsif ($riga =~ m|<table[^r]|)
      {
	&SaltaBlocco( "</table" );
      }
    elsif ($riga =~ m|^<verbatimpre>$|)
      {
	&ElaboraVerbatimpre ();
	$dopo_un_comando = 0;
      }
    elsif ($riga =~ m|^\s*$|)
      {
        print STDOUT ("$riga");
      }
    else
      {
        $dopo_un_comando = 0;
        print STDOUT ("$riga");
      }
  }

#======================================================================

