#!/opt/perl/bin/perl -w
# ---------------------------------------------------------------------------
# Convert DOS line endings to UNIX line endings
# (and do a few other character conversions along the way)
#
$vernum = "1.0 - 04 MAR 99" ;           # Code version and modify date
#
# This code copyright 1999 by
# D. W. Eaton, Artronic Development, Phoenix, AZ -- dwe@arde.com
#
# This software is made freely available under the provisions of the Perl
# "Artistic" license:  http://language.perl.com/misc/Artistic.html
#
# This code is not supported and is not warranteed to perform any particular
# function. Contact dwe@arde.com for aditional information.
# If you find bugs or make enhancements, it would be appreciated if you
# sent them on to the author at dwe@arde.com.
#
#
$DOT = "\x85";   # code for "..."
#
$scriptleaf = $0;
$scriptleaf =~ s/^.*\///; # strip leading path to see who we are
use vars qw($opt_h $opt_l $opt_v);
#
# Set the default configuration options:

# Edit the next line to specify the default location of the input file
$default_input = './input';

# ==========================================================================
# Get the command-line options

require "getopts.pl";
&Getopts('hlv');
if ($@ || $opt_h) { &syntax_message; }
if ($opt_l) { $LongLines         = 1; }
if ($opt_v) { $Verbose           = 1; }
#
# Now process each file left on command-line or just the default one
# 
if ($Verbose) { print(STDERR "$scriptleaf $vernum\n"); }

if ($ARGV[0])
{
 # Process files
 while ($default_input = shift)
 {
  &process_it($default_input);
 }
}
else
{
 &process_it($default_input);
}

if ($Verbose) { print(STDERR "Done processing all files.\n"); }

print (STDERR "done.\n");

exit(0);

# ======================================
# Read the passed-in file and process it
#
sub process_it
{
  local($thisfile) = @_;

  if ($Verbose) { print(STDERR "Processing file \"$thisfile\"\n"); }

  if (!open (MYFILE,$thisfile))
  {
    print(STDERR "Error opening file: $thisfile\n");
    return;
  }

  LINE: while (<MYFILE>)
  {
    $saveline = $_ ;

    # Process each line
    $saveline =~ s/\r\n/\n/g ; # convert returns/newline to newline
    $saveline =~ s/\n\r/\n/g ; # convert newline/returns to newline
    $saveline =~ s/\r/\n/g ;   # convert remaining returns to newlines
    $saveline =~ s/\n\n//g ;   # convert multiple newlines to single
    $saveline =~ s//'/g ;     # convert apostrophies
    $saveline =~ s//'/g ;     # convert apostrophies
    $saveline =~ s//-/g ;     # convert dashes
    $saveline =~ s//-/g ;     # convert dashes
    $saveline =~ s//`/g ;     # convert single quotes
    $saveline =~ s//'/g ;     # convert single quotes
    $saveline =~ s/$DOT/.../g ;     # convert '...'

    if ($LongLines)
    {
     $saveline =~ s/\. +/.\n/g ; # convert sentences to separate lines
    }
    print "$saveline";         # print the line

  }
  close MYFILE;

}

# ----------
# Show syntax of how to ues this
#
sub syntax_message
{
    die <<"EndSyntax";
usage: $scriptleaf [-h|-l|-v] filename [morefilenames]
$scriptleaf $vernum
Convert DOS line endings to UNIX line endings
(and do a few other character conversions along the way)

Results are written to STDOUT, which may be redirected
to a specified file. Thus if multiple input filenames
are specified, they are concatenated in the results.

If no filename is specified on the command line, this
script will try to read from '$default_input'.

Display Options:
     -h  Help -- just display this message and quit.
     -l  The file contains long lines.
         Make a newline at the end of each sentence.
     -v  Verbose -- used for diagnostics.
EndSyntax
}

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