#!/usr/bin/perl

##########################################################################
#  Program:     texconv
#
#  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.3;
$program  = 'texconv';
$data     = '2000-01-23';

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

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


# citesc parametrii din linia de comanda
while ($param = shift) {
  # long format options
  if      ($param eq '--verbose')  {
     $opt{'verbose'}   = 1;
  } elsif ($param  eq '--lang=')   {
     $opt{'lang'}      = 1;
     ($dummy, $language) = split('=',$param);
  } 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 '-l') {
     $opt{'lang'}      = 1;
     $language = shift;
  } 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 have ISO - TEX file table
if ($opt{'lang'}) {
  if (! -r "$language.iso2tex") {
     if ($opt{'verbose'}) {
        print STDERR "ERROR: Cannot read language file `$language.iso2tex'.\n";
     }
     exit 1;  
  } else {
    require "$language.iso2tex";
  }
} else {
  if ($opt{'verbose'}) {
     print STDERR "ERROR: Language not specified. You must do this.\n";
  }
  exit 1;
}


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

sub load_iso2tex() {
  open($iso2texfile, "$language.iso2tex");
  
  close($iso2texfile);
}

# 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: 
  texconv [--lang=<language> | -l <language>]
          [--output=<output_file> | -o <output_file>]
          [--stdin | <input_file]
          [--verbose | -v] 
          [--version | -V] 
          [--help | -h]
  
Options:
 -l <language>, --lang=<language>  
         Specify coding of input file(s). <language> must be in ISO
	 format; e.g.: us,de,fr etc. Default is US, that mean no
	 conversion will be done.
 -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;
}
