#!/usr/local/ymir/perl/bin/perl -w
## ----------------------------------------------------------------------------
#  ujconv
# -----------------------------------------------------------------------------
# Mastering programmed by YAMASHINA Hio
#
# Copyright 2005 YAMASHINA Hio
# -----------------------------------------------------------------------------
# $Id: ujconv,v 1.3 2007/01/16 02:01:36 hio Exp $
# -----------------------------------------------------------------------------
package Unicode::Japanese::UJConv;
use strict;
use Unicode::Japanese;
our $VERSION = '0.02';

if( !caller )
{
  __PACKAGE__->do_work(@ARGV);
}

# -----------------------------------------------------------------------------
# main.
#
sub do_work
{
  my $pkg = shift;
  
  my $from = 'auto';
  my $to = 'auto';
  my $string;
  my @files;
  
  while(@_)
  {
    my $key = shift;
    if( $key !~ /^-/ )
    {
      push(@files,$key);
      next;
    }elsif( $key eq '--' )
    {
      push(@files,@_);
      last;
    }
    if( $key eq '-f' )
    {
      $from = shift;
      next;
    }elsif( $key eq '-t' )
    {
      $to = shift;
      next;
    }elsif( $key eq '-s' )
    {
      my $value = shift;
      push(@files,[$key,$value]);
      next;
    }elsif( $key =~ /^(-h|--help)$/ )
    {
      print_usage();
      return 1;
    }elsif( $key =~ /^(-V|--version)$/ )
    {
      print_version();
      return 1;
    }elsif( $key =~ /^(-l|--list)$/ )
    {
      print_list();
      return 1;
    }else
    {
      die "unkown argument [$key]";
    }
  }
  
  if( $to eq 'auto' )
  {
    my $lang = $ENV{LANG};
    if( $lang && $lang=~/\.(.*)/ )
    {
      my $code = $1;
      if( $code=~/^(ujis|jis|iso-2022-jp)$/i )
      {
        $to = 'jis';
      }elsif( $code=~/^(ujis|eucJP)$/i )
      {
        $to = 'euc';
      }elsif( $code=~/^(sjis|shift_?jis)$/i )
      {
        $to = 'sjis';
      }elsif( $code=~/^(utf-?8)$/i )
      {
        $to = 'utf8';
      }
    }
    if( $to eq 'auto' )
    {
      $to = $^O eq 'MSWin32' ? 'sjis' : 'euc';
    }
  }
  
  local($/) = undef;
  if( !@files )
  {
    my $text = <STDIN>;
    print Unicode::Japanese->new($text,$from)->conv($to);
  }
  foreach my $file (@files)
  {
    my $text;
    if( ref($file) )
    {
      $text = $file->[1];
    }elsif( $file eq '-' )
    {
      $text = <STDIN>;
    }else
    {
      open(FILE,$file) or die "could not open file [$file] : $!";
      $text = <FILE>;
      close(FILE);
    }
    print Unicode::Japanese->new($text,$from)->conv($to);
  }
  1;
}

# -----------------------------------------------------------------------------
# print_usage();
#
sub print_usage
{
  print "usage: ujconv [-f from_encode] [-t to_encode] [-s string] [files...]\n";
  print "see \`perldoc ujconv' for detail.\n";
}

# -----------------------------------------------------------------------------
# print_version();
#
sub print_version
{
  print "ujconv $VERSION\n";
  print "Unicode::Janaese $Unicode::Japanese::VERSION\n";
}

# -----------------------------------------------------------------------------
# print_list();
#
sub print_list
{
  foreach my $enc (qw(
      utf8
      ucs2
      ucs4
      utf16
      jis
      euc
      euc-jp
      sjis
      cp932
      sjis-imode
      sjis-doti
      sjis-jsky
      jis-jsky
      jis-au
      sjis-icon-au
      euc-icon-au
      jis-icon-au
      utf8-icon-au
  ))
  {
    print "$enc\n";
  }
}

__END__

=head1 NAME

ujconv -- iconv(1), reinvented in Unicode::Japanese

=head1 SYNOPSIS

  ujconv [-f from_encoding] [-t to_encoding] [-s string] [files...]
  ujconv -l
  ujconv -h
  ujconv -V

=head1 VERSION

ujconv 0.02

=head1 DESCRIPTION

B<ujconv> is Unicode::Japanese (written in perl) version of B<iconv>.

ujconv converts the character encoding of either STDIN or files
specified in the argument and prints out to STDOUT.

Here is the list of options.  Each option can be in short format (-f)
or long (--from).

=over 4

=item -f,--from from_encoding

Specifies the encoding you are converting from.  Unlike B<iconv>,
this option can be omitted.  In such cases, source encoding is 
automatically guessed.

=item -t,--to to_encoding

Specifies the encoding you are converting to. 

=item -s,--string I<string>

uses I<string> instead of file for the source of text.

=item -l,--list

Lists all available encodings, one per line.

=item -h,--help

Show usage.

=back

=head1 SEE ALSO

L<Unicode::Japanese>,
L<piconv(1)>,
L<iconv(1)>,
L<ujguess>

=cut

# -----------------------------------------------------------------------------
# End of File.
# -----------------------------------------------------------------------------
