#!/usr/bin/perl
# This script is in the public domain.
# Usage: "jouyou.pl <kanjidic"
while ($line = <STDIN>) {
    if (substr ($line, 0, 1) eq "#") {
	next;
    }
    chomp ($line);
    $jouyou = 0;
    $sh = "";
    @toks = split (/\s+/, $line);
    foreach $tok (@toks) {
	if (substr ($tok, 0, 2) eq "IN") {
	    $jouyou = 1;
	} elsif (substr ($tok, 0, 1) eq "I") {
	    $sh = substr ($tok, 1);
	}
    }
    if ($jouyou) {
	$sh =~ /^(1?[0-9])([a-z])/;
	$number = $1;
	$letter = $2;
	$radical = "$1$2";
	$byradical{$radical} = [$toks[0] . " " . $sh, $byradical{$radical}];
	$bysh{$sh} = $toks[0];
	print STDERR "#";
    }
}
print STDERR "\n";

foreach $number ("0", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11") {
    foreach $letter ("a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
		     "k", "l", "m", "n", "o", "p", "q", "r", "s", "t") {
	$radical = $number . $letter;
	if ($byradical{$radical}->[0]) {
	    print "$radical:\n";
	    for ($stroke = 0; $stroke < 30; ++$stroke) {
		for ($seqn = 0; $seqn < 50; ++$seqn) {
		    $sh = $radical . $stroke . "." . $seqn;
		    if ($bysh{$sh}) {
			print "   " . $bysh{$sh} . $sh . "\n";
		    }
		}
	    }
	}
#	    $i = $byradical{$radical}->[0];
#	    $n = $byradical{$radical}->[1];
#	    while ($i) {
#		print "   $i\n";
#		$i = $n->[0];
#		$n = $n->[1];
#	    }
#	}
    }
}
