#! perl -wl
#
# Print out Iroha table
#
# Copyright 2016 Ken Takusagawa
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

open FI,"hiragana.txt" or die;
while(<FI>){
    die unless /^(\S+)\s+HIRAGANA LETTER (\S+)/;
    $hira{$2}=$1;
}
close FI;
open FI,"katakana.txt" or die;
while(<FI>){
    die unless /^(\S+)\s+KATAKANA LETTER (\S+)/;
    $kata{$2}=$1;
}
close FI;
open FI,"iroha.txt" or die;
while(<FI>){
    @F=split;
    for(@F){
        for(uc$_){
            push @iroha,$_;
            die $_ unless defined($hira{$_});
            die $_ unless defined($kata{$_});
        }}}
print'<p>';
for(@iroha){
    printf " %d",&dec($kata{$_});
    #,' ',&dec($kata{$_})-&dec($hira{$_});
}
print'</p>';
for ('A'..'Z') {
    next if $_ eq 'I';
    push @go_coordinates,$_;
}
for(1..100){
    push @go_coordinates,""; #pad
}
#10 stored as 0
@numerals_hex=qw/5341 4E00 4E8C 4E09 56DB 4E94 516D 4E03 516B 4E5D/;
for(@numerals_hex){
    push @numerals,("&#".&dec($_).";");
}
print qq(<table border="1">);
print "<tr><th>Iroha</th><th>Num</th><th>Jnum</th><th>Hira</th><th>Hira decimal</th><th>Hira hex</th><th>Kata</th><th>Kata decimal</th><th>Kata hex</th><th>Go</th><th>Zero</th></tr>";
for($i=0;$i<@iroha;++$i){
    $v=$iroha[$i];
    $iindex{$v}=$i;
    print "<tr>"
        ,"<td>$v</td>"
        ,"<td>",$i+1,"</td>"
        ,"<td>",&japanese_number($i+1),"</td>"
        ,"<td>&#",&dec($hira{$v}),";</td><td>",&dec($hira{$v}),"</td><td>U+",$hira{$v},"</td>"
        ,"<td>&#",&dec($kata{$v}),";</td><td>",&dec($kata{$v}),"</td><td>U+",$kata{$v},"</td>"
        ,"<td>$go_coordinates[$i]</td>"
        ,"<td>$i</td>"
        ,"</tr>";
}
print "</table>";
print qq(<table border="1">);
print "<tr><th>Iroha</th><th>Num</th><th>Jnum</th><th>Hira</th><th>Hira decimal</th><th>Hira hex</th><th>Kata</th><th>Kata decimal</th><th>Kata hex</th><th>Go</th><th>Zero</th></tr>";
for$v(sort {&dec($hira{$a}) <=> &dec($hira{$b})} @iroha){
    $i=$iindex{$v};
    print "<tr>"
        ,"<td>$v</td>"
        ,"<td>",$i+1,"</td>"
        ,"<td>",&japanese_number($i+1),"</td>"
        ,"<td>&#",&dec($hira{$v}),";</td><td>",&dec($hira{$v}),"</td><td>U+",$hira{$v},"</td>"
        ,"<td>&#",&dec($kata{$v}),";</td><td>",&dec($kata{$v}),"</td><td>U+",$kata{$v},"</td>"
        ,"<td>$go_coordinates[$i]</td>"
        ,"<td>$i</td>"
        ,"</tr>";
};
print "</table>";
sub dec {
    my $s=shift;
    unpack "n",(pack "H*",$s);
}
sub japanese_number {
    my $i=shift;
    my $out;
    $ones=$i % 10;
    $tens=($i-$ones)/10;
    if($tens){
        if($tens>1){
            $out=$numerals[$tens];
        }
        $out.=$numerals[0];  #ten;
    }
    if($ones){
        $out.=$numerals[$ones];
    }
    $out;
}
