#!/usr/bin/perl -w
# Created by Ben Okopnik on Tue Dec 25 22:26:14 EST 2007
#
# Copyright (C) 2007, 2008 Ben Okopnik <ben@okopnik.com>
# 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.

use encoding "utf8";

my $dir = "/usr/local/share/translit";

if ($0 =~ /translit$/){
	$map = 'russian';
	die "Map '$map' not found in $dir/!\n" unless -f "$dir/$map";
}
else {
	($map) = $0 =~ /[-_](.*)$/;
	die "Incorrectly formed name; must consist of 'translit-MAP', where 'MAP' corresponds to a filename in $dir/.\n"
		unless $1 && -f "$dir/$1";
}

open Map, "$dir/$map" or die "$dir/$map: $_\n";
for (<Map>){
	chomp;
	next if /^#|^\s*$/;
	tr/'"= //d;
	my ($a, $b) = split //;
	$map{$a} = $b;
}

if (grep /^-h$|^--help$/, @ARGV){
	print "Mappings:\n\n";
	for (sort {$map{$a} cmp $map{$b}} keys %map){
		print "$_|$map{$_}\t";
		print "\n" unless ++$% % 8;
	}
	print "\n";
	exit;
}

while (<>){
	for (split//){
		s/(.)/$map{$1}||$1/ge;
		print;
	}
}

########## Cometh the POD ###################

=head1 NAME

translit - a character-for-character transliterator that uses
user-generated mapfiles

=head1 SYNOPSIS

translit [-h] [filename[s]]

'translit' can be used much like 'grep'; you can specify a file to be
transliterated as a commandline argument, you can run it without an
argument to process text typed on STDIN, or you can use it on the right
side of a pipe. Invoking it with '-h' displays the current mapping.

=head1 INSTALLATION

Create '/usr/local/share/translit/' if it does not exist. Copy the default
mapfile ('russian') into it. Copy 'translit' into some directory in your
path, e.g. '/usr/local/bin'.

=head1 MAPFILES
 
To have 'translit' work with languages other than the default
(English->UTF8 Russian), create a mapfile containing character pairs - e.g.
 
 -------- Mapfile starts here -------------------
 # This is a map for English->UTF8 Russian
 
     'A' = "А"
 'B' = "Б"
 'V' = "В"
 'G' = "Г"
 'D' = "Д"
 
 [etc.]
 ------- Mapfile ends here ----------------------

Next, copy the mapfile to '/usr/local/share/translit' (if this directory
does not exist, create it.) Last, create a symlink to the 'translit' script
with the same extension as the name of the mapfile - e.g.,
'translit-hebrew'. When invoked by that name, 'translit' will do the
appropriate mappings.

=head1 BUGS

None. Really. Would I lie to you?

If your system explodes due to your running "translit", you get to keep 
all the pieces. Plus, I promise to feel sorry for you. Or at least 
restrain myself from laughing.

Enjoy!

Ben Okopnik
ben@okopnik.com
Thu Jan 24 14:37:48 EST 2008
=cut

