* fix: convert non Shift_JIS character only
[lab.git] / misc / findsimplified.pl
1 #! /usr/bin/perl -w
2
3 use strict;
4 use warnings;
5 use utf8;
6
7 use Encode;
8 use Kanconvit;  # http://kanconvit.ta2o.net/
9
10 my $top = shift @ARGV || exit;
11 if (! -d $top) { exit; }
12
13 my $utf8 = find_encoding("utf8");
14 my $ck = Kanconvit->new();
15
16 checkdir($top);
17
18 sub match {
19     my $str = shift @_;
20     
21     $str = $utf8->decode($str);
22     my $sjis = encode("cp932", $str, Encode::FB_HTMLCREF);
23     if ($sjis =~ /&#\d{4,};/) {
24         my $jtext = $ck->conv_c2j($str);
25         if ($str ne $jtext) {
26             return $utf8->encode($jtext);
27         }
28     }
29     
30     return '';
31 }
32
33 sub checkdir {
34     my $target = shift @_;
35     
36     print STDERR "# checking '$target'\n";
37     opendir(my $dir, $target) || return $target;
38     my @entries = sort grep { !m/^(\.|\.\.)$/g } readdir($dir);
39     closedir($dir);
40     
41     my @dirs;
42     while (my $entry = shift @entries) {
43         if (my $japanese = match($entry)) {
44             print "mv '$target/$entry' \\\n";
45             print "   '$target/$japanese'\n";
46             next;
47         }
48         if (-d "$target/$entry") {
49             push @dirs, $entry;
50             next;
51         }
52     }
53     
54     while (my $entry = pop @dirs) {
55         checkdir("$target/$entry");
56     }
57 }