#!/usr/bin/perl -C 0

use varcon;

sub usage {
    print STDERR "Usage   : $0 <options> [<translation array>] <from> <to>\n";
    print STDERR "For help: $0 -h\n";
    exit 2;
}

$to   = -1;
$from = -1;
@what = (\$from, \$to);
$file = "varcon.txt";
$ques = 0;
$thresh = 80;

while ($#ARGV != -1) {
    $p = shift @ARGV;
    if ($p eq "--mark" || $p eq "-m") {
        $ques = 2;
    } elsif ($p eq "--include" || $p eq "-i") {
        $ques = 1;
    } elsif ($p eq "--thresh" || $p eq "-t") {
        $thresh = shift;
        &usage if ($thresh < 0 || $thresh > 100);
    } elsif ($p eq '-h' || $p eq '--help' || $p eq '-?') {
        print 
            "translate 1.2 by Kevin Atkinson\n",
            "Usage: $0 <options> [<translation array>] <from> <to>\n",
            "<options> is any of\n",
            "  -?,-h,--help this screen\n",
            "  -m,--mark     mark words where the translation is questionable\n",
            "  -i,--include  include words where the translation is questionable\n",
            "  -t,--thresh <num> only use entres with SCOWL level <= <num>\n",
            "<translation array> is the file name of the translation array,\n",
            "                    defaults to \"abbc.tab\".\n",
            "<from> and <to> are one of: american, british, british_z, or canadian\n";
        exit (0);
    } elsif ($p eq '-') {
        &usage if $#what != 1;
        $w = shift @what;
        $$w = '-';
    } elsif ($p eq "american" || $p eq "American") {
        &usage if $#what == -1;
        $w = shift @what;
        $$w = 'A';
    } elsif ($p eq "british" || $p eq "British" ||
             $p eq "british-ise" || $p eq "British-ise") {
        &usage if $#what == -1;
        $w = shift @what;
        $$w = 'B';
    } elsif ($p eq "british_z" || $p eq "British_z" ||
             $p eq "british-ize" || $p eq "British-ize") {
        &usage if $#what == -1;
        $w = shift @what;
        $$w = 'Z';
    } elsif ($p eq "canadian" || $p eq "Canadian") {
        &usage if $#what == -1;
        $w = shift @what;
        $$w = 'C';
    } elsif ($p eq "australian" || $p eq "Australian") {
        &usage if $#what == -1;
        $w = shift @what;
        $$w = 'D';
    } else {
        $file = $p;
    }
}

&usage if ($to == -1 || $from == -1);
    
$s = open IN, $file;

if (!$s) {
    print STDERR "Unable to open $file\n";
    exit (1);
}

sub add(\@$) {
    return if grep {$_ eq $_[1]} @{$_[0]};
    push @{$_[0]}, $_[1];
}

while (my $d = varcon::get_cluster IN) {
    my @lines = split "\n", $d->{data};
    next unless $d->{level} <= $thresh;
    foreach (@lines) {
        next if varcon::filter $_;
        my %r = varcon::readline($_);
        foreach my $f ($from eq '-' ? (grep {$_ ne $to} keys %varcon::map) : $from) {
            foreach my $v (0..3) {
                foreach (@{$r{$f}[$v]}) {
                    add @{$trans{$_}}, $r{$to}[0][0];
                }
            }
        }
    }
}

while (<>) {
    @words = split /(\'?[^A-Za-z\']+\'?)/;
    foreach $w (@words) {
        if (@{$trans{$w}} == 1) {
            print $trans{$w}[0];
        } elsif (@{$trans{$w}} > 1 && $ques == 1) {
            print $trans{$w}[0];
        } elsif (@{$trans{$w}} > 1 && $ques == 2) {
            print '?'.join('/', @{$trans{$w}}).'?';
        } else {
            print $w;
        }
    }
}
