#!/usr/bin/perl -w
# Author: I.Plisco (igor at plisco dot ru)
# Aim: Gets unicode codes from sample file.
# $Id$

use strict;

use File::Find;
use IO::Handle;

my $file_codes = "trans.dat";

my %codes;
my $count;

%codes = read_codes();

my $do_unlink = 1;

# Define parameters
my $dir_in = 'G:\tmp\texts';
my $dir_test = 'G:\tmp\texts\Apr';
#$/ = undef;
STDOUT->autoflush(1);

# Test or work
if(0) {
  chdir $dir_test;
  recode_file('08.htm');
} else {
  find(\&recode_file, $dir_in);
}


#============================================================
sub recode_file {
#============================================================

=head2 recode_file

 Purpose   : Recodes one file.
 Arguments :
	1 - filename
 Returns   : Nothing.
 Throws    : No.
 Comments  : None.

See Also   : N/A.

=cut

#------------------------------------------------------------
  my ($file) = @_;
  my ($file_out);

  $file |= $File::Find::name;
  return unless $file;

  if ($file =~ /(.*)\.html?/) {
    $file_out = "$1.txt";
  } else {
    return;
#    $file_out = "$file.txt";
  }

  print "File: $file -> ";

  open (IN, $file) or die;
  open (OUT, ">$file_out") or die;

  while (<IN>) {
    if (/^\s*$/) {
      if ($count) {
	print OUT "\n";
	$count = 0;
      }
      next;
    }
    $count = 0;
    while (/(\&\#\d+\;| )/g) {
      if ($& eq ' ') {
	print OUT "\n" if ($count);
	$count = 0;
      } else {
	$count++;
	print OUT $codes{$&};
      }
    }
    if ($count) {
      print OUT "\n";
#    $count = 0;
    }
  }

  close IN;
  close OUT;

  if ($do_unlink) {
    unlink($file);
  }

  print "$file_out\n";

}


#============================================================
sub read_codes {
#============================================================

=head2 read_codes

 Purpose   : Reads letter codes.
 Arguments :
	None.
 Returns   : Codes as plain hash.
 Throws    : No.
 Comments  : None.

See Also   : N/A.

=cut

#------------------------------------------------------------
#  my () = @_;
  my (@arr);

  my %out = ();

# Do it 
  open(CODES, $file_codes) or die;
  while (<CODES>) {
    chomp;
    @arr = split;
#    $out{$arr[0]} = $arr[1];
    $out{$arr[0]} = lc($arr[1]);
  }

  return %out;

}

