#!/usr/bin/env perl
#
# Output a convertion table from UCS character to iso-2022-jp character
# Copyright (C) 2006  MIRACLE LINUX CORPORATION.
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

use Encode;
use Encode::ISO2022JPMS;

sub print_ucs2mb_one_char {
    my ($handle, $ucs, $mb) = @_;
    my $ucslen = length($ucs);
    my $mblen = length($mb);

    for (my $i = 0; $i < $ucslen; $i++) {
        printf $handle "<U%04X>", ord(substr($ucs, $i, 1));
    }
    print $handle " ";
    for (my $i = 0; $i < $mblen; $i++) {
        printf $handle "\\x%02X", ord(substr($mb, $i, 1));
    }
    print $handle "\n";
}

sub dump_ucs2mb_iso2022_plane {
    my ($codeset, $plane, $escseq) = @_;
    my $state_ascii = 0;
    my $ucs;

    my $dummy = encode($codeset, "\x{3000}");
    $dummy =~ s/\x1B\(B$//;
    my $skiplen = length($dummy);

    for (my $u = 0; $u < 0x10000; $u++) {
        my $skiplen = 0;
        next if ($plane == 0 && $u >= 0xD800 && $u <= 0xDFFF);
        undef $ucs;
        if ($escseq =~ /^\x1B\([BJ]/) {
            my $dummy;
            $state_ascii = 1;
            $ucs = chr(0x3000);
            $dummy = encode($codeset, $ucs);
            $dummy =~ s/\x1B\(B$//;
            $skiplen = length($dummy);
        }
        $ucs .= chr($plane << 16 | $u);
        $mb = encode($codeset, $ucs);
        my $outesc = substr($mb, $skiplen, length($escseq));
        if ($outesc eq $escseq) {
            print_ucs2mb_one_char(STDOUT, $ucs, $mb);
	} else {
            print_ucs2mb_one_char(STDERR, $ucs, $mb);
	}
    }
}

sub dump_ucs2mb_iso2022 {
    my ($codeset, $escseq) = @_;

    dump_ucs2mb_iso2022_plane($codeset, 0, $escseq);
    dump_ucs2mb_iso2022_plane($codeset, 2, $escseq);
}

my $codeset;
my $escseq;

if ($#ARGV != 1) {
    printf STDERR "Usage: mb2ucs_iso2022 <codeset> <escape sequence>\n";
    exit 1;
}
$codeset = $ARGV[0];
if (!defined(find_encoding($codeset))) {
    print STDERR "Unknown encoding $codeset\n";
    exit 1;
}
if ($ARGV[1] =~ /^ESC/i ) {
    $escseq = $ARGV[1];
    $escseq =~ s/^ESC/\x1B/i;
} else {
   printf STDERR "%s: Illegal format.\n", $ARGV[1];
   exit 1;
}

dump_ucs2mb_iso2022($codeset, $escseq);

0;
