#! /usr/bin/perl
#
# Samba Multibyte companion tool
# copyright (c) 2000, Hiroshi MIURA 
# All rights reserved by Hiroshi MIURA
#
#
# Changelog
#   * Oct. 25, 2000 Ver.1.2 Hiroshi MIURA
#       - fix CAP encoding bug with the code 0x80 and 0x2f(/)
#             This is reported by  HIROSE, Masaaki.
#
#   * Aug. 20, 2000 Ver.1.1 Hiroshi MIURA
#     - Shape up codes (delete unused codes)
#     - Support multiple file names and options
#
#   * Aug. 16, 2000 Ver.1.0 Hiroshi MIURA
#     - First release
#     - Based on smbchartool V.1.5

use File::stat;

# now, I use jcode.pl, does someone knows how to use Jcode.pm??
#
require "jcode.pl";

# -----------------------------------------
# definition of important table and constants, regex.
#
@sjisreg = (
0x8470, 0x847e, 0x8440,
0x8754, 0x8754, 0xfa4a,
0x8782, 0x8782, 0xfa59,
0x8784, 0x8784, 0xfa5a,
0x878a, 0x878a, 0xfa58,
0x8790, 0x8790, 0x81e0,
0x8791, 0x8791, 0x81df,
0x8792, 0x8792, 0x81e7,
0x8795, 0x8795, 0x81e3,
0x8796, 0x8796, 0x81db,
0x8797, 0x8797, 0x81da,
0x879a, 0x879a, 0x81e6,
0x879b, 0x879b, 0x81bf,
0x879c, 0x879c, 0x81be,
0xed40, 0xed62, 0xfa5c,
0xed63, 0xed7e, 0xfa80,
0xed80, 0xede0, 0xfa9c,
0xede1, 0xedfc, 0xfb40,
0xee40, 0xee62, 0xfb5c,
0xee63, 0xee7e, 0xfb80,
0xee80, 0xeee0, 0xfb9c,
0xeee1, 0xeeec, 0xfc40,
0xeeef, 0xeef8, 0xfa40, 
0xeef9, 0xeef9, 0x81ca,
0xfa54, 0xfa54, 0x81ca,
0xfa6b, 0xfa6b, 0x81e6
);
$sjisreglen=($#sjisreg+1)/3-1;
 
%match = (
 SJIS_C    => '[\x80-\x9f\xe0-\xfc][\x40-\xfc]',
 SJIS_KANA => '[\xa1-\xdf]',
 SJIS_O    => '[\x2f\x80-\xfc]',
 SJIS_S    => '[\x81-\x9f\xe0-\ec][\x40-\xfc]',
 SKOS_D    => '[\xed-\xfc][\x40-\xfc]'
);


#---------------------------------------------
# check command name which it called.
# 
$comn=$0;
$comn=~s|(.*/)*(.+)|$2|;

## who am I? :)
## definition of mb convert 

if     ($comn eq "hexecho"){
    $convmb= sub {
	local(*nname) = @_;
	my $name=$nname;
        jcode::euc2sjis(*nname);
        &sjis2rsjis(*nname);
        $nname =~ s/($match{SJIS_C})/sprintf ":%2x:%2x",ord($1),ord(substr($1,1))/geo;
        $nname =~ s/($match{SJIS_KANA})/sprintf ":%2x",ord($1)/geo; 
    };

 }elsif ($comn eq "capecho"){
    $convmb= sub {
	local(*nname) = @_;
        my $name=$nname;
        jcode::euc2sjis(*nname);
	&sjis2rsjis(*nname);
	$nname=~s/($match{SJIS_O})/sprintf ":%2x",ord($1)/geo;
      };

 }else {
    &Show_Usage();
    exit 1;
}

#========================================================
# main routine.
#
$string=join " ",@ARGV;
&$convmb(*string);
print $string;
exit;

#===========================================================
# subroutines.
#
#---------------------------------------------------------
#
# convert regular sjis to regular sjis.
# using binary snearch method.
# 
sub sjis2rsjis {
    local(*line) = @_;
    $line =~ s/($match{SJIS_C})/&s2r($1)/geo;
}

sub s2r {
    local($char) = @_;
    $mb=ord($char)*256 + ord(substr($char,1));
    return $char if ($mb < $sjisreg[0]);	# checkk if it is 
    if ($sjisreg[$sjisreglen*3+1] < $mb){	# target to conv?
	$char=$GETA if ($mb > 0xfc4b);
	return $char;
    };
    $min=0;$max=$sjisreglen;		# OK is will be conv
    while($max >= $min){
	    $j=$min+($max-$min)%2;
	    if ($sjisreg[$j*3+0] > $mb){		 
		$max=$j-1;
	    } elsif ($mb > $sjisreg[$j*3+1]) {
		$min=$j+1;
	    } else {					# hit it!
		$mb=$sjisreg[$j*3+2]+$mb-$sjisreg[$j*3+0];
		$char=sprintf "%c%c",($mb >> 8) & 0xff, $mb & 0xff;
		break;
	    }
	} 
$char;
}


#---------------------------------------------------------

sub Show_Usage {
    print "This is samba companion tool \n";
    print "Usage: $comn [-h] strings \n";
    print "-h: show this help message\n";
exit;
}