#!/usr/bin/perl
##
## Copyright (C) 2000-2005 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.
##
##
##
## alml-extra OPTION ARGUMENT
##
## Simple extra program for Alml.
##
#
use POSIX;
use Locale::gettext;
setlocale (LC_MESSAGES, "");
textdomain ("alml");

#sub gettext
#{
#    return $_[0];
#}
#
# Program version.
#
$VERSION = "2005.01.01";
#
# Program name.
#
$program_canonical_name = "alml-extra";

#-----------------------------------------------------------------------
# Program executable without path.
#-----------------------------------------------------------------------

$program_executable = $0;
$program_executable =~ m{([^/]*)$};
$program_executable = $1;

#-----------------------------------------------------------------------
# &help_syntax ()
#-----------------------------------------------------------------------

sub help_syntax
{
    printf STDOUT (gettext ("\
Usage: %s OPTION ARGUMENT\
       %s --help\
       %s --version\
       %s --html-index=DIR\
       %s --html-index-basic=DIR\
       %s --html-index-basic-recursive\
       %s --html-to-text-for-spell\
       %s --perl-to-gettext\
       %s --dos2unix\
       %s --unix2dos\
\
Simple extra program for Alml.\
\
Standard options:\
--help        display this help and exit.\
--version     display version information and exit.\
\
PostScript format conversion options:\
    these options are complex and can be interpreted in a uniform\
    fashion:\
\
    --PAPER_FORMAT-to-PAPER_FORMAT-TIMES-PAPER_FORMAT-FOLD-SIGNATURE\
\
    For example, --a4-to-a5-2-a4-1h-4 means:\
\
    original A4 converted to A5, putting two A5 pages on a single\
    final A4, that is to be folded once, horizontally, for a final\
    binding signature with four sheets.\
\
    Available conversion options:\
    --a4-to-a5\
    --a4-to-a5-1-a4\
    --a4-to-a5-2-a4\
    --a4-to-a6\
    --a4-to-a6-4-a4\
    --a4-to-a5-2-a4-1h-1\
    --a4-to-a5-2-a4-1h-10\
    --a4-to-a6-4-a4-2h-2\
    --a4-to-a6-4-a4-2h-4\
    --a4-to-a6-4-a4-2h-6\
    --a4-to-a6-4-a4-2h-8\
    --a4-to-a6-4-a4-2h-10\
    --a4-to-a6-4-a4-1v-1\
    --a5x4-to-a7x4\
    --a5x4-to-a7x4-2-a4\
    --a7x4-to-a7x4-2-a4\
    --a7x4-to-a7x4-2-a4-1v-1\
    --a7x4-to-a7x4-2-a4-1v-10\
\
Other PostScript conversion options:\
--ps-group-pages=N	renumber PostScript pages to show group of pages\
			made of N pages.\
--ps-renumber-pages	renumber PostScript pages starting from number one.\
\
--alml-ps-split-tome=FILE\
                        split the PostScript file into single tomes.\
			The FILE contains the splitting informations.\
--alml-ps-split-part=FILE\
                        split the PostScript file into single parts.\
			The FILE contains the splitting informations.\
\
DVI format conversion options:\
\
--alml-dvi-split-tome=FILE\
                        split the DVI file into single tomes.\
			The FILE contains the splitting informations.\
--alml-dvi-split-part=FILE\
                        split the DVI file into single parts.\
			The FILE contains the splitting informations.\
\
DVI to PDF format conversion options:\
\
--alml-dvi-to-pdf-split-tome=FILE\
                        split the DVI file into PDF single tomes.\
			The FILE contains the splitting informations.\
--alml-dvi-to-pdf-split-part=FILE\
                        split the DVI file into PDF single parts.\
			The FILE contains the splitting informations.\
\
Different purpose options:\
\
--html-index=DIR	    create an HTML page on standard output, with a\
                              simple list of files.\
--html-index-basic=DIR	    create an HTML page on standard output, with a\
                              simpler list of files.\
--html-index-basic-recursive\
                            create an \"index.html\" file inside current\
			      directory and every subdirectory.\
--html-to-text-for-spell    filter an HTML file giving a pure text that\
			      can be used for orthography verification.\
--perl-to-gettext           filter a Perl source for gettext.\
--dos2unix                  filter STDIN to STDOUT to get common newline\
                              code for Unix systems.\
--dos2unix                  filter STDIN to STDOUT to get common newline\
                              code for Dos systems.\
\
Final argument:\
The final argument might be a PostScript file, a DVI file, an HTML file,\
depending on the context.\
\
Report bugs to <daniele\@swlibero.org>"),
    $program_executable,
    $program_executable,
    $program_executable,
    $program_executable,
    $program_executable,
    $program_executable,
    $program_executable,
    $program_executable);
}

#-----------------------------------------------------------------------
# Show version information.
#
# &version_info ()
#-----------------------------------------------------------------------

sub version_info
{
    printf STDOUT (gettext ("\
%s %s\
\
Copyright (C) 2000-2005 Daniele giacomini <daniele\@swlibero.org>\
This is free software; see the source for copying conditions.\
There is NO warranty; not even for MERCHANTABILITY or FITNESS\
FOR A PARTICULAR PURPOSE."),
	$program_canonical_name,
	$VERSION);
}
#
# Define a temporary file.
#
# &temporary_file ( <prefix> )
#
sub temporary_file
{
    my ($file_prefix) = $_[0];
    my ($temp_dir) = "";
    my ($random_number) = 0;
    my ($random_file_name) = "";
    #
    # Check file prefix.
    #
    if ( $file_prefix ne "" )
      {
        $file_prefix = $file_prefix . "_";
      }
    #
    # Check for temp dir.
    #
    if (-d $ENV{TEMPDIR}
        && -r $ENV{TEMPDIR}
        && -w $ENV{TEMPDIR}
        && -x $ENV{TEMPDIR})
      {
        #
        # This directory is good.
        #
        $temp_dir = $ENV{TEMPDIR};
      }
    elsif (-d "/tmp" && -r "/tmp" && -w "/tmp" && -x "/tmp")
      {
        #
        # This other directory is good.
        #
        $temp_dir = "/tmp";
      }
    elsif (-r "." && -w "." && -x ".")
      {
        #
        # Current directory is good.
        #
        $temp_dir = ".";
      }
    else
      {
        #
        # We cannot use any temporary file!
        #
        printf STDERR (gettext ("%s: cannot create any temporary file!\n"),
                       $program_executable);
        exit 1;
      }
    #
    # If we are here, we have a valid temporary directory.
    # We try to find a good name for the file.
    #
    while (1)
      {
        #
        # Define the random number (six digits).
        #
        $random_number = int ((rand) * 1000000);
        #
        # Define the random file name: TF...tmp.
        #
        $random_file_name = "$temp_dir/$file_prefix" . "TF${random_number}tmp";
        #
        # Check if it is new.
        #
        if (-e $random_file_name)
          {
	    #
            # The file exists already.
	    #
            next;
          }
        else
          {
            if (open (TEMP_FILE, "> $random_file_name"))
              {

	        #-------------------------------------------------------
                # It works.
	        #-------------------------------------------------------

                close( TEMP_FILE );
                last;
              }
            else
              {

	        #-------------------------------------------------------
                # Don't know what to do.
	        #-------------------------------------------------------

                printf STDERR
                  (gettext ("%s: cannot create the temporary file %s\n"),
                   $program_executable, $random_file_name);
                next;
              }

	    #-----------------------------------------------------------
            # This point cannot be reached.
	    #-----------------------------------------------------------

            printf STDERR
              (gettext ("%s: function %s unknown error 1\n"),
               $program_executable, "&temporary_file($file_prefix)");
          }

	#---------------------------------------------------------------
        # This point cannot be reached.
	#---------------------------------------------------------------

        printf STDERR
          (gettext ("%s: function %s unknown error 2\n"),
           $program_executable, "&temporary_file($file_prefix)");
      }         

    #-------------------------------------------------------------------
    # Return the file name.
    #-------------------------------------------------------------------

    return ("$random_file_name");
}


#-----------------------------------------------------------------------
# Determinate the file name without extention.
#
# &root_name (FILE_NAME, EXTENTION)
#-----------------------------------------------------------------------

sub root_name
{
    my ($file_name) = $_[0];
    my ($extention_name) = $_[1];
    my ($root_name) = "";
    #
    if ($file_name =~ m/^(.*)$extention_name$/)
      {
	$root_name = $1;
      }
    #
    if ($root_name eq "")
      {
	#
        # The extention is not the same.
	#
        $root_name = $file_name;
      }
    #
    return ($root_name);
    #
} # &root_name
#
# Group PostScript pages for binding.
#
# &ps_group_pages (FILE_NAME, PAGES)
#
sub ps_group_pages
{
    my ($file_name)   = $_[0];
    my ($group_pages) = $_[1];
    my ($original_file_name) = $file_name . "~";
    my ($n) = 0;

    system ("mv $file_name $original_file_name");
    open (ORIGINAL, "< $original_file_name");
    open (NEW, "> $file_name");

    $n=1;
    while ($line = <ORIGINAL>)
      {
	if ($line =~ m/^\%\%Page:\s+(\S+)\s+(\S+)/)
	  {
	    $line = "%%Page: ($1-$n) $2\n";
	    $n++;
	    if ($n > $group_pages)
	      {
	        $n = 1;
	      }
	  }
	print NEW ($line);
      }

    close (NEW);
    close (ORIGINAL);
}

