#!/usr/local/bin/perl
#
#	uddel 1.0 - Wnn4.2$B$N%f!<%6<-=q$+$i0l3g:o=|(B
#		by endo-h@zizi.cs.uec.ac.jp
#
# $B<j=g(B:
#     1)$BI8=`F~NO$+$i:o=|$9$k%(%s%H%j$rFI$`(B
#     2)$B%f!<%6<-=q$r%7%j%"%kHV9f$D$-$G%@%s%W(B
#     3)$B:o=|%(%s%H%j$HHf3S$7$F!":o=|$9$k%7%j%"%kHV9f$rF@$k(B
#     4)$B<B:]$N:o=|$r9T$&(B
#
use Getopt::Std;

getopts('D:hd:', \%opts);

if ($opts{'h'}) {
    print STDERR "Usage: uddel [-h] [-d ud_file] [-D server] < text \n";
    print STDERR "Example: uddel -d ud2 -D localhost < delete.txt\n";
    exit(0);
}

if ($opts{'D'}) {
    $server = $opts{'D'};
} elsif ($ENV{'JSERVER'}) {
    $server = $ENV{'JSERVER'};
} else {
    $server = '';
}

$usr = $ENV{'USER'};

$dic = $opts{'d'} ? $opts{'d'} : 'ud';		# $B<-=q%U%!%$%kL>(B

$dtoa = "/usr/local/bin/Wnn4/dtoa";
$wddel = "/usr/local/bin/Wnn4/wddel";
$usrdicdir = "/usr/local/lib/wnn/ja_JP/dic/usr";

$ud = "$usrdicdir/$usr/$dic";

#
# $B%f!<%6<-=q$NCf?H$rFI$s$GO"A[G[Ns$K%9%H%"(B
#
open(DTOA, "$dtoa -s $ud|");
while (<DTOA>) {
    if (/^\d+\t+(\S+\t+\S+\t+\S+\t+)\S+/) {
	$serial{$1} = $_;
    }
}
close(DTOA);

#
# $BI8=`F~NO$+$i%j%9%H$rFI$s$G3:Ev$9$k%7%j%"%kHV9f$D$-%(%s%H%j$rCj=P(B
#
while (<>) {
    if (/^(\S+\t+\S+\t+\S+\t+)\S+/) {
	$text = $1;
#	print "[[$1]]\n";
	if ($serial{$text}) {
	    push(@del, $serial{$text});
	} else {
	    push(@unkown, $serial{$text});
	}
    }
}

#
# $B<B:]$N:o=|$r9T$&(B
#
if ($server) {
    open(WDDEL, "|$wddel -D $server  $ud");
} else {
    open(WDDEL, "|$wddel  $ud");
}
foreach (@del) {
    print WDDEL $_;
    
}
close(WDDEL);
