#!/usr/bin/perl

# Script to turn the Exim FAQ into plain ASCII.

use integer;


# Function to do text conversions to display paragraphs

sub process_display {
my($s) = $_[0];
$s =~ s/^==>/   /;
return $s;
}


# Function to do text conversions to paragraphs not in displays.

sub process_non_display {
my($s) = $_[0];

$s =~ s/@\\/@@backslash@@/g;                 # @\ temporarily hidden

$s =~ s/\\#/ /g;                             # \# is a hard space

$s =~ s/\\\*\*([^*]*)\*\*\\/$1/g;            # \**...**\   => text
$s =~ s/\\\*([^*]*)\*\\/"$1"/g;              # \*.....*\   => "text"
$s =~ s/\\"([^"]*)"\\/"$1"/g;                # \"....."\   => "text"
$s =~ s/\\\$([^\$]*)\$\\/\$$1/g;             # \$.....$\   => $text
$s =~ s/\\\\([^\\]*)\\\\/$1/g;               # \\.....\\   => text
$s =~ s/\\\(([^)]*)\)\\/$1/g;                # \(.....)\   => text
$s =~ s/\\-([^-]*)-\\/-$1/g;                 # \-.....-\   => -text
$s =~ s/\\\[([^]]*)\]\\/<$1>/gx;             # \[.....]\   => <text>
$s =~ s/\\\?(.*?)\?\\/$1/g;                  # \?.....?\   => text
$s =~ s/\\\^\^([^^]*)\^\^\\/$1/g;            # \^^...^^\   => text
$s =~ s/\\\^([^^]*)\^\\/$1/g;                # \^.....^\   => text
$s =~ s/\\%([^%]*)%\\/"$1"/g;                # \%.....%\   => "text"
$s =~ s/\\\/([^\/]*)\/\\/$1/g;               # \/...../\   => text
$s =~ s/\\([^\\]+)\\/"$1"/g;                 # \.......\   => "text"

$s =~ s"//([^/\"]*)//"$1"g;                  # //.....//   => text
$s =~ s/::([^:]*)::/$1:/g;                   # ::.....::   => text:

$s =~ s/``(.*?)''/"$1"/g;                    # ``.....''   => "text"

$s =~ s/\s*\[\[br\]\]\s*\n/\n/g;             # Remove [[br]]

$s =~ s/@@backslash@@/\\/g;                  # Put back single backslash

return $s;
}


# Main program

# We want to read the file paragraph by paragraph; Perl only does this if the
# separating lines are truly blank. Having been caught by lines containing
# whitespace before, do a de-trailing pass first.

open(IN, "$ARGV[0]") || die "can't open $ARGV[0] (preliminary)\n";
open(OUT, ">$ARGV[0]-$$") || die "can't open $ARGV[0]-$$\n";
while (<IN>)
  {
  s/[ \t]+$//;
  print OUT;
  }
close(IN);
close(OUT);
rename("$ARGV[0]-$$", "$ARGV[0]") ||
  die "can't rename $ARGV[0]-$$ as $ARGV[0]\n";

# The second argument is the name of the output file.

open(IN, "$ARGV[0]") || die "can't open $ARGV[0] (for real)\n";
open(OUT, ">$ARGV[1]") || die "can't open $ARGV[1]\n";

$/ = "";

while ($_ = <IN>)
  {
  # Comment lines start with ##

  next if /^\#\#/;

  # If a paragraph begins ==> it is a display which must remain verbatim
  # and not be reformatted. The flag gets turned into spaces.

  if ($_ =~ /^==>/)
    {
    $_ = &process_display($_);
    }

  # Non-display paragraph

  else
    {
    $_ = &process_non_display($_);
    }

  print OUT;
  }

close(IN);
close(OUT);

End
