#!/usr/local/bin/perl5

$target = "bushu.select";
$target = shift if @ARGV;

%num = (qw(0  1  2  3  4  5  6  7  8  9  ,  .  / ),
	"\'", "");

open (STDOUT, "|nkf -s");
$\ = "\r\n";

$CH = "(?:[!-~]|[\241-\376][\241-\376])";

# ܥȥɤࡣΤޤ޽Ϥ롣

open (IN, "tcode.txt");
while (<IN>) {
    chomp;
    ($s, $c) = /^($CH+) ($CH)/o;
    push(@tchars, $c);
    (print), next if $c eq '';
    $stroke_code{"$s"} = $c;
    $code_stroke{$c} = "$s";
    if ($c eq '') {
	$jf = $c;
	next;
    } elsif ($c eq '') {
	$fj = $c;
	next;
    }
    
    print "$s $c";
}
close (IN);

# ѴμФ

print "";
open (IN, $target);
while (<IN>) {
    chomp;
    my ($f, $s, $c) = /^($CH)($CH)($CH)/o;
    my ($cf, $cs);
    $cf = $code_stroke{$f};
    $cs = $code_stroke{$s};

    if ($cs) {
	$used{$f}++;
	$f =~ s|([0-9\',./])|$num{$1}|ge;
	print "$jf$f$cs $c";
    }
}
close (IN);


# Ѵγϥ󥹤Ф

print "";
print $code_stroke{$jf}, " \\0 $jf";

for $c (@tchars) {
    next unless $used{$c};
    (print ""), next if $c eq '';
    next if $c eq $jf || $c eq $fj;

    if ($code_stroke{$c}) {
	my ($c2) = $c;
	$c2 =~ s|([0-9\',./])|$num{$1}|ge;
	print $jf, $code_stroke{$c}, " \\0 $jf$c2";
    }
}

