#!/usr/bin/perl
#=======================================================================
# Copyright (c) 2000-2001 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 = "2002.03.04";

# 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\
       %s --html-to-text-for-spell\
       %s --perl-to-gettext\
\
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-2-a4\
    --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\
    --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.\
\
Different purpose options:\
\
--html-index		    create an HTML page on standard output, with a\
                              simple list of files.\
--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.\
\
Final argument:\
The final argument might be a PostScript file, for PostScript\
conversions; in case of --html-to-text-for-spell, it is a HTML file.\
\
Report bugs to <daniele\@swlibero.org>"),
    $program_executable,
    $program_executable,
    $program_executable,
    $program_executable);

}

#-----------------------------------------------------------------------
# Show version information.
#
# &version_info ()
#-----------------------------------------------------------------------
sub version_info
{
    printf STDOUT (gettext ("\
%s %s\
\
Copyright (c) 2000-2001 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
{
    local ($file_prefix) = $_[0];
    local ($temp_dir) = "";
    local ($random_number) = 0;
    local ($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
{
    local ($file_name) = $_[0];
    local ($extention_name) = $_[1];
    local ($root_name) = "";
    
    $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
{
    local ($file_name)   = $_[0];
    local ($group_pages) = $_[1];
    local ($original_file_name) = $file_name . "~";
    local ($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);
}

#-----------------------------------------------------------------------
# Renumber PostScript pages.
#
# &ps_renumber_pages (FILE_NAME)
#-----------------------------------------------------------------------
sub ps_renumber_pages
{
    local ($file_name)          = $_[0];
    local ($original_file_name) = $file_name . "~";
    local ($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
{
    local ($file_name)          = $_[0];
    local ($new_bounding_box)   = $_[1];
    local ($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);
}


#-----------------------------------------------------------------------
# A4 to A5 two times inside a final A4 landscape (no folding).
#-----------------------------------------------------------------------
sub a4_to_a5_2_a4
{
    local ($file_name)      = $_[0];
    local ($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
{
    local ($file_name)      = $_[0];
    local ($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
{
    local ($file_name)      = $_[0];
    local ($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
{
    local ($file_name)      = $_[0];
    local ($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
{
    local ($file_name)      = $_[0];
    local ($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
{
    local ($file_name)      = $_[0];
    local ($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
{
    local ($file_name)      = $_[0];
    local ($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
{
    local ($file_name)      = $_[0];
    local ($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
{
    local ($file_name)      = $_[0];
    local ($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
{
    local ($file_name)      = $_[0];
    local ($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);
}

#-----------------------------------------------------------------------
# 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
{
    local ($file_name)      = $_[0];
    local ($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
{
    local ($file_name)      = $_[0];
    local ($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");
}


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

#----------------------------------------------------------------------
# Start of program.
#----------------------------------------------------------------------
local ($argument)       = "";
local ($action)         = "";
local ($n)              = 0;
local ($group_pages)    = 0;
local ($split_list_file)  = "";
local ($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-2-a4")
      {
	$action = "a4-to-a5-2-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 "--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] 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] =~ 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";
      }
    else
      {
        printf STDERR (gettext ("Unknown argument: %s\n",
				$ARGV[$n]));
      }
  }

#-----------------------------------------------------------------------
# Do the work.
#-----------------------------------------------------------------------

if ($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-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);
  }
elsif ($action eq "ps-renumber-pages"
       && $argument ne "")
  {
    &ps_renumber_pages ($argument);
  }
elsif ($action eq "html-index")
  {

    local ($line) = "";
    local ($type) = "";
    local ($inode) = "";
    local ($user) = "";
    local ($group) = "";
    local ($size) = "";
    local ($week) = "";
    local ($month) = "";
    local ($day) = "";
    local ($hour) = "";
    local ($year) = "";
    local ($name) = "";

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

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

    while ($line = <DIRECTORY_LISTING>)
      {
        if ($line =~ m/^(\S)\S+\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)$/)
	  {
	    $type	= $1;
	    $inode 	= $2;
	    $user	= $3;
	    $group	= $4;
	    $size	= $5;
    	    $week	= $6;
    	    $month	= $7;
	    $day	= $8;
	    $hour	= $9;
	    $year	= $10;
	    $name	= $11;

	    if ($type eq "d")
	      {
	        print STDOUT ("<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")
	      {
	        print STDOUT ("<TR><TD>link</TD><TD><A HREF=\"$name\">$name</A></TD><TD></TD><TD>$month</TD><TD>$day</TD><TD>$year</TD></TR>\n");
	      }
	    else
	      {
	        print STDOUT ("<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");
	      }
	  }
      }
    print STDOUT ("</TBODY>\n");
    print STDOUT ("</TABLE>\n");
    print STDOUT ("</BODY>\n");
    print STDOUT ("</HTML>\n");

    close (DIRECTORY_LISTING);
  }
elsif ($action eq "html-to-text-for-spell")
  {
    local ($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

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

	# Print output.
        print STDOUT ($line);
      }
  }
elsif ($action eq "perl-to-gettext")
  {
    local ($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 "alml-ps-split-tome"
       && $argument ne "")
  {
    local ($line) = "";
    local ($stage) = "intro";
    local ($previous_tome) = 0;
    local ($tome) = 0;
    local ($start_page) = 1;
    local ($stop_page) = 0;
    local ($radice) = &root_name ($argument, ".ps");

    open (SPLIT_FILE, "< $split_list_file");

    while ($line = <SPLIT_FILE>)
      {
	if ($stage eq "intro")
	  {
	    if ($line =~ m/tome\{([0-9]+)\}pageoffset\{([0-9]+)\}relativepage\{([0-9]+)\}/i)
	      {
		$tome = $1;
		$stop_page = ($2 + $3 -1);
	        print STDOUT ("$start_page-$stop_page	$radice.0.ps\n");
		system ("psselect -q -p $start_page-$stop_page $argument $radice.0.ps");
		$start_page = ($stop_page + 1);
		$stage = "tome";
	      }
	  }
	elsif ($stage eq "tome")
	  {
	    if ($line =~ m/tome\{([0-9]+)\}pageoffset\{([0-9]+)\}relativepage\{([0-9]+)\}/i)
	      {
		$previous_tome = $tome;
		$tome = $1;
		$stop_page = ($2 + $3 -1);
	        print STDOUT ("$start_page-$stop_page	$radice.$previous_tome.ps\n");
		system ("psselect -q -p $start_page-$stop_page $argument $radice.$previous_tome.ps");
		$start_page = ($stop_page + 1);
	      }
	    elsif ($line =~ m/appendix\{\}pageoffset\{([0-9]+)\}relativepage\{([0-9]+)\}/i)
	      {
		$stop_page = ($1 + $2 -1);
	        print STDOUT ("$start_page-$stop_page	$radice.$tome.ps\n");
		system ("psselect -q -p $start_page-$stop_page $argument $radice.$tome.ps");
		$start_page = ($stop_page + 1);
		$stage = "appendix";
	      }
	    elsif ($line =~ m/index\{\}pageoffset\{([0-9]+)\}relativepage\{([0-9]+)\}/i)
	      {
		$stop_page = ($1 + $2 -1);
	        print STDOUT ("$start_page-$stop_page	$radice.$tome.ps\n");
		system ("psselect -q -p $start_page-$stop_page $argument $radice.$tome.ps");
		$start_page = ($stop_page + 1);
		$stage = "index";
	      }
	    elsif ($line =~ m/eof\{\}pageoffset\{([0-9]+)\}relativepage\{([0-9]+)\}/i)
	      {
		$stop_page = ($1 + $2);
	        print STDOUT ("$start_page-$stop_page	$radice.$tome.ps\n");
		system ("psselect -q -p $start_page-$stop_page $argument $radice.$tome.ps");
		$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);
	        print STDOUT ("$start_page-$stop_page	$radice.app.ps\n");
		system ("psselect -q -p $start_page-$stop_page $argument $radice.app.ps");
		$start_page = ($stop_page + 1);
		$stage = "index";
	      }
	    elsif ($line =~ m/eof\{\}pageoffset\{([0-9]+)\}relativepage\{([0-9]+)\}/i)
	      {
		$stop_page = ($1 + $2);
	        print STDOUT ("$start_page-$stop_page	$radice.app.ps\n");
		system ("psselect -q -p $start_page-$stop_page $argument $radice.app.ps");
		$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);
	        print STDOUT ("$start_page-$stop_page	$radice.ndx.ps\n");
		system ("psselect -q -p $start_page-$stop_page $argument $radice.ndx.ps");
		$start_page = ($stop_page + 1);
		$stage = "eof";
	      }
	  }
      }        
    close (SPLIT_FILE);
  }
elsif ($action eq "alml-ps-split-part"
       && $argument ne "")
  {
    local ($line) = "";
    local ($stage) = "intro";
    local ($previous_part) = 0;
    local ($part) = 0;
    local ($start_page) = 1;
    local ($stop_page) = 0;
    local ($radice) = &root_name ($argument, ".ps");

    open (SPLIT_FILE, "< $split_list_file");

    while ($line = <SPLIT_FILE>)
      {
	if ($stage eq "intro")
	  {
	    if ($line =~ m/part\{([0-9]+)\}pageoffset\{([0-9]+)\}relativepage\{([0-9]+)\}/i)
	      {
		$part = $1;
		$stop_page = ($2 + $3 -1);
	        print STDOUT ("$start_page-$stop_page	$radice.0.ps\n");
		system ("psselect -q -p $start_page-$stop_page $argument $radice.0.ps");
		$start_page = ($stop_page + 1);
		$stage = "part";
	      }
	  }
	elsif ($stage eq "part")
	  {
	    if ($line =~ m/part\{([0-9]+)\}pageoffset\{([0-9]+)\}relativepage\{([0-9]+)\}/i)
	      {
		$previous_part = $part;
		$part = $1;
		$stop_page = ($2 + $3 -1);
	        print STDOUT ("$start_page-$stop_page	$radice.$previous_part.ps\n");
		system ("psselect -q -p $start_page-$stop_page $argument $radice.$previous_part.ps");
		$start_page = ($stop_page + 1);
	      }
	    elsif ($line =~ m/appendix\{\}pageoffset\{([0-9]+)\}relativepage\{([0-9]+)\}/i)
	      {
		$stop_page = ($1 + $2 -1);
	        print STDOUT ("$start_page-$stop_page	$radice.$part.ps\n");
		system ("psselect -q -p $start_page-$stop_page $argument $radice.$part.ps");
		$start_page = ($stop_page + 1);
		$stage = "appendix";
	      }
	    elsif ($line =~ m/index\{\}pageoffset\{([0-9]+)\}relativepage\{([0-9]+)\}/i)
	      {
		$stop_page = ($1 + $2 -1);
	        print STDOUT ("$start_page-$stop_page	$radice.$part.ps\n");
		system ("psselect -q -p $start_page-$stop_page $argument $radice.$part.ps");
		$start_page = ($stop_page + 1);
		$stage = "index";
	      }
	    elsif ($line =~ m/eof\{\}pageoffset\{([0-9]+)\}relativepage\{([0-9]+)\}/i)
	      {
		$stop_page = ($1 + $2);
	        print STDOUT ("$start_page-$stop_page	$radice.$part.ps\n");
		system ("psselect -q -p $start_page-$stop_page $argument $radice.$part.ps");
		$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);
	        print STDOUT ("$start_page-$stop_page	$radice.app.ps\n");
		system ("psselect -q -p $start_page-$stop_page $argument $radice.app.ps");
		$start_page = ($stop_page + 1);
		$stage = "index";
	      }
	    elsif ($line =~ m/eof\{\}pageoffset\{([0-9]+)\}relativepage\{([0-9]+)\}/i)
	      {
		$stop_page = ($1 + $2);
	        print STDOUT ("$start_page-$stop_page	$radice.app.ps\n");
		system ("psselect -q -p $start_page-$stop_page $argument $radice.app.ps");
		$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);
	        print STDOUT ("$start_page-$stop_page	$radice.ndx.ps\n");
		system ("psselect -q -p $start_page-$stop_page $argument $radice.ndx.ps");
		$start_page = ($stop_page + 1);
		$stage = "eof";
	      }
	  }
      }        
    close (SPLIT_FILE);
  }

