#!/usr/bin/perl -w
#
# $Revision: 1.2 $ $Date: 2012-07-26 17:11:49-04 $
# $Source: /home/vogelke/bin/RCS/spcheck,v $
# $Host: sys7.com $
# $UUID: 7e17f25e-e36f-3664-b267-b26f1e34c1ed $
#
#<spcheck: read output from spell, see where problems are.
# usage: spcheck file spell-output

use strict;

my $file     = shift(@ARGV) || die 'no file to check';
my $mistakes = shift(@ARGV) || die 'no spelling mistakes file';
my ($fh, $orig, $match, $pat, %errs);

# Read the mistakes.

open($fh, "$mistakes") || die "$mistakes: $!\n";
while (<$fh>) {
    chomp;
    tr/a-zA-Z0-9/ /cs;
    $errs{$_} = "$_";
}
close($fh);

# Highlight the mistakes in the original file.

open($fh, "$file") || die "$file: $!\n";
while (<$fh>) {
    chomp;
    $orig = $_;
    tr/a-zA-Z0-9/ /cs;
    $match = 0;

    foreach my $m (keys %errs) {
        if (/\s$m\s|^$m\s|\s$m$/) {
            $match++;
            $pat = $m;
            last;
        }
    }

    if ($match) {
        print "($pat)\t> ";
    }
    else {
        print "\t> ";
    }
    print "$orig\n";
}
close($fh);

exit(0);
