#!/usr/local/bin/perl -w
#-------------------------------------------------------------------------------
# convuml.pl                                     (C) 2014 T.Birnthaler OSTC GmbH
# Umlaute-Umwandlung: äöüÄÖÜß <-> aeoeueAeOeUess
# $Id: convuml.pl,v 1.5 2014/01/23 13:45:52 tsbirn Exp $
#-------------------------------------------------------------------------------
use strict;
use warnings;

# Standardmodule
use Lingua::DE::ASCII;
use Getopt::Std;
use File::Basename;

# Optionen-Variablen
my %opts    = ();
my $dbg     = 0;
my $ascii   = 0;

# Optionen einlesen
&Usage("wrong option") unless (getopts(':adhi', \%opts));

# Erkannte Optionen
$ascii = 1  if (exists $opts{"a"});
$dbg   = 1  if (exists $opts{"d"});
&Usage("")  if (exists $opts{"h"});
$^I    = "" if (exists $opts{"i"});   # in-place = Perl-Schalter "-i, kein Backup"

#-------------------------------------------------------------------------------
# Hauptprogramm
#-------------------------------------------------------------------------------
while (<>) {
	chomp($_);
	print " IN($.): $_\n" if ($dbg);

	if ($ascii) { $_ = to_ascii($_); }
	else        { $_ = to_latin1($_); }

	print "OUT($.): " if ($dbg);
	print "$_\n";
}
#-------------------------------------------------------------------------------
# Usage-Meldung + Abbruch
#-------------------------------------------------------------------------------
sub Usage
{
    my ($msg) = @_;

    *STDOUT = *STDERR;
    print "ERROR: $msg\n" if ($msg);
    print "USAGE: ", basename($0), " [OPTS] [FILE...]\n";
    print "       ", basename($0), " [OPTS] -i FILE...\n";
    print "       Intelligente Umlaut-Konvertierung in beide Richtungen.\n";
	print "       Standard: aeoeueAeOeUess --> äöüÄÖÜß\n";
    print "       (Eingabe per Dateiname(n) oder STDIN, Ausgabe auf STDOUT)\n";
    print "       (per Schalter '-i' angegebene Dateien 'in-place' editiert)\n";
    print "OPTIONEN: -a  Konvertieren nach ASCII (äöüÄÖÜß --> aeoeueAeOeUess)\n";
    print "          -d  Debugging\n";
    print "          -h  Hilfe (diese Usage-Meldung)\n";
    print "          -i  Dateien auf Kommandozeile 'in-place' konvertieren\n";
    exit(1);
}