#-----------------------------------------------------------------------
# Put an index on a directory.
#
# &html_index (DIRECTORY)
#
#-----------------------------------------------------------------------
sub html_index
{
    my ($directory) = $_[0];
    my ($index) = "";
    my ($line) = "";
    my ($type) = "";
    my ($inode) = "";
    my ($user) = "";
    my ($group) = "";
    my ($size) = "";
    my ($week) = "";
    my ($month) = "";
    my ($day) = "";
    my ($hour) = "";
    my ($year) = "";
    my ($name) = "";
    my ($true_name) = "";

    open (DIRECTORY_LISTING, "export LC_ALL=C ; ls -l --full-time --si $directory |");

    $index = "\
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\
    \"http://www.w3.org/TR/html4/strict.dtd\">\
<HTML LANG=\"en\">\
<HEAD>\
<TITLE>$directory/</TITLE>\
</HEAD>\
<BODY>\
<H1>$directory/</H1>\
<TABLE>\
<TBODY>\
<TR><TD>previous</TD><td><A HREF=\"..\">..</A></TD><TD></TD><TD></TD><TD></TD><TD></TD></TR>\
";

    while ($line = <DIRECTORY_LISTING>)
      {
        if ($line =~ m/^(\S)\S+\s+\S+\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+\S+\s+\S+\s+(.+)$/)
	  {
	    $type	= $1;
	    $user	= $2;
	    $group	= $3;
	    $size	= $4;
	    $day	= $5;
	    $name	= $6;

	    if ($type eq "d")
	      {
	        $index = $index . "<TR><TD>directory</TD><TD><A HREF=\"$name\">$name/</A></TD><TD></TD><TD>$month</TD><TD>$day</TD><TD>$year</TD></TR>\n";
	      }
	    elsif ($type eq "l")
	      {
	        $name =~ m/^(\S+)\s+\-\>\s+(\S+)$/;
		$true_name = $2;
	        $index = $index . "<TR><TD>link</TD><TD><A HREF=\"$true_name\">$name</A></TD><TD></TD><TD>$month</TD><TD>$day</TD><TD>$year</TD></TR>\n";
	      }
	    else
	      {
	        $index = $index . "<TR><TD>file</TD><TD><A HREF=\"$name\">$name</A></TD><TD>$size</TD><TD>$month</TD><TD>$day</TD><TD>$year</TD></TR>\n";
	      }
	  }
      }

    $index = $index . "
</TBODY>\
</TABLE>\
</BODY>\
</HTML>\
";
    close (DIRECTORY_LISTING);

    return ($index);
}

#-----------------------------------------------------------------------
# Put a simple index on a directory.
#
# &html_index_basic (DIRECTORY)
#
#-----------------------------------------------------------------------

sub html_index_basic
{
    my ($directory) = $_[0];
    my ($index) = "";
    my ($line) = "";
    my ($type) = "";
    my ($inode) = "";
    my ($user) = "";
    my ($group) = "";
    my ($size) = "";
    my ($week) = "";
    my ($month) = "";
    my ($day) = "";
    my ($hour) = "";
    my ($year) = "";
    my ($name) = "";
    my ($true_name) = "";

    open (DIRECTORY_LISTING, "export LC_ALL=C ; ls -l --full-time --si $directory |");

    $index = "\
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\
    \"http://www.w3.org/TR/html4/strict.dtd\">\
<HTML LANG=\"en\">\
<HEAD>\
<TITLE>$directory/</TITLE>\
</HEAD>\
<BODY>\
<H1>$directory/</H1>\
<TABLE>\
<TBODY>\
<TR><TD>previous</TD><td><A HREF=\"..\">..</A></TD><TD></TD><TD></TD><TD></TD><TD></TD></TR>\
";
    while ($line = <DIRECTORY_LISTING>)
      {
        if ($line =~ m/^(\S)\S+\s+\S+\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+\S+\s+\S+\s+(.+)$/)
	  {
	    $type	= $1;
	    $user	= $2;
	    $group	= $3;
	    $size	= $4;
	    $day	= $5;
	    $name	= $6;

	    if ($type eq "d")
	      {
	        $index = $index . "<TR><TD>directory</TD><TD><A HREF=\"$name\">$name/</A></TD><TD></TD><TD></TD><TD></TD><TD></TD></TR>\n";
	      }
	    elsif ($type eq "l")
	      {
	        $name =~ m/^(\S+)\s+\-\>\s+(\S+)$/;
		$true_name = $2;
	        $index = $index . "<TR><TD>link</TD><TD><A HREF=\"$true_name\">$name</A></TD><TD></TD><TD><TD></TD><TD></TD></TR>\n";
	      }
	    else
	      {
	        $index = $index . "<TR><TD>file</TD><TD><A HREF=\"$name\">$name</A></TD><TD>$size</TD><TD></TD><TD></TD><TD></TD></TR>\n";
	      }
	  }
      }

    $index = $index . "
</TBODY>\
</TABLE>\
</BODY>\
</HTML>\
";
    close (DIRECTORY_LISTING);

    return ($index);
}
#
# Put an index on any subdirectory
#
# &html_index_basic_recursive (DIRECTORY)
#
#
sub html_index_basic_recursive
{
    my ($directory) = $_[0];
    my ($ls) = $directory;
    #
    opendir ($ls, "$directory");
    #
    while ($file = readdir ($ls))
      {
	if (-l "$directory/$file")
	  {
	    #
	    # Ignore this link.
	    #
	    ;
	  }
	elsif (-d "$directory/$file")
	  {
	    if ($file !~ m/^\./)
	      {
    		&html_index_basic_recursive ("$directory/$file");
	      }
	  }
      }    
    closedir ($ls);
    #
    open (HTMLINDEX, "> $directory/index.html");
    print HTMLINDEX (&html_index_basic ($directory));
    close (HTMLINDEX);
    #    
} # &index_html
#
# Renumber PostScript pages.
#
# &ps_renumber_pages (FILE_NAME)
#
sub ps_renumber_pages
{
    my ($file_name)          = $_[0];
    my ($group_pages)        = $_[1];
    my ($original_file_name) = $file_name . "~";
    my ($n) = 0;

    system ("mv $file_name $original_file_name");
    open (ORIGINAL, "< $original_file_name");
    open (NEW, "> $file_name");

    $n=1;
    while ($line = <ORIGINAL>)
      {
	if ($line =~ m/^\%\%Page:\s+(\S+)\s+(\S+)/)
	  {
	    $line = "%%Page: $2 $2\n";
	    $n++;
	    if ($n > $group_pages)
	      {
	        $n = 1;
	      }
	  }
	print NEW ($line);
      }

    close (NEW);
    close (ORIGINAL);
}
#
# Change wrong bounding box inside PostScript file.
#
# &ps_new_bounding_box (FILE-NAME, NEW_BOUNDING_BOX)
#
sub ps_new_bounding_box
{
    my ($file_name)          = $_[0];
    my ($new_bounding_box)   = $_[1];
    my ($original_file_name) = $file_name . "~";
    #
    system ("mv $file_name $original_file_name");
    open (ORIGINAL, "< $original_file_name");
    open (NEW, "> $file_name");
    #
    while ($line = <ORIGINAL>)
      {
	if ($line =~ m/^\%\%BoundingBox:\s/
	    || $line =~ m/^\%\%DocumentPageSizes:\s/)
	  {
	    $line = "$new_bounding_box\n";
	  }
	print NEW ($line);
      }
    #
    close (NEW);
    close (ORIGINAL);
}
#
# Add an empty page at the beguinning.
#
# &ps_start_with_emtpy_page (FILE_NAME, NEW_FILE_NAME)
#
sub ps_start_with_empty_page
{
    my ($file_name)          = $_[0];
    my ($new_file_name)      = $_[1];
    my ($n)                  = 0;
    my ($m)                  = 0;
    #
    open (ORIGINAL, "< $file_name");
    open (NEW, "> $new_file_name");
    #
    while ($line = <ORIGINAL>)
      {
	if ($line =~ m/^\%\%Pages:\s+([0-9]+)/)
	  {
	    $m = $1;
	    $m++;
	    $line = "%%Pages: $m\n";
	  }
	elsif ($line =~ m/^\%\%Page:\s+([0-9]+)\s+1\s*$/)
	  {
	    $m = $1;
	    #
	    # Add the empy page.
	    #
	    print NEW ("%%Page: 0 1\n");
	    print NEW ("save\n");
	    print NEW ("    72 72 moveto\n");
	    print NEW ("    ()\n");
	    print NEW ("    show\n");
	    print NEW ("stroke\n");
	    print NEW ("restore\n");
	    print NEW ("showpage\n");
	    #
	    $line = "%%Page: $m 2\n"
	  }
	elsif ($line =~ m/^\%\%Page:\s+([0-9]+)\s+([0-9]+)/)
	  {
	    $m = $1;
	    $n = $2;
	    $n++;
	    $line = "%%Page: $m $n\n";
	  }
	print NEW ($line);
      }
    #
    close (NEW);
    close (ORIGINAL);
}
#
# A4 to A5 once, inside a final A5 portrait.
#
sub a4_to_a5
{
    my ($file_name)      = $_[0];
    my ($root_file_name) = &root_name ($file_name, ".ps");
    print STDOUT "$file_name --> $root_file_name.a5.ps\n";
    system ("cat $file_name | pstops -q \"1:0\@0.7(0,0)\" > $root_file_name.a5.ps");
}

#-----------------------------------------------------------------------
# A4 to A5 once, inside a final A4 portrait.
#-----------------------------------------------------------------------

sub a4_to_a5_1_a4
{
    my ($file_name)      = $_[0];
    my ($root_file_name) = &root_name ($file_name, ".ps");
    print STDOUT "$file_name --> $root_file_name.a4-to-a5-1-a4.ps\n";
    system ("cat $file_name | pstops -q \"1:0\@0.7(3.15cm,8.91cm)\" > $root_file_name.a4-to-a5-1-a4.ps");
}

#-----------------------------------------------------------------------
# A4 to A6 once, inside a final A6 portrait.
#-----------------------------------------------------------------------

sub a4_to_a6
{
    my ($file_name)      = $_[0];
    my ($root_file_name) = &root_name ($file_name, ".ps");
    print STDOUT "$file_name --> $root_file_name.a6.ps\n";
    system ("cat $file_name | pstops -q \"1:0\@0.45(0,0)\" > $root_file_name.a6.ps");
}


