#!/usr/bin/perl

##########################################################################
#  Program:     conv-sl
#
#  Description: This program convert 8bit characters to TeX sequence
#               (ISO language must be specified)
#
#  Version:     0.1
#
#  Author:      Claudiu Costin <claudiuc@interplus.ro>
#  Modified by: Roman Maurer <roman.maurer@amis.net>
#
#  Date:        Feb 03, 2000
#
##########################################################################

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

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

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

%diacritics = (
  '\xE8','\\v{c}',
  '\xB9','\\v{s}',
  '\xBE','\\v{z}',
  '\xE6','\\\'{c}',
  '\xC8','\\v{C}',
  '\xA9','\\v{S}',
  '\xAE','\\v{Z}',
  '\xC6','\\\'{C}',
  '\xBA','\\c{s}',
  '\xAA','\\c{S}',
  '\xFE','\\c{t}',
  '\xDE','\\c{T}',
  '\xE3','\\u{a}',
  '\xC3','\\u{A}',
  '\xE2','\\^{a}',
  '\xC2','\\^{A}',
  '\xEE','\\^{\\i}',
  '\xCE','\\^{I}'
);

# 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-ro [--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;
}
