#!/usr/bin/perl

##########################################################################
#  Program:     conv-ru
#
#  Description: This program convert 8bit characters to TeX sequence
#               (ISO language must be specified)
#
#  Version:     0.1
#
#  Author:      Claudiu Costin <claudiuc@interplus.ro>
#
#  Date:        Feb 03, 2000
#
##########################################################################

$version  = 0.2;
$program  = 'conv-ru';
$data     = '2000-02-03';

$opt{'stdin'}     = 0;
$opt{'output'}    = 0;
$opt{'help'}      = 0;
$opt{'version'}   = 0;
$opt{'verbose'}   = 0;

@filelist         = ();
$outputfile       = '';
$language         = '';
$langlist         = '';

%diacritics = (
'\xC0', '{\\cyr yu}',
'\xC1', '{\\cyr a}',
'\xC2', '{\\cyr b}',
'\xC3', '{\\cyr c}',
'\xC4', '{\\cyr d}',
'\xC5', '{\\cyr e}',
'\xC6', '{\\cyr f}',
'\xC7', '{\\cyr g}',
'\xC8', '{\\cyr h}',
'\xC9', '{\\cyr i}',
'\xCA', '{\\cyr i0}',
'\xCB', '{\\cyr k}',
'\xCC', '{\\cyr l}',
'\xCD', '{\\cyr m}',
'\xCE', '{\\cyr n}',
'\xCF', '{\\cyr o}',
'\xD0', '{\\cyr p}',
'\xD1', '{\\cyr ya}',
'\xD2', '{\\cyr r}',
'\xD3', '{\\cyr s}',
'\xD4', '{\\cyr t}',
'\xD5', '{\\cyr u}',
'\xD6', '{\\cyr zh}',
'\xD7', '{\\cyr v}',
'\xD8', '{\\cyr p1}',
'\xD9', '{\\cyr y}',
'\xDA', '{\\cyr z}',
'\xDB', '{\\cyr x}',
'\xDC', '{\\cyr e1}',
'\xDD', '{\\cyr w}',
'\xDE', '{\\cyr q}',

'\xE0', '{\\cyr Yu}',
'\xE1', '{\\cyr A}',
'\xE2', '{\\cyr B}',
'\xE3', '{\\cyr C}',
'\xE4', '{\\cyr D}',
'\xE5', '{\\cyr E}',
'\xE6', '{\\cyr F}',
'\xE7', '{\\cyr G}',
'\xE8', '{\\cyr H}',
'\xE9', '{\\cyr I}',
'\xEA', '{\\cyr I0}',
'\xEB', '{\\cyr K}',
'\xEC', '{\\cyr L}',
'\xED', '{\\cyr M}',
'\xEE', '{\\cyr N}',
'\xEF', '{\\cyr O}',
'\xF0', '{\\cyr P}',
'\xF1', '{\\cyr Ya}',
'\xF2', '{\\cyr R}',
'\xF3', '{\\cyr S}',
'\xF4', '{\\cyr T}',
'\xF5', '{\\cyr U}',
'\xF6', '{\\cyr Zh}',
'\xF7', '{\\cyr V}',
'\xF8', '{\\cyr P1}',
'\xF9', '{\\cyr Y}',
'\xFA', '{\\cyr Z}',
'\xFB', '{\\cyr X}',
'\xFC', '{\\cyr E1}',
'\xFD', '{\\cyr W}',
'\xFE', '{\\cyr Q}',
);


# citesc parametrii din linia de comanda
while ($param = shift) {
  # long format options
  if      ($param eq '--verbose')  {
     $opt{'verbose'}   = 1;
  } elsif ($param  eq '--stdin')   {
     $opt{'stdin'}     = 1;
  } elsif ($param  eq '--help')    {
     $opt{'help'}      = 1;
  } elsif ($param  eq '--version') {
     $opt{'version'}   = 1;
  } elsif ($param  =~ '--output=') {
     $opt{'output'}    = 1;
     ($dummy, $outputfile) = split('=',$param);
  # short format options
  } elsif ($param  eq '-v') {
     $opt{'verbose'}   = 1;
  } elsif ($param  eq '-h') {
     $opt{'help'}      = 1;
  } elsif ($param  eq '-V') {
     $opt{'version'}   = 1;
  } elsif ($param  eq '-s') {
     $opt{'stdin'}     = 1;
  } elsif ($param  =~ '-o') {
     $opt{'output'}    = 1;
     $outputfile       = shift;
  } else {
     push @filelist, $param;
  }
}   

# display program version
if ($opt{'version'}) {
  print STDOUT "$program $version\n";
  exit 0;
}

# display help 
if ($opt{'help'}) {
  help();
  exit 0;
}


# check if I can read from input files
foreach $infile (@filelist) {
  if (! -r $infile) {
     if ($opt{'verbose'}) {
        print STDERR "ERROR: Cannot read from `$infile'. Check permissions.\n";
     }
     exit 1;
  }
}

# check if I can write in output file
if ($opt{'output'}) {
   if (! open(OUTPUT_FILE,">$outputfile")) {
       if ($opt{'verbose'}) {
          print STDERR "ERROR: Cannot write to `$outputfile'. Check permissions.\n";
       }
     exit 1;
   }     
}


# this will mark if read will be from STDIN or from file list
# specified on command line
$magic = '__MARKER__';
if ($opt{'stdin'}) {
  @filelist = ($magic);
}

foreach $inputfilename (@filelist) {
  # choose from which source to read
  if ($inputfilename ne $magic) {
   $inputfilehandle = INPUT_FILE ;
  } else {
   $inputfilehandle = STDIN ;
  }
  
   open($inputfilehandle, $inputfilename);
  
  # process current file line by line
  while ($textline=<$inputfilehandle>) {
     # ISO8859-2 to TeX sequence conversion
     iso2tex();
     
     # send to specified output
     if ($opt{'output'}) {
        print OUTPUT_FILE $textline;
     } else {
        print STDOUT $textline;
     }
  }

  close($inputfilehandle);
}


# if worked with real file for output then close it
if ($opt{'output'}) {
   close(OUTPUT_FILE);
}

   
# convert ISO diacritic characters to TeX sequences
sub iso2tex() {
  foreach $isochar (keys(%diacritics)) {
      $textline =~ s/$isochar/$diacritics{$isochar}/g;
  }
}


# display program usage
sub help() {
   print STDOUT << "eof";
[$program $version, $data] 
  Convert 8bit ASCII characters to TeX sequences
  acordingly with specified language. 
  
  Copyright 1999, Claudiu Costin <claudiuc\@interplus.ro>
  Released under GNU Public Licence. There\'s no warranty.

Usage: 
  conv-ru [--output=<output_file> | -o <output_file>]
          [--stdin | <input_file]
          [--verbose | -v] 
          [--version | -V] 
          [--help | -h]
  
Options:
 -o <output_file>, --output=<output_file>
         Conversion output will be write in <output_file>. Default action
	 is to write to STDOUT. Be careful, untranslated 8bit ASCII will
	 scramble your terminal if is send to tty.

 -s, --stdin
         Read input file from STDIN. Default is to read file(s) from
	 command line.

 -v, --verbose
         More details about what is happen for your file(s).
	 
 -h, --help 
         This help screen
    
 -V, --version
         Display name and version of this program
eof

   exit 0;
}