#-----------------------------------------------------------------------
# A4 to A5 two times inside a final A4 landscape (no folding).
#-----------------------------------------------------------------------

sub a4_to_a5_2_a4
{
    my ($file_name)      = $_[0];
    my ($root_file_name) = &root_name ($file_name, ".ps");
    print STDOUT "$file_name --> $root_file_name.a5-2-a4.ps\n";
    system ("cat $file_name | psnup -q -2 > $root_file_name.a5-2-a4.ps");
}

#-----------------------------------------------------------------------
# A4 to A6 four times inside a final A4 portrait (no folding).
#-----------------------------------------------------------------------

sub a4_to_a6_4_a4
{
    my ($file_name)      = $_[0];
    my ($root_file_name) = &root_name ($file_name, ".ps");
    print STDOUT "$file_name --> $root_file_name.a6-4-a4.ps \n";
    system ("cat $file_name | psnup -q -4 > $root_file_name.a6-4-a4.ps");
}

#-----------------------------------------------------------------------
# A4 to A5 two times inside a final A4, folded horizontally once,
# to be binded with a signature of 1.
#-----------------------------------------------------------------------

sub a4_to_a5_2_a4_1h_1
{
    my ($file_name)      = $_[0];
    my ($root_file_name) = &root_name ($file_name, ".ps");
    print STDOUT "$file_name --> $root_file_name.a5-2-a4-1h-1.ps \n";
    system ("cat $file_name		\\
             | psbook -q -s4		\\
	     | pstops -q \"4:0L\@0.7(21cm,0)+1L\@0.7(21cm,14.85cm),2R\@0.7(0,29.75cm)+3R\@0.7(0,14.85cm)\" \\
	     > $root_file_name.a5-2-a4-1h-1.ps");
    &ps_group_pages ("$root_file_name.a5-2-a4-1h-1.ps", 2);
}

#-----------------------------------------------------------------------
# A4 to A5 two times inside a final A4, folded horizontally once,
# to be binded with a signature of 10.
#-----------------------------------------------------------------------

sub a4_to_a5_2_a4_1h_10
{
    my ($file_name)      = $_[0];
    my ($root_file_name) = &root_name ($file_name, ".ps");
    print STDOUT "$file_name --> $root_file_name.a5-2-a4-1h-10.ps\n";
    system ("cat $file_name		\\
	     | psbook -q -s40		\\
	     | pstops -q \"4:0L\@0.7(21cm,0)+1L\@0.7(21cm,14.85cm),2R\@0.7(0,29.75cm)+3R\@0.7(0,14.85cm)\"	\\
	     > $root_file_name.a5-2-a4-1h-10.ps");
    &ps_group_pages ("$root_file_name.a5-2-a4-1h-10.ps", 20);
}

#-----------------------------------------------------------------------
# A4 to A6 four times inside a final A4, folded horizontally twice,
# to be binded with a signature of 2.
#-----------------------------------------------------------------------

sub a4_to_a6_4_a4_2h_2
{
    my ($file_name)      = $_[0];
    my ($root_file_name) = &root_name ($file_name, ".ps");
    print STDOUT "$file_name --> $root_file_name.a6-4-a4-2h-2.ps \n";
    system ("cat $file_name		\\
	     | pstops -q \"8:\\
4U\@0.45(10cm,29.2cm)+\\
3U\@0.45(20.5cm,29.2cm)+\\
7\@0.45(0.5cm,0.5cm)+\\
0\@0.45(11cm,0.5cm),\\
2U\@0.45(10cm,29.2cm)+\\
5U\@0.45(20.5cm,29.2cm)+\\
1\@0.45(0.5cm,0.5cm)+\\
6\@0.45(11cm,0.5cm)\"\\
	     > $root_file_name.a6-4-a4-2h-2.ps");
    &ps_group_pages ("$root_file_name.a6-4-a4-2h-2.ps", 2);
}

#-----------------------------------------------------------------------
# A4 to A6 four times inside a final A4, folded horizontally twice,
# to be binded with a signature of 4.
#-----------------------------------------------------------------------

sub a4_to_a6_4_a4_2h_4
{
    my ($file_name)      = $_[0];
    my ($root_file_name) = &root_name ($file_name, ".ps");
    print STDOUT "$file_name --> $root_file_name.a4_to_a6_4_a4_2h_4.ps \n";
    system ("cat $file_name		\\
	     | pstops -q \"16:\\
8U\@0.45(10cm,29.2cm)+\\
7U\@0.45(20.5cm,29.2cm)+\\
15\@0.45(0.5cm,0.5cm)+\\
0\@0.45(11cm,0.5cm),\\
6U\@0.45(10cm,29.2cm)+\\
9U\@0.45(20.5cm,29.2cm)+\\
1\@0.45(0.5cm,0.5cm)+\\
14\@0.45(11cm,0.5cm),\\
10U\@0.45(10cm,29.2cm)+\\
5U\@0.45(20.5cm,29.2cm)+\\
13\@0.45(0.5cm,0.5cm)+\\
2\@0.45(11cm,0.5cm),\\
4U\@0.45(10cm,29.2cm)+\\
11U\@0.45(20.5cm,29.2cm)+\\
3\@0.45(0.5cm,0.5cm)+\\
12\@0.45(11cm,0.5cm)\"\\
	     > $root_file_name.a4_to_a6_4_a4_2h_4.ps");
    &ps_group_pages ("$root_file_name.a4_to_a6_4_a4_2h_4.ps", 4);
}

#-----------------------------------------------------------------------
# A4 to A6 four times inside a final A4, folded horizontally twice,
# to be binded with a signature of 6.
#-----------------------------------------------------------------------

sub a4_to_a6_4_a4_2h_6
{
    my ($file_name)      = $_[0];
    my ($root_file_name) = &root_name ($file_name, ".ps");
    print STDOUT "$file_name --> $root_file_name.a6-4-a4-2h-6.ps \n";
    system ("cat $file_name		\\
	     | pstops -q \"24:\\
12U\@0.45(10cm,29.2cm)+\\
11U\@0.45(20.5cm,29.2cm)+\\
23\@0.45(0.5cm,0.5cm)+\\
0\@0.45(11cm,0.5cm),\\
10U\@0.45(10cm,29.2cm)+\\
13U\@0.45(20.5cm,29.2cm)+\\
1\@0.45(0.5cm,0.5cm)+\\
22\@0.45(11cm,0.5cm),\\
14U\@0.45(10cm,29.2cm)+\\
9U\@0.45(20.5cm,29.2cm)+\\
21\@0.45(0.5cm,0.5cm)+\\
2\@0.45(11cm,0.5cm),\\
8U\@0.45(10cm,29.2cm)+\\
15U\@0.45(20.5cm,29.2cm)+\\
3\@0.45(0.5cm,0.5cm)+\\
20\@0.45(11cm,0.5cm),\\
16U\@0.45(10cm,29.2cm)+\\
7U\@0.45(20.5cm,29.2cm)+\\
19\@0.45(0.5cm,0.5cm)+\\
4\@0.45(11cm,0.5cm),\\
6U\@0.45(10cm,29.2cm)+\\
17U\@0.45(20.5cm,29.2cm)+\\
5\@0.45(0.5cm,0.5cm)+\\
18\@0.45(11cm,0.5cm)\"\\
	     > $root_file_name.a6-4-a4-2h-6.ps");
    &ps_group_pages ("$root_file_name.a6-4-a4-2h-6.ps", 6);
}

#-----------------------------------------------------------------------
# A4 to A6 four times inside a final A4, folded horizontally twice,
# to be binded with a signature of 8.
#-----------------------------------------------------------------------

sub a4_to_a6_4_a4_2h_8
{
    my ($file_name)      = $_[0];
    my ($root_file_name) = &root_name ($file_name, ".ps");
    print STDOUT "$file_name --> $root_file_name.a6-4-a4-2h-8.ps \n";
    system ("cat $file_name		\\
	     | pstops -q \"32:\\
16U\@0.45(10cm,29.2cm)+\\
15U\@0.45(20.5cm,29.2cm)+\\
31\@0.45(0.5cm,0.5cm)+\\
0\@0.45(11cm,0.5cm),\\
14U\@0.45(10cm,29.2cm)+\\
17U\@0.45(20.5cm,29.2cm)+\\
1\@0.45(0.5cm,0.5cm)+\\
30\@0.45(11cm,0.5cm),\\
18U\@0.45(10cm,29.2cm)+\\
13U\@0.45(20.5cm,29.2cm)+\\
29\@0.45(0.5cm,0.5cm)+\\
2\@0.45(11cm,0.5cm),\\
12U\@0.45(10cm,29.2cm)+\\
19U\@0.45(20.5cm,29.2cm)+\\
3\@0.45(0.5cm,0.5cm)+\\
28\@0.45(11cm,0.5cm),\\
20U\@0.45(10cm,29.2cm)+\\
11U\@0.45(20.5cm,29.2cm)+\\
27\@0.45(0.5cm,0.5cm)+\\
4\@0.45(11cm,0.5cm),\\
10U\@0.45(10cm,29.2cm)+\\
21U\@0.45(20.5cm,29.2cm)+\\
5\@0.45(0.5cm,0.5cm)+\\
26\@0.45(11cm,0.5cm),\\
22U\@0.45(10cm,29.2cm)+\\
9U\@0.45(20.5cm,29.2cm)+\\
25\@0.45(0.5cm,0.5cm)+\\
6\@0.45(11cm,0.5cm),\\
8U\@0.45(10cm,29.2cm)+\\
23U\@0.45(20.5cm,29.2cm)+\\
7\@0.45(0.5cm,0.5cm)+\\
24\@0.45(11cm,0.5cm)\"\\
	     > $root_file_name.a6-4-a4-2h-8.ps");
    &ps_group_pages ("$root_file_name.a6-4-a4-2h-8.ps", 8);
}

#-----------------------------------------------------------------------
# A4 to A6 four times inside a final A4, folded horizontally twice,
# to be binded with a signature of 10.
#-----------------------------------------------------------------------

sub a4_to_a6_4_a4_2h_10
{
    my ($file_name)      = $_[0];
    my ($root_file_name) = &root_name ($file_name, ".ps");
    print STDOUT "$file_name --> $root_file_name.a6-4-a4-2h-10.ps \n";
    system ("cat $file_name		\\
	     | pstops -q \"40:\\
20U\@0.45(10cm,29.2cm)+\\
19U\@0.45(20.5cm,29.2cm)+\\
39\@0.45(0.5cm,0.5cm)+\\
0\@0.45(11cm,0.5cm),\\
18U\@0.45(10cm,29.2cm)+\\
21U\@0.45(20.5cm,29.2cm)+\\
1\@0.45(0.5cm,0.5cm)+\\
38\@0.45(11cm,0.5cm),\\
22U\@0.45(10cm,29.2cm)+\\
17U\@0.45(20.5cm,29.2cm)+\\
37\@0.45(0.5cm,0.5cm)+\\
2\@0.45(11cm,0.5cm),\\
16U\@0.45(10cm,29.2cm)+\\
23U\@0.45(20.5cm,29.2cm)+\\
3\@0.45(0.5cm,0.5cm)+\\
36\@0.45(11cm,0.5cm),\\
24U\@0.45(10cm,29.2cm)+\\
15U\@0.45(20.5cm,29.2cm)+\\
35\@0.45(0.5cm,0.5cm)+\\
4\@0.45(11cm,0.5cm),\\
14U\@0.45(10cm,29.2cm)+\\
25U\@0.45(20.5cm,29.2cm)+\\
5\@0.45(0.5cm,0.5cm)+\\
34\@0.45(11cm,0.5cm),\\
26U\@0.45(10cm,29.2cm)+\\
13U\@0.45(20.5cm,29.2cm)+\\
33\@0.45(0.5cm,0.5cm)+\\
6\@0.45(11cm,0.5cm),\\
12U\@0.45(10cm,29.2cm)+\\
27U\@0.45(20.5cm,29.2cm)+\\
7\@0.45(0.5cm,0.5cm)+\\
32\@0.45(11cm,0.5cm),\\
28U\@0.45(10cm,29.2cm)+\\
11U\@0.45(20.5cm,29.2cm)+\\
31\@0.45(0.5cm,0.5cm)+\\
8\@0.45(11cm,0.5cm),\\
10U\@0.45(10cm,29.2cm)+\\
29U\@0.45(20.5cm,29.2cm)+\\
9\@0.45(0.5cm,0.5cm)+\\
30\@0.45(11cm,0.5cm)\"\\
	     > $root_file_name.a6-4-a4-2h-10.ps");
    &ps_group_pages ("$root_file_name.a6-4-a4-2h-10.ps", 10);
}

#-----------------------------------------------------------------------
# A4 to A6 four times inside a final A4, folded vertically once,
# to be binded with a signature of 1.
#-----------------------------------------------------------------------

sub a4_to_a6_4_a4_1v_1
{
    my ($file_name)      = $_[0];
    my ($root_file_name) = &root_name ($file_name, ".ps");
    print STDOUT "$file_name --> $root_file_name.a6-4-a4-1v-1.ps \n";
    system ("cat $file_name		\\
	     | pstops -q \"8:\\
6\@0.45(0cm,14.35cm)+\\
0\@0.45(11cm,14.35cm)+\\
7\@0.45(0cm,0.5cm)+\\
1\@0.45(11cm,0.5cm),\\
2\@0.45(0cm,14.35cm)+\\
4\@0.45(11cm,14.35cm)+\\
3\@0.45(0cm,0.5cm)+\\
5\@0.45(11cm,0.5cm)\"\\
	     > $root_file_name.a6-4-a4-1v-1.ps");
    &ps_group_pages ("$root_file_name.a6-4-a4-1v-1.ps", 2);
}


#-----------------------------------------------------------------------
# A5x4 to A7x4 two times inside a final A4.
#-----------------------------------------------------------------------

sub a5x4_to_a7x4
{
    my ($file_name)      = $_[0];
    my ($root_file_name) = &root_name ($file_name, ".ps");

    #-------------------------------------------------------------------
    # For compatibility with "Appunti di informatica libera",
    # if there is the string ".X1T." it is deleted from the name.
    #-------------------------------------------------------------------

    if ($root_file_name =~ m/^(.*?)\.X1T(.*?)$/)
      {
	$root_file_name = $1 . $2;
      }

    print STDOUT "$file_name --> $root_file_name.a7x4.ps \n";
    system ("cat $file_name                    			\\
	     | psresize -w 10.5cm -h 29.7cm 			\\
             | pstops -w 10.5cm -h 29.7cm                     	\\
             -q \"1:0\@1(0cm,-7.67cm)\"    \\
             > $root_file_name.a7x4.ps");
    &ps_new_bounding_box ("$root_file_name.a7x4.ps", "%%BoundingBox: 0 0 298 842");
}
#
# A5x4 to A7x4 two times inside a final A4.
#
sub a5x4_to_a7x4_2_a4
{
    my ($file_name)      = $_[0];
    my ($root_file_name) = &root_name ($file_name, ".ps");
    #
    # For compatibility with "Appunti di informatica libera",
    # if there is the string ".X1T." it is deleted from the name.
    #
    if ($root_file_name =~ m/^(.*?)\.X1T(.*?)$/)
      {
	$root_file_name = $1 . $2;
      }
    #
    print STDOUT "$file_name --> $root_file_name.a7x4-2-a4.ps \n";
    &ps_start_with_empty_page ("$file_name", "${file_name}~" );
    system ("cat ${file_name}~                			\\
	     | psresize -w 10.5cm -h 29.7cm 			\\
             | pstops -w 10.5cm -h 29.7cm                     	\\
             -q \"2:0\@1(0cm,-7.67cm)+1\@1(10.5cm,-7.67cm)\"    \\
             > $root_file_name.a7x4-2-a4.ps");
    &ps_new_bounding_box ("$root_file_name.a7x4-2-a4.ps", "%%BoundingBox: 0 0 596 842");
}
#
# A7x4 to A7x4 two times inside a final A4.
#
sub a7x4_to_a7x4_2_a4
{
    my ($file_name)      = $_[0];
    my ($root_file_name) = &root_name ($file_name, ".ps");

    #-------------------------------------------------------------------
    # For compatibility with "Appunti di informatica libera",
    # if there is the string ".X1T." it is deleted from the name.
    #-------------------------------------------------------------------

    if ($root_file_name =~ m/^(.*?)\.X1T(.*?)$/)
      {
	$root_file_name = $1 . $2;
      }

    print STDOUT "$file_name --> $root_file_name.a7x4-2-a4.ps \n";
    system ("cat $file_name                    \\
             | pstops -w 10.5cm -h 29.7cm                     \\
             -q \"2:0\@1(0cm,-0.25cm)+1\@1(10.5cm,-0.25cm)\"     \\
             > $root_file_name.a7x4-2-a4.ps");
    &ps_new_bounding_box ("$root_file_name.a7x4-2-a4.ps", "%%BoundingBox: 0 0 596 842");
}

#-----------------------------------------------------------------------
# A4 "long" to A6 "long" two times inside a final A4, folded vertically
# once, to be binded with a signature of 1.
#-----------------------------------------------------------------------

sub a7x4_to_a7x4_2_a4_1v_1
{
    my ($file_name)      = $_[0];
    my ($root_file_name) = &root_name ($file_name, ".ps");

    #-------------------------------------------------------------------
    # For compatibility with "Appunti di informatica libera",
    # if there is the string ".X1T." it is deleted from the name.
    #-------------------------------------------------------------------

    if ($root_file_name =~ m/^(.*?)\.X1T(.*?)$/)
      {
	$root_file_name = $1 . $2;
      }

    print STDOUT "$file_name --> $root_file_name.a7x4-2-a4-1v-1.ps \n";
    system ("cat $file_name                    \\
             | pstops -w 10.5cm -h 29.7cm                     \\
             -q \"4:0\@1(10.5cm,-0.25cm)+3\@1(0cm,-0.25cm),2\@1(10.5cm,-0.25cm)+1\@1(0cm,-0.25cm)\"     \\
             > $root_file_name.a7x4-2-a4-1v-1.ps");
    &ps_group_pages ("$root_file_name.a7x4-2-a4-1v-1.ps", 2);
    &ps_new_bounding_box ("$root_file_name.a7x4-2-a4-1v-1.ps", "%%BoundingBox: 0 0 596 842");
}

#-----------------------------------------------------------------------
# A4 "long" to A6 "long" two times inside a final A4, folded vertically
# once, to be binded with a signature of 10.
#-----------------------------------------------------------------------

sub a7x4_to_a7x4_2_a4_1v_10
{
    my ($file_name)      = $_[0];
    my ($root_file_name) = &root_name ($file_name, ".ps");

    #-------------------------------------------------------------------
    # For compatibility with "Appunti di informatica libera",
    # if there is the string ".X1T." it is deleted from the name.
    #-------------------------------------------------------------------

    if ($root_file_name =~ m/^(.*?)\.X1T(.*?)$/)
      {
	$root_file_name = $1 . $2;
      }

    print STDOUT "$file_name --> $root_file_name.a7x4-2-a4-1v-10.ps \n";
    system ("cat $file_name		\\
	     | psbook -q -s40		\\
             | pstops -w 10.5cm -h 29.7cm                     \\
             -q \"4:0\@1(0cm,-0.25cm)+1\@1(10.5cm,-0.25cm),2\@1(0cm,-0.25cm)+3\@1(10.5cm,-0.25cm)\"     \\
             > $root_file_name.a7x4-2-a4-1v-10.ps");
    &ps_group_pages ("$root_file_name.a7x4-2-a4-1v-10.ps", 20);
    &ps_new_bounding_box ("$root_file_name.a7x4-2-a4-1v-10.ps", "%%BoundingBox: 0 0 596 842");
}
#
# Split the document inside different files, at the tome or part level.
#
sub split_pages
{
    my ($source_file_name)            = $_[0];
    my ($split_type)                  = $_[1];
    my ($split_level)                 = $_[2];
    my ($split_list_file)             = $_[3];
    my ($line)                        = "";
    my ($stage)                       = "intro";
    my ($previous_split)              = 0;
    my ($current_split)               = 0;
    my ($start_page)                  = 1;
    my ($stop_page)                   = 0;
    my ($source_root_file_name)       = "";
    my (@title_list)                  = ();
    my ($title_counter)               = 0;
    my ($link_ext)                    = "";
    #
    # Split pages.
    #
    sub split_pages_now
    {
        my ($start_page)                   = $_[0];
        my ($stop_page)                    = $_[1];
        my ($source_file_name)             = $_[2];
        my ($destination_root_file_name)   = $_[3];
        my ($split_type)                   = $_[4];
	#
	if ($split_type eq "psps")
	  {
	    print STDOUT ("$start_page-$stop_page	$destination_root_file_name.ps\n");
	    system ("psselect -q -p$start_page-$stop_page $source_file_name $destination_root_file_name.ps");
	  }
	elsif ($split_type eq "dvidvi")
	  {
	    print STDOUT ("$start_page-$stop_page	$destination_root_file_name.dvi\n");
	    system ("dvidvi -i $start_page..$stop_page $source_file_name $destination_root_file_name.dvi");
	  }
	elsif ($split_type eq "dvipdf")
	  {
	    print STDOUT ("$start_page-$stop_page	$destination_root_file_name.pdf\n");
	    system ("dvipdft -s $start_page-$stop_page -o $destination_root_file_name.pdf $source_file_name");
	  }
    }
    #
    # Define the source root file name and the extention to
    # be used for symlink.
    #
    if ($split_type eq "psps")
      {
	$source_root_file_name = &root_name ($source_file_name, ".ps");
        $link_ext              = ".ps";
      }
    elsif ($split_type eq "dvidvi")
      {
	$source_root_file_name = &root_name ($source_file_name, ".dvi");
        $link_ext              = ".dvi";
      }
    elsif ($split_type eq "dvipdf")
      {
	$source_root_file_name = &root_name ($source_file_name, ".dvi");
        $link_ext              = ".pdf";
      }
    else
      {
	$source_root_file_name = $source_file_name;
        $link_ext              = "";
      }
    #
    # Open the file containing the informations about titles.
    # It is the same that has the split list.
    #
    open (TITLE_FILE, "< $split_list_file");
    #
    while ($line = <TITLE_FILE>)
      {
	#
	# The titles are saved inside @title_list in sequential order.
	# Please note that the tome or part number from the input file
	# is ignored, and the first title is at array position 0.
	#
	if    ($split_level eq "tome"
	       && $line =~ m/tometitle ([0-9]+) ([a-z0-9_]+)/i)
	  {
            $#title_list++;
            $title_list[$#title_list] = $2;
	  }
	elsif ($split_level eq "part"
	       && $line =~ m/parttitle ([0-9]+) ([a-z0-9_]+)/i)
	  {
            $#title_list++;
            $title_list[$#title_list] = $2;
	  }
      }    
    close (TITLE_FILE);
    #
    # Open (again) the file containing the informations about splitting.
    #
    open (SPLIT_FILE, "< $split_list_file");
    #
    # Scan and do the job.
    #
    $start_page    = 1;
    $title_counter = -1;
    $stage         = "intro";
    #
    while ($line = <SPLIT_FILE>)
      {
	if ($stage eq "intro")
	  {
	    if    ($split_level eq "tome"
	           && $line =~ m/tome\{([0-9]+)\}pageoffset\{([0-9]+)\}relativepage\{([0-9]+)\}/i)
	      {
	    	$current_split = $1;
		$stop_page = ($2 + $3 -1);
		&split_pages_now ($start_page, $stop_page, $source_file_name, $source_root_file_name . ".0", $split_type);
		#$title_counter++;
		#system ("ln -s ${source_root_file_name}.0$link_ext $title_list[$title_counter]$link_ext");
    		$start_page = ($stop_page + 1);
		$stage = "body";
	      }
	    elsif ($split_level eq "part"
	           && $line =~ m/part\{([0-9]+)\}pageoffset\{([0-9]+)\}relativepage\{([0-9]+)\}/i)
	      {
	    	$current_split = $1;
		$stop_page = ($2 + $3 -1);
		&split_pages_now ($start_page, $stop_page, $source_file_name, $source_root_file_name . ".0", $split_type);
		#$title_counter++;
		#system ("ln -s ${source_root_file_name}.0$link_ext $title_list[$title_counter]$link_ext");
    		$start_page = ($stop_page + 1);
		$stage = "body";
	      }
	    if    ($split_level eq "part"
	           && $line =~ m/tome\{([0-9]+)\}pageoffset\{([0-9]+)\}relativepage\{([0-9]+)\}/i)
	      {
		$stop_page = ($2 + $3 -1);
		&split_pages_now ($start_page, $stop_page, $source_file_name, $source_root_file_name . ".0", $split_type);
		#$title_counter++;
		#system ("ln -s ${source_root_file_name}.0$link_ext $title_list[$title_counter]$link_ext");
		$stage = "void";
	      }
	  }
    	elsif ($stage eq "body")
	  {
	    if    ($split_level eq "tome"
	           && $line =~ m/tome\{([0-9]+)\}pageoffset\{([0-9]+)\}relativepage\{([0-9]+)\}/i)
	      {
	        $previous_split = $current_split;
	        $current_split = $1;
	        $stop_page = ($2 + $3 -1);
	        &split_pages_now ($start_page, $stop_page, $source_file_name, $source_root_file_name . "." . $previous_split, $split_type);
		$title_counter++;
		system ("ln -s ${source_root_file_name}.$previous_split$link_ext $title_list[$title_counter]$link_ext");
	        $start_page = ($stop_page + 1);
	      }
	    elsif ($split_level eq "part"
	           && $line =~ m/tome\{([0-9]+)\}pageoffset\{([0-9]+)\}relativepage\{([0-9]+)\}/i)
	      {
	        $stop_page = ($2 + $3 -1);
	        &split_pages_now ($start_page, $stop_page, $source_file_name, $source_root_file_name . "." . $current_split, $split_type);
		$title_counter++;
		system ("ln -s ${source_root_file_name}.$current_split$link_ext $title_list[$title_counter]$link_ext");
		$stage = "void";
	      }
	    elsif ($split_level eq "part"
	           && $line =~ m/part\{([0-9]+)\}pageoffset\{([0-9]+)\}relativepage\{([0-9]+)\}/i)
	      {
	        $previous_split = $current_split;
	        $current_split = $1;
	        $stop_page = ($2 + $3 -1);
	        &split_pages_now ($start_page, $stop_page, $source_file_name, $source_root_file_name . "." . $previous_split, $split_type);
		$title_counter++;
		system ("ln -s ${source_root_file_name}.$previous_split$link_ext $title_list[$title_counter]$link_ext");
	        $start_page = ($stop_page + 1);
	      }
	    elsif ($line =~ m/appendix\{\}pageoffset\{([0-9]+)\}relativepage\{([0-9]+)\}/i)
	      {
		$stop_page = ($1 + $2 -1);
	        &split_pages_now ($start_page, $stop_page, $source_file_name, $source_root_file_name . "." . $current_split, $split_type);
		$title_counter++;
		system ("ln -s ${source_root_file_name}.$current_split$link_ext $title_list[$title_counter]$link_ext");
		$start_page = ($stop_page + 1);
		$stage = "appendix";
	      }
	    elsif ($line =~ m/index\{\}pageoffset\{([0-9]+)\}relativepage\{([0-9]+)\}/i)
	      {
		$stop_page = ($1 + $2 -1);
	        &split_pages_now ($start_page, $stop_page, $source_file_name, $source_root_file_name . "." . $current_split, $split_type);
		$title_counter++;
		system ("ln -s ${source_root_file_name}.$current_split$link_ext $title_list[$title_counter]$link_ext");
		$start_page = ($stop_page + 1);
		$stage = "index";
	      }
	    elsif ($line =~ m/eof\{\}pageoffset\{([0-9]+)\}relativepage\{([0-9]+)\}/i)
	      {
		$stop_page = ($1 + $2);
	        &split_pages_now ($start_page, $stop_page, $source_file_name, $source_root_file_name . "." . $current_split, $split_type);
		$title_counter++;
		system ("ln -s ${source_root_file_name}.$current_split$link_ext $title_list[$title_counter]$link_ext");
		$start_page = ($stop_page + 1);
		$stage = "eof";
	      }
          }
	elsif ($stage eq "appendix")
	  {
	    if ($line =~ m/index\{\}pageoffset\{([0-9]+)\}relativepage\{([0-9]+)\}/i)
	      {
		$stop_page = ($1 + $2 -1);
	        &split_pages_now ($start_page, $stop_page, $source_file_name, $source_root_file_name . ".app", $split_type);
		#$title_counter++;
		#system ("ln -s ${source_root_file_name}.app$link_ext $title_list[$title_counter]$link_ext");
		$start_page = ($stop_page + 1);
		$stage = "index";
	      }
	    elsif ($line =~ m/eof\{\}pageoffset\{([0-9]+)\}relativepage\{([0-9]+)\}/i)
	      {
		$stop_page = ($1 + $2);
	        &split_pages_now ($start_page, $stop_page, $source_file_name, $source_root_file_name . ".app", $split_type);
		#$title_counter++;
		#system ("ln -s ${source_root_file_name}.app$link_ext $title_list[$title_counter]$link_ext");
		$start_page = ($stop_page + 1);
		$stage = "eof";
	      }
	  }
	elsif ($stage eq "index")
	  {
	    if ($line =~ m/eof\{\}pageoffset\{([0-9]+)\}relativepage\{([0-9]+)\}/i)
	      {
		$stop_page = ($1 + $2);
	        &split_pages_now ($start_page, $stop_page, $source_file_name, $source_root_file_name . ".ndx", $split_type);
		#$title_counter++;
		#system ("ln -s ${source_root_file_name}.ndx$link_ext $title_list[$title_counter]$link_ext");
		$start_page = ($stop_page + 1);
		$stage = "eof";
	      }
	  }
    	elsif ($stage eq "void")
	  {
	    if    ($split_level eq "tome"
	           && $line =~ m/tome\{([0-9]+)\}pageoffset\{([0-9]+)\}relativepage\{([0-9]+)\}/i)
	      {
	        $previous_split = $current_split;
	        $current_split = $1;
	        $start_page = ($2 + $3);
		$stage = "body";
	      }
	    elsif ($split_level eq "part"
	           && $line =~ m/part\{([0-9]+)\}pageoffset\{([0-9]+)\}relativepage\{([0-9]+)\}/i)
	      {
	        $previous_split = $current_split;
	        $current_split = $1;
	        $start_page = ($2 + $3);
		$stage = "body";
	      }
	    elsif ($line =~ m/appendix\{\}pageoffset\{([0-9]+)\}relativepage\{([0-9]+)\}/i)
	      {
		$start_page = ($1 + $2);
		$stage = "appendix";
	      }
	    elsif ($line =~ m/index\{\}pageoffset\{([0-9]+)\}relativepage\{([0-9]+)\}/i)
	      {
		$start_page = ($1 + $2);
		$stage = "index";
	      }
	    elsif ($line =~ m/eof\{\}pageoffset\{([0-9]+)\}relativepage\{([0-9]+)\}/i)
	      {
		$start_page = ($1 + $2);
		$stage = "eof";
	      }
	  }
      }        
    close (SPLIT_FILE);
}
##
## Start of program.
##
my ($argument)       = "";
my ($action)         = "";
my ($n)              = 0;
my ($group_pages)    = 0;
my ($split_list_file)  = "";
my ($html_index_dir)  = "";
#
# Scan arguments.
#
for ($n = 0; $n <= $#ARGV; $n++)
  {
    #
    # Analyze argument $n.
    #
    if ($ARGV[$n] !~ m/^-/)
     {
        #
        # This argument has no minus at the beguinning.
	# This must be a final argument.
        #
        $argument = $ARGV[$n];
     }
    elsif ($ARGV[$n] eq "--help")
      {
        #
        # The user is asking for help.
        #
        &help_syntax;
        exit 0;
      }
    elsif ($ARGV[$n] eq "--version")
      {
        #
        # The user wants to know the program version.
        #
        &version_info;
        exit 0;
      }
    elsif ($ARGV[$n] eq "--a4-to-a5")
      {
	$action = "a4-to-a5";
      }
    elsif ($ARGV[$n] eq "--a4-to-a6")
      {
	$action = "a4-to-a6";
      }
    elsif ($ARGV[$n] eq "--a4-to-a5-2-a4")
      {
	$action = "a4-to-a5-2-a4";
      }
    elsif ($ARGV[$n] eq "--a4-to-a5-1-a4")
      {
	$action = "a4-to-a5-1-a4";
      }
    elsif ($ARGV[$n] eq "--a4-to-a6-4-a4")
      {
	$action = "a4-to-a6-4-a4";
      }
    elsif ($ARGV[$n] eq "--a4-to-a5-2-a4-1h-1")
      {
	$action = "a4-to-a5-2-a4-1h-1";
      }
    elsif ($ARGV[$n] eq "--a4-to-a5-2-a4-1h-10")
      {
	$action = "a4-to-a5-2-a4-1h-10";
      }
    elsif ($ARGV[$n] eq "--a4-to-a6-4-a4-2h-2")
      {
	$action = "a4-to-a6-4-a4-2h-2";
      }
    elsif ($ARGV[$n] eq "--a4-to-a6-4-a4-2h-4")
      {
	$action = "a4-to-a6-4-a4-2h-4";
      }
    elsif ($ARGV[$n] eq "--a4-to-a6-4-a4-2h-6")
      {
	$action = "a4-to-a6-4-a4-2h-6";
      }
    elsif ($ARGV[$n] eq "--a4-to-a6-4-a4-2h-8")
      {
	$action = "a4-to-a6-4-a4-2h-8";
      }
    elsif ($ARGV[$n] eq "--a4-to-a6-4-a4-2h-10")
      {
	$action = "a4-to-a6-4-a4-2h-10";
      }
    elsif ($ARGV[$n] eq "--a4-to-a6-4-a4-1v-1")
      {
	$action = "a4-to-a6-4-a4-1v-1";
      }
    elsif ($ARGV[$n] eq "--a5x4-to-a7x4")
      {
	$action = "a5x4-to-a7x4";
      }
    elsif ($ARGV[$n] eq "--a5x4-to-a7x4-2-a4")
      {
	$action = "a5x4-to-a7x4-2-a4";
      }
    elsif ($ARGV[$n] eq "--a7x4-to-a7x4-2-a4")
      {
	$action = "a7x4-to-a7x4-2-a4";
      }
    elsif ($ARGV[$n] eq "--a7x4-to-a7x4-2-a4-1v-1")
      {
	$action = "a7x4-to-a7x4-2-a4-1v-1";
      }
    elsif ($ARGV[$n] eq "--a7x4-to-a7x4-2-a4-1v-10")
      {
	$action = "a7x4-to-a7x4-2-a4-1v-10";
      }
    elsif ($ARGV[$n] =~ m/--ps-group-pages=([^ ]+)/)
      {
        $group_pages = $1;
	$action = "ps-group-pages";
      }
    elsif ($ARGV[$n] =~ m/--ps-renumber-pages/)
      {
	$action = "ps-renumber-pages";
      }
    elsif ($ARGV[$n] =~ m/--html-index=([^ ]+)/)
      {
	$html_index_dir = $1;
	$action = "html-index";
      }
    elsif ($ARGV[$n] =~ m/--html-index-basic=([^ ]+)/)
      {
	$html_index_dir = $1;
	$action = "html-index-basic";
      }
    elsif ($ARGV[$n] =~ m/--html-index-basic-recursive/)
      {
	$action = "html-index-basic-recursive";
      }
    elsif ($ARGV[$n] eq "--html-to-text-for-spell")
      {
	$action = "html-to-text-for-spell";
      }
    elsif ($ARGV[$n] eq "--perl-to-gettext")
      {
	$action = "perl-to-gettext";
      }
    elsif ($ARGV[$n] eq "--dos2unix")
      {
	$action = "dos2unix";
      }
    elsif ($ARGV[$n] eq "--unix2dos")
      {
	$action = "unix2dos";
      }
    elsif ($ARGV[$n] =~ m/--alml-ps-split-tome=([^ ]+)/)
      {
        $split_list_file = $1;
	$action = "alml-ps-split-tome";
      }
    elsif ($ARGV[$n] =~ m/--alml-ps-split-part=([^ ]+)/)
      {
        $split_list_file = $1;
	$action = "alml-ps-split-part";
      }
    elsif ($ARGV[$n] =~ m/--alml-dvi-to-pdf-split-tome=([^ ]+)/)
      {
        $split_list_file = $1;
	$action = "alml-dvi-to-pdf-split-tome";
      }
    elsif ($ARGV[$n] =~ m/--alml-dvi-to-pdf-split-part=([^ ]+)/)
      {
        $split_list_file = $1;
	$action = "alml-dvi-to-pdf-split-part";
      }
    elsif ($ARGV[$n] =~ m/--alml-dvi-split-tome=([^ ]+)/)
      {
        $split_list_file = $1;
	$action = "alml-dvi-split-tome";
      }
    elsif ($ARGV[$n] =~ m/--alml-dvi-split-part=([^ ]+)/)
      {
        $split_list_file = $1;
	$action = "alml-dvi-split-part";
      }
    else
      {
        printf STDERR (gettext ("Unknown argument: %s\n"),
				$ARGV[$n]);
      }
  }
#
# Do the work.
#
if ($action eq "a4-to-a5"
    && $argument ne "")
  {
    &a4_to_a5 ($argument);
  }
elsif ($action eq "a4-to-a6"
    && $argument ne "")
  {
    &a4_to_a6 ($argument);
  }
elsif ($action eq "a4-to-a5-1-a4"
    && $argument ne "")
  {
    &a4_to_a5_1_a4 ($argument);
  }
elsif ($action eq "a4-to-a5-2-a4"
    && $argument ne "")
  {
    &a4_to_a5_2_a4 ($argument);
  }
elsif ($action eq "a4-to-a6-4-a4"
    && $argument ne "")
  {
    &a4_to_a6_4_a4 ($argument);
  }
elsif ($action eq "a4-to-a5-2-a4-1h-1"
    && $argument ne "")
  {
    &a4_to_a5_2_a4_1h_1 ($argument);
  }
elsif ($action eq "a4-to-a5-2-a4-1h-10"
    && $argument ne "")
  {
    &a4_to_a5_2_a4_1h_10 ($argument);
  }
elsif ($action eq "a4-to-a6-4-a4-2h-2"
    && $argument ne "")
  {
    &a4_to_a6_4_a4_2h_2 ($argument);
  }
elsif ($action eq "a4-to-a6-4-a4-2h-4"
    && $argument ne "")
  {
    &a4_to_a6_4_a4_2h_4 ($argument);
  }
elsif ($action eq "a4-to-a6-4-a4-2h-6"
    && $argument ne "")
  {
    &a4_to_a6_4_a4_2h_6 ($argument);
  }
elsif ($action eq "a4-to-a6-4-a4-2h-8"
    && $argument ne "")
  {
    &a4_to_a6_4_a4_2h_8 ($argument);
  }
elsif ($action eq "a4-to-a6-4-a4-2h-10"
    && $argument ne "")
  {
    &a4_to_a6_4_a4_2h_10 ($argument);
  }
elsif ($action eq "a4-to-a6-4-a4-1v-1"
       && $argument ne "")
  {
    &a4_to_a6_4_a4_1v_1 ($argument);
  }
elsif ($action eq "a7x4-to-a7x4-2-a4"
       && $argument ne "")
  {
    &a7x4_to_a7x4_2_a4 ($argument);
  }
elsif ($action eq "a5x4-to-a7x4"
       && $argument ne "")
  {
    &a5x4_to_a7x4 ($argument);
  }
elsif ($action eq "a5x4-to-a7x4-2-a4"
       && $argument ne "")
  {
    &a5x4_to_a7x4_2_a4 ($argument);
  }
elsif ($action eq "a7x4-to-a7x4-2-a4-1v-1"
       && $argument ne "")
  {
    &a7x4_to_a7x4_2_a4_1v_1 ($argument);
  }
elsif ($action eq "a7x4-to-a7x4-2-a4-1v-10"
       && $argument ne "")
  {
    &a7x4_to_a7x4_2_a4_1v_10 ($argument);
  }
elsif ($action eq "ps-group-pages"
       && $argument ne "")
  {
    &ps_group_pages ($argument, $group_pages);
  }
elsif ($action eq "ps-renumber-pages"
       && $argument ne "")
  {
    &ps_renumber_pages ($argument, $group_pages);
  }
elsif ($action eq "html-index")
  {
    print STDOUT (&html_index ($html_index_dir));
  }
elsif ($action eq "html-index-basic")
  {
    print STDOUT (&html_index_basic ($html_index_dir));
  }
elsif ($action eq "html-index-basic-recursive")
  {
    &html_index_basic_recursive (".");
  }
elsif ($action eq "html-to-text-for-spell")
  {
    my ($line) = "";

    # Scan and subst.
    while ($line = <STDIN>)
      {
	# Eliminate HTML elements.
        $line =~ s/<[^<>]*>//g;

        # US-ASCII special characters for any typesetting system.
        $line =~ s/&excl;/\x21/g;    # ISOnum : EXCLAMATION MARK
        $line =~ s/&quot;/\x22/g;    # ISOnum : QUOTATION MARK
        $line =~ s/&num;/\x23/g;    # ISOnum : NUMBER SIGN
        $line =~ s/&dollar;/\x24/g;    # ISOnum : dollar sign
        $line =~ s/&percnt;/\x25/g;    # ISOnum : percent sign
        $line =~ s/&amp;/\x26/g;    # ISOnum : ampersand
        $line =~ s/&apos;/\x27/g;    # ISOnum : apostrophe
        $line =~ s/&lpar;/\x28/g;    # ISOnum : left parenthesis
        $line =~ s/&rpar;/\x29/g;    # ISOnum : right parenthesis
        $line =~ s/&ast;/\x2A/g;    # ISOnum : asterisk
        $line =~ s/&plus;/\x2B/g;    # ISOnum : plus sign
        $line =~ s/&comma;/\x2C/g;    # ISOnum : comma
        $line =~ s/&hyphen;/\x2D/g;    # ISOnum : hyphen
        $line =~ s/&dot;/\x2E/g;    # ISOnum : dot above
        $line =~ s/&sol;/\x2F/g;    # ISOnum : solidus
        # numbers from 0 to 9 are not translated
        $line =~ s/&colon;/\x3A/g;    # ISOnum : colon
        $line =~ s/&semi;/\x3B/g;    # ISOnum : semicolon
        $line =~ s/&lt;/\x3C/g;    # ISOnum : less-than sign
        $line =~ s/&equals;/\x3D/g;    # ISOnum : equals sign
        $line =~ s/&gt;/\x3E/g;    # ISOnum : greater-than sign
        $line =~ s/&quest;/\x3F/g;    # ISOnum : question mark
        $line =~ s/&commat;/\x40/g;    # ISOnum : commercial at
        # letters from A to Z are not translated
        $line =~ s/&lsqb;/\x5B/g;  # ISOnum : left square bracket
        $line =~ s/&bsol;/\x5C/g;  # ISOnum : reverse solidus - this is special!
        $line =~ s/&rsqb;/\x5D/g;  # ISOnum : right square bracket
        $line =~ s/&circ;/\x5E/g;  # ISOnum : circumflex accent
        $line =~ s/&lowbar;/\x5F/g;  # ISOnum : low line
        $line =~ s/&lsquo;/\x60/g;  # ISOnum : single quotation mark, left
        # letters from a to z are not translated
        $line =~ s/&lcub;/\x7B/g;  # ISOnum : left curly bracket
        $line =~ s/&verbar;/\x7C/g;  # ISOnum : vertical bar
        $line =~ s/&rcub;/\x7D/g;  # ISOnum : right curly bracket
        $line =~ s/&tilde;/\x7E/g;  # ISOdia : tilde

        # ISO 8859-1
        $line =~ s/&nbsp;/\xA0/g;    # ISOnum : NO-BREAK SPACE
        $line =~ s/&iexcl;/\xA1/g;  # ISOnum : INVERTED EXCLAMATION MARK
        $line =~ s/&cent;/\xA2/g;  # ISOnum : CENT SIGN
        $line =~ s/&pound;/\xA3/g;  # ISOnum : POUND SIGN
        $line =~ s/&curren;/\xA4/g;  # ISOnum : CURRENCY SIGN
        $line =~ s/&yen;/\xA5/g;  # ISOnum : YEN SIGN
        $line =~ s/&brvbar;/\xA6/g;  # ISOnum : BROKEN BAR
        $line =~ s/&sect;/\xA7/g;  # ISOnum : SECTION SIGN
        $line =~ s/&die;/\xA8/g;  # ISOdia : DIAERESIS
        $line =~ s/&copy;/\xA9/g;  # ISOnum : COPYRIGHT SIGN
        $line =~ s/&ordf;/\xAA/g;  # ISOnum : FEMININE ORDINAL INDICATOR
        $line =~ s/&laquo;/\xAB/g;  # ISOnum : LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
        $line =~ s/&not;/\xAC/g;  # ISOnum : NOT SIGN
        $line =~ s/&shy;/\xAD/g;  # ISOnum : SOFT HYPHEN
        $line =~ s/&reg;/\xAE/g;  # ISOnum : REGISTERED SIGN
        $line =~ s/&macr;/\xAF/g;  # ISOdia : OVERLINE (MACRON)
        $line =~ s/&deg;/\xB0/g;  # ISOnum : DEGREE SIGN
        $line =~ s/&plusmn;/\xB1/g;  # ISOnum : PLUS-MINUS SIGN
        $line =~ s/&sup2;/\xB2/g;  # ISOnum : SUPERSCRIPT TWO
        $line =~ s/&sup3;/\xB3/g;  # ISOnum : SUPERSCRIPT THREE
        $line =~ s/&acute;/\xB4/g;  # ISOdia : ACUTE ACCENT
        $line =~ s/&micro;/\xB5/g;  # ISOnum : MICRO SIGN
        $line =~ s/&para;/\xB6/g;  # ISOnum : PILCROW SIGN
        $line =~ s/&middot;/\xB7/g;  # ISOnum : MIDDLE DOT
        $line =~ s/&cedil;/\xB8/g;  # ISOdia : CEDILLA
        $line =~ s/&sup1;/\xB9/g;  # ISOnum : SUPERSCRIPT ONE
        $line =~ s/&ordm;/\xBA/g;  # ISOnum : MASCULINE ORDINAL INDICATOR
        $line =~ s/&raquo;/\xBB/g;  # ISOnum : RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
        $line =~ s/&frac14;/\xBC/g;  # ISOnum : VULGAR FRACTION ONE QUARTER
        $line =~ s/&frac12;/\xBD/g;  # ISOnum : VULGAR FRACTION ONE HALF
        $line =~ s/&frac34;/\xBE/g;  # ISOnum : VULGAR FRACTION THREE QUARTERS
        $line =~ s/&iquest;/\xBF/g;  # ISOnum : INVERTED QUESTION MARK
        $line =~ s/&Agrave;/\xC0/g;  # ISOlat1: LATIN CAPITAL LETTER A WITH GRAVE
        $line =~ s/&Aacute;/\xC1/g;  # ISOlat1: LATIN CAPITAL LETTER A WITH ACUTE
        $line =~ s/&Acirc;/\xC2/g;  # ISOlat1: LATIN CAPITAL LETTER A WITH CIRCUMFLEX
        $line =~ s/&Atilde;/\xC3/g;  # ISOlat1: LATIN CAPITAL LETTER A WITH TILDE
        $line =~ s/&Auml;/\xC4/g;  # ISOlat1: LATIN CAPITAL LETTER A WITH DIAERESIS
        $line =~ s/&Aring;/\xC5/g;  # ISOlat1: LATIN CAPITAL LETTER A WITH RING ABOVE
        $line =~ s/&AElig;/\xC6/g;  # ISOlat1: LATIN CAPITAL LETTER AE
        $line =~ s/&Ccedil;/\xC7/g;  # ISOlat1: LATIN CAPITAL LETTER C WITH CEDILLA
        $line =~ s/&Egrave;/\xC8/g;  # ISOlat1: LATIN CAPITAL LETTER E WITH GRAVE
        $line =~ s/&Eacute;/\xC9/g;  # ISOlat1: LATIN CAPITAL LETTER E WITH ACUTE
        $line =~ s/&Ecirc;/\xCA/g;  # ISOlat1: LATIN CAPITAL LETTER E WITH CIRCUMFLEX
        $line =~ s/&Euml;/\xCB/g;  # ISOlat1: LATIN CAPITAL LETTER E WITH DIAERESIS
        $line =~ s/&Igrave;/\xCC/g;  # ISOlat1: LATIN CAPITAL LETTER I WITH GRAVE
        $line =~ s/&Iacute;/\xCD/g;  # ISOlat1: LATIN CAPITAL LETTER I WITH ACUTE
        $line =~ s/&Icirc;/\xCE/g;  # ISOlat1: LATIN CAPITAL LETTER I WITH CIRCUMFLEX
        $line =~ s/&Iuml;/\xCF/g;  # ISOlat1: LATIN CAPITAL LETTER I WITH DIAERESIS
        $line =~ s/&ETH;/\xD0/g;  # ISOlat1: LATIN CAPITAL LETTER ETH (Icelandic)
        $line =~ s/&Ntilde;/\xD1/g;  # ISOlat1: LATIN CAPITAL LETTER N WITH TILDE
        $line =~ s/&Ograve;/\xD2/g;  # ISOlat1: LATIN CAPITAL LETTER O WITH GRAVE
        $line =~ s/&Oacute;/\xD3/g;  # ISOlat1: LATIN CAPITAL LETTER O WITH ACUTE
        $line =~ s/&Ocirc;/\xD4/g;  # ISOlat1: LATIN CAPITAL LETTER O WITH CIRCUMFLEX
        $line =~ s/&Otilde;/\xD5/g;  # ISOlat1: LATIN CAPITAL LETTER O WITH TILDE
        $line =~ s/&Ouml;/\xD6/g;  # ISOlat1: LATIN CAPITAL LETTER O WITH DIAERESIS
        $line =~ s/&times;/\xD7/g;  # ISOnum : MULTIPLICATION SIGN
        $line =~ s/&Oslash;/\xD8/g;  # ISOlat1: LATIN CAPITAL LETTER O WITH STROKE
        $line =~ s/&Ugrave;/\xD9/g;  # ISOlat1: LATIN CAPITAL LETTER U WITH GRAVE
        $line =~ s/&Uacute;/\xDA/g;  # ISOlat1: LATIN CAPITAL LETTER U WITH ACUTE
        $line =~ s/&Ucirc;/\xDB/g;  # ISOlat1: LATIN CAPITAL LETTER U WITH CIRCUMFLEX
        $line =~ s/&Uuml;/\xDC/g;  # ISOlat1: LATIN CAPITAL LETTER U WITH DIAERESIS
        $line =~ s/&Yacute;/\xDD/g;  # ISOlat1: LATIN CAPITAL LETTER Y WITH ACUTE
        $line =~ s/&THORN;/\xDE/g;  # ISOlat1: LATIN CAPITAL LETTER THORN (Icelandic)
        $line =~ s/&szlig;/\xDF/g;  # ISOlat1: LATIN SMALL LETTER SHARP S (German)
        $line =~ s/&agrave;/\xE0/g;  # ISOlat1: LATIN SMALL LETTER A WITH GRAVE
        $line =~ s/&aacute;/\xE1/g;  # ISOlat1: LATIN SMALL LETTER A WITH ACUTE
        $line =~ s/&acirc;/\xE2/g;  # ISOlat1: LATIN SMALL LETTER A WITH CIRCUMFLEX
        $line =~ s/&atilde;/\xE3/g;  # ISOlat1: LATIN SMALL LETTER A WITH TILDE
        $line =~ s/&auml;/\xE4/g;  # ISOlat1: LATIN SMALL LETTER A WITH DIAERESIS
        $line =~ s/&aring;/\xE5/g;  # ISOlat1: LATIN SMALL LETTER A WITH RING ABOVE
        $line =~ s/&aelig;/\xE6/g;  # ISOlat1: LATIN SMALL LETTER AE
        $line =~ s/&ccedil;/\xE7/g;  # ISOlat1: LATIN SMALL LETTER C WITH CEDILLA
        $line =~ s/&egrave;/\xE8/g;  # ISOlat1: LATIN SMALL LETTER E WITH GRAVE
        $line =~ s/&eacute;/\xE9/g;  # ISOlat1: LATIN SMALL LETTER E WITH ACUTE
        $line =~ s/&ecirc;/\xEA/g;  # ISOlat1: LATIN SMALL LETTER E WITH CIRCUMFLEX
        $line =~ s/&euml;/\xEB/g;  # ISOlat1: LATIN SMALL LETTER E WITH DIAERESIS
        $line =~ s/&igrave;/\xEC/g;  # ISOlat1: LATIN SMALL LETTER I WITH GRAVE
        $line =~ s/&iacute;/\xED/g;  # ISOlat1: LATIN SMALL LETTER I WITH ACUTE
        $line =~ s/&icirc;/\xEE/g;  # ISOlat1: LATIN SMALL LETTER I WITH CIRCUMFLEX
        $line =~ s/&iuml;/\xEF/g;  # ISOlat1: LATIN SMALL LETTER I WITH DIAERESIS
        $line =~ s/&eth;/\xF0/g;  # ISOlat1: LATIN SMALL LETTER ETH (Icelandic)
        $line =~ s/&ntilde;/\xF1/g;  # ISOlat1: LATIN SMALL LETTER N WITH TILDE
        $line =~ s/&ograve;/\xF2/g;  # ISOlat1: LATIN SMALL LETTER O WITH GRAVE
        $line =~ s/&oacute;/\xF3/g;  # ISOlat1: LATIN SMALL LETTER O WITH ACUTE
        $line =~ s/&ocirc;/\xF4/g;  # ISOlat1: LATIN SMALL LETTER O WITH CIRCUMFLEX
        $line =~ s/&otilde;/\xF5/g;  # ISOlat1: LATIN SMALL LETTER O WITH TILDE
        $line =~ s/&ouml;/\xF6/g;  # ISOlat1: LATIN SMALL LETTER O WITH DIAERESIS
        $line =~ s/&divide;/\xF7/g;  # ISOnum : DIVISION SIGN
        $line =~ s/&oslash;/\xF8/g;  # ISOlat1: LATIN SMALL LETTER O WITH STROKE
        $line =~ s/&ugrave;/\xF9/g;  # ISOlat1: LATIN SMALL LETTER U WITH GRAVE
        $line =~ s/&uacute;/\xFA/g;  # ISOlat1: LATIN SMALL LETTER U WITH ACUTE
        $line =~ s/&ucirc;/\xFB/g;  # ISOlat1: LATIN SMALL LETTER U WITH CIRCUMFLEX
        $line =~ s/&uuml;/\xFC/g;  # ISOlat1: LATIN SMALL LETTER U WITH DIAERESIS
        $line =~ s/&yacute;/\xFD/g;  # ISOlat1: LATIN SMALL LETTER Y WITH ACUTE
        $line =~ s/&thorn;/\xFE/g;  # ISOlat1: LATIN SMALL LETTER THORN (Icelandic)
	$line =~ s/&yuml;/\xFF/g;  # ISOlat1: LATIN SMALL LETTER Y WITH DIAERESIS

        # Extra ISO standard
	$line =~ s/&euro;/EUR/g;  # Euro

	# Extra $#nnn chars
	while ($line =~ m/^(.*)&\#([0-9]+);(.*)$/)
	  {
	    $line = $1 . chr ($2) . $3;
	  }

	# Extra $#xnnn chars
	while ($line =~ m/^(.*)&\#x([0-9]+);(.*)$/)
	  {
	    $line = $1 . chr (hex ($2)) . $3;
	  }

        # Delete extra entities; replace with "?".
	$line =~ s/&[^; ]+;/\?/g;  

	# Eliminate "\"
        $line =~ s/\\/ /g;

	# Print output.
        print STDOUT ("$line\n");
      }
  }
elsif ($action eq "perl-to-gettext")
  {
    my ($line) = "";

    #open (PERL_SOURCE, "cat $argument | expand |");
    #open (PERL_SOURCE_GETTEXT, "> ${argument}~");

    while ($line = <STDIN>)
      {
        $line =~ s/\\$/\\n\\/;
        $line =~ s/\\\@/\@/g;
	print STDOUT ($line);
      }        
    #close (PERL_SOURCE);
    #close (PERL_SOURCE_GETTEXT);
  }
elsif ($action eq "dos2unix")
  {
    my ($line) = "";
    #
    # Change the current line separator to <CR><LF>.
    #
    $/ = "\r\n";
    while ($line = <STDIN>)
      {
	chomp ($line);
	print STDOUT ($line);
	print STDOUT ("\n");
      }
    exit 0;
  }
elsif ($action eq "unix2dos")
  {
    my ($line) = "";
    while ($line = <STDIN>)
      {
	chomp ($line);
	print STDOUT ($line);
	print STDOUT ("\r\n");
      }
    exit 0;
  }
elsif ($action eq "alml-ps-split-tome"
       && $argument ne "")
  {
    &split_pages ($argument, "psps", "tome", $split_list_file);
  }
elsif ($action eq "alml-ps-split-part"
       && $argument ne "")
  {
    &split_pages ($argument, "psps", "part", $split_list_file);
  }
elsif ($action eq "alml-dvi-to-pdf-split-tome"
       && $argument ne "")
  {
    &split_pages ($argument, "dvipdf", "tome", $split_list_file);
  }
elsif ($action eq "alml-dvi-to-pdf-split-part"
       && $argument ne "")
  {
    &split_pages ($argument, "dvipdf", "part", $split_list_file);
  }
elsif ($action eq "alml-dvi-split-tome"
       && $argument ne "")
  {
    &split_pages ($argument, "dvidvi", "tome", $split_list_file);
  }
elsif ($action eq "alml-dvi-split-part"
       && $argument ne "")
  {
    &split_pages ($argument, "dvidvi", "part", $split_list_file);
  }

