#!/usr/local/bin/perl
#
#	supcheck 1.2 - supplement$B$N;HMQ>u67$r%A%'%C%/$9$k6=L#K\0L$J%3%^%s%I(B
#		by endo-h@remus.dti.ne.jp
#
use Getopt::Std;

getopts('hsSu:', \%opts);

if ($opts{'h'}) {
    print "Usage: supcheck [-h] [-s] [-S] [-u user]\n";
    exit(0);
}

$usr = $opts{'u'} ? $opts{'u'} : $ENV{'USER'};	# $B%f!<%6;XDj(B

$dtoa = "%WNNBINDIR%/dtoa";
$supdicdir = "%DESTDIR%";
$usrdicdir = "%JWNNDICDIR%/usr/$usr";
$dic = "$supdicdir/supplement.dic";

open(MOD, "$dtoa $dic $usrdicdir/supplement.h|");
open(ORG, "$dtoa $dic|");

$f = $i = $j = 0;
while (!eof(MOD)) {
    $i++;
    $mod = <MOD>;
    $org = <ORG>;
    if ($mod ne $org) {		# $BIQEY>pJs$,99?7$5$l$F$$$k(B
	$j++;
	next if ($opts{'s'});
	$mod =~ /^\S+\t+\S+\t+\S+\t+(\d+)/;
	$mfreq = $1;
	$org =~ /^(\S+\t+\S+\t+\S+\t+)(\d+)/;
	$key = $1;
	$ofreq = $2;
	$delta = $mfreq - $ofreq;
	$f += $delta if ($opts{'S'});
	print "$key$delta\n" unless ($opts{'S'});
    }
}
close(MOD);
close(ORG);

if ($opts{'s'} || $opts{'S'}) {
    $i -= 4;	# header$B$NJ,$r0z$/(B
    $r = 100 * $j / $i;
    printf "%d/%d %#3.1f%\n" ,$j, $i, $r if ($opts{'s'});
    printf "%d/%d %#3.1f% $f\n" ,$j, $i, $r, $f if ($opts{'S'});
#    print "total = $total_freq\n";
}
