#!/usr/local/bin/perl

# read in Windows created text files and fix end of line characters


unless ($#ARGV eq 0) {
  print "ERROR: give directory name to process as argument.\n";
  exit;
}

# read directory name from command line:
$indir = $ARGV[0];

# check it:

print "read from command line: $indir\n";

opendir(DIR, "$indir") || die "Can't open $indir: $!\n";
@in_files = readdir(DIR);
closedir(DIR);

# make list of only *.htm files:
foreach $one (@in_files) {
  if ($one =~ /\.htm$/) {
    push(@htm_files,$one);
  }
}

# check it:
print "\@htm_files:\n @htm_files\n"; 

# now loop over the htm files and translate end of file characters in each:
foreach $hfile (@htm_files) {
#  print "processing $hfile ...\n";
  $infile = $indir . '/' . $hfile;
  print "processing $infile ...\n";
  &read_input;
  &convert_end_olines;
  &write_output;
}





#/////////////////////////////////////////////////////////////////
#///////////////////////////////////////////// subroutines:
#////////////////////////////////////////////////////////////////


sub read_input {
  open(HFILE,"$infile") || die "can't open $infile";

# must now reinitialize $line for each new file!
$line = "";
while (<HFILE>)          #  while lines to read
   {
     $line=$line.$_;     # concatenate with next line
    }
close(HFILE);

## -----------------
##  remove newlines
## -----------------
#
#$_=$line;
#tr/\n/\ /;
#$line=$_;

## check:
#print "\$line:\n";
#print "$line\n";
#print "---------------------------------------\n\n";

}	# end "sub read_input"


#----

sub convert_end_olines {
$_=$line;
tr/\015/\ /;
$line=$_;
}	# end "sub convert_end_olines:

#----

sub write_output {
# make temporary backup copy:
#  $backup = $hfile . '-bak';
  $backup = $infile . '-bak';
  system("cp $infile $backup");

# open output filehandle and write to it:
#  $outfile = $infile;
# (10/20/98) now give output file .html extension:
  $outfile = $infile . 'l';
  open(OUT,">$outfile") || die "Can't open file $outfile: $!";
  print OUT "$line";
  close(OUT);
}


__END__


