#!/usr/bin/perl -w

eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
    if 0; # not running under some shell
	eval 'exec perl -S $0 "$@"'
		if 0;


use vars qw( $running_under_some_shell );

=head1 NAME

vntovn -- charset encoding convertor for Vietnamese.

=head1 FORMAT

	vntovn [options] src_encoding dst_encoding [files ...]

=head1 SYNOPSIS

	vntovn viqr tcvn < file | more

or

	vntovn viscii vni infile > outfile

Please see the

	vntovn --help

for short usage info.

=head1 DESCRIPTION

C<vntovn> is a simple conversion utility to change charset encoding of a
text. It reads either specified files or (if none specified) the standard
input, assumes that the input is encoded in C<src_encoding> and ties to
reencode it into C<dst_encoding>. The result is written to the standard
output. Characters that are not defined in C<src_encoding> are passed to
the output unchanged.

Run C<vntovn> without parameters to get short help and list of
available encodings.

For more detailed description of C<vntovn> please have a look at the
manual of C<cstocs>, from which C<vntovn> was derived.

=head1 SEE ALSO

cstocs(1).

=head1 AUTHOR

Jan "Yenya" Kasprzak has done the original Un*x implementation of C<cstocs>.

Jan Pazdziora <adelton@fi.muni.cz> created the Perl module version of C<cstocs>.

Han The Thanh <thanh@fi.muni.cz> added the support for Vietnamese.

=cut

use strict;
use Cz::Cstocs;

sub usage {
	print_version();
	print STDERR <<EOF;
Usage: vntovn [options] inputencoding outputencoding [ files ... ]
  where [options] can be
    --dir=string	Alternate directory with encoding and accent files.
    --fillstring=str	String that will replace unconvertable characters.
    --null		Equivalent to --fillstring=""
    --nofillstring	Keep unconvertable unconverted.
    --nochange, --noaccent	Do not use accent file at all.
    --onebyone		Use only one-by-one character rules from accent file.
    --onebymore		Use all entries from the accent file.
    --version		Print out the version information.
    --debug		Print out debugging info while processing.
Input and output encodings can also be specified using --inputencoding
and --outputencoding options. See man page for detailed description.
Encodings available are:
EOF
	print STDERR "\t@{[ &Cz::Cstocs::available_enc() ]}\n";
	exit;
}

sub print_version
	{
	print STDERR "This is vntovn version $Cz::Cstocs::VERSION.\n";
	}

# Out defaults are
my %options = ();
my $vnencdir;
$vnencdir = $INC{'Cz/Cstocs.pm'};
$vnencdir =~ s!\.pm$!/vnenc!;
$Cz::Cstocs::cstocsdir = $vnencdir;

if (grep { /--/ } @ARGV)
	{
	require Getopt::Long;
	Getopt::Long::GetOptions(

		'null' =>	sub { $options{'fillstring'} = ''; },
		'fillstring=s' =>	\$options{'fillstring'},
		'nofillstring' =>	sub { $options{'nofillstring'} = 1 },
		'usefillstring' =>	sub { $options{'nofillstring'} = 0 },

		'onebyone' =>	sub { $options{'one_by_one'} = 1; },
		'onebymore' =>	sub { $options{'one_by_one'} = 0; },

		'noaccent',	sub { $options{'use_accent'} = 0; },
		'nochange',	sub { $options{'use_accent'} = 0; },

		'dir=s' =>	\$options{'cstocsdir'},

		'inputencoding=s' =>	\$options{'inputenc'},
		'outputencoding=s' =>	\$options{'outputenc'},

		'help'	=>	\&usage,
		'version' =>	sub { print_version(); exit 0; },
		'debug' =>	sub { $Cz::Cstocs::DEBUG = 1; },

		);
	}

elsif (@ARGV < 2)
	{ usage(); }

my ($inputenc, $outputenc);
if (defined $options{'inputenc'})
	{ $inputenc = $options{'inputenc'}; delete $options{'inputenc'}; }
else
	{ $inputenc = shift; }

if (defined $options{'outputenc'})
	{ $outputenc = $options{'outputenc'}; delete $options{'outputenc'}; }
else
	{ $outputenc = shift; }

my $tag;
for $tag (keys %options)
	{ delete $options{$tag} unless defined $options{$tag}; }

print STDERR "Calling Cz::Cstocs $inputenc, $outputenc\n" if Cz::Cstocs::DEBUG;
my $convert = new Cz::Cstocs $inputenc, $outputenc, %options;
if (not defined $convert) {
	print STDERR $@;
	exit(1);
	}

while (<>)
	{ print &$convert($_); }

