#! /usr/bin/perl
#
# $Id: trans.in,v 2.5 2003/08/05 13:41:18 luckmann Rel $
#
# User Agent: Lynx/2.8.5rel.1 libwww-FM/2.14


# warnings on
$^W = 1;

use vars qw( $VERSION );
$VERSION = '2.0.1';


# Additional module path
#
use lib qw( .  );


# Pragmas
#
use strict;
use integer;


# Modules
#
use Getopt::Long 2.24 qw( :config posix_default bundling no_ignore_case );
use Locale::gettext;
use Net::Dict::Leo 0.03 qw( :DEBUG );
use Pod::Usage;
use POSIX qw( locale_h );
use Term::Size;

setlocale(LC_MESSAGES, "");
bindtextdomain "leo", $ENV{TEXTDOMAINDIR}||'/usr/share/locale';
textdomain("leo");

my %opts = ();
GetOptions( \%opts, "s|spelltolerance=s", "u|umlaut=s",
    "w|width:i", "d|debug", "h|help", "V|version" ,"g|g2e", "e|e2g" );


# help
defined $opts{h} and $opts{h}
    and pod2usage exitval => 0;

# version
if ( defined $opts{V} and $opts{V} ) {
    print STDERR "$VERSION\n";
    exit;
}

# standard query
my %query = ( search => "@ARGV" );

# monodirectional search
defined $opts{e} and $opts{e}
    and $query{searchLoc} = -1;

defined $opts{g} and $opts{g}
    and $query{searchLoc} = 1;


# spelling tolerance settings
if ( defined $opts{s} and $opts{s} ) {
    $opts{s} eq "high"
	and $opts{s} = "on";
    $opts{s} =~ /^(on|standard|off)$/
	or pod2usage exitval => 2, verbose => 1;
    $query{spellToler} = $opts{s};
}


# umlaut tolerance settings
if ( defined $opts{u} and $opts{u} ) {
    $opts{u} =~ /^(exact|relaxed|fuzzy)$/
	or pod2usage exitval => 2, verbose => 1;
    $query{cmpType} = $opts{u};
}


unless ( defined $opts{d} and $opts{d} ) {
    my @pairs = leo_translate %query;

    if ( $#pairs == -1 ) {
	print gettext("No entry found."), "\n";
	exit
    }

    my @colwidth = map { ($_, $_) } (Term::Size::chars *STDOUT{IO});
    if ( defined $opts{w} ) {
	@colwidth = map { ($_, $_) } (($opts{w}) ? $opts{w}/2-2 : 39);
    }
    elsif ( $colwidth[0] == 0 ) {
	@colwidth = (8, 8);
	foreach my $p ( @pairs ) {
	    for my $j ( 0..1 ) {
		length $p->[$j] > $colwidth[$j]
		    and $colwidth[$j] = length $p->[$j];
	    }
	}
    }
    else {
	map { ($_ /= 2) -= 2 } @colwidth;
    }

    my ($english, $german) = (gettext("English") . ":", gettext("German") . ":");
    my $format = "format = \n" . '^' . "<"x$colwidth[0] . '~~^' . "<"x$colwidth[1]
	. "\n" . '$english, $german' . "\n.\n";
    eval $format;

    write;
    $english = $german = "-" x 8;
    write;

    foreach ( @pairs ) {
	($english, $german) = @$_;
	write;
    }
}
else {
    print leo_debug(%query), "\n";
}


__END__


=head1 NAME

leo - Translate english into german words and vice versa

=head1 SYNOPSIS

B<leo> [I<options>] word

=head1 DESCRIPTION

B<leo> is a simple yet useful program for all those of you who
don't like taking their dictionary and browsing through it.  B<leo>
does this job for you with just a few keystrokes.  B<leo> accepts
a word--either english or german--and returns all translations
it finds.

=head1 OPTIONS

=over 8

=item B<-g>, B<--g2e>

German to English translation only.  Default is bidirectional search.

=item B<-e>, B<--e2g>

English to German translation only.  Default is bidirectional search.

=cut

#=item B<-m>, B<--morph>
#
#Whether to extend the search to morphologic base forms of the word.
#Accepted values are (see Net::Dict::Leo(3)):
#
#=over 8
#
#=item none
#
#No attempt to find a base word.
#
#=item standard
#
#(Default) Only find base forms if no or few results are returned
#from the original search. The exact meaning of "few" is hidden
#within Leo's internals.
#
#=item force
#
#Find base forms in any case.
#
#=back

=item B<-u> I<value>, B<--umlaut> I<value>

Determines how strictly the input of umlauts is handled.
The accepted values are:

I<exact>: Only E<Auml>, E<Ouml>, E<Uuml>, E<auml>, E<ouml>, E<uuml>
are accepted.

I<relaxed>: (Default) In addition, the character sequences Ae, Oe,
Ue, ae, oe, ue, ss are accepted as well.

I<fuzzy>: Even the characters A, O, U, a, o, u are accepted for
the respective umlauts.


=item B<-w> [num], B<--width> [num]

This flag overrides the default output width (which is your terminal
width or unlimited if using output redirection).

=item B<-h>, B<--help>

Just drop a short help message.

=item B<-V>, B<--version>

Nothing more than the version.

=item B<-d>, B<--debug>

If you think B<leo>' rendering engine is broken, you might want
to use this switch which gives you the plain, unprocessed HTML code.

=head1 NOTES

As B<leo> only queries the online dictionary at
F<http://dict.leo.org/>, its scope is limited by the scope of
leo's dictionary.

=head1 BUGS

Presently none known.  :-)

Please send bug reports to Carsten Luckmann
<I<leo@luckmann.name>>.

=head1 SEE ALSO

perl(1), Net::Dict::Leo(3)

=head1 AUTHOR

Carsten Luckmann <I<leo@luckmann.name>>

=cut


######################################################################
#
# Change Log
#
######################################################################
#
# $Log: trans.in,v $
# Revision 2.5  2003/08/05 13:41:18  luckmann
# * Did not handle locales C and POSIX correctly.
#
# Revision 2.4  2003/06/02 15:38:28  luckmann
# * $^W for warnings make -w switch superfluous.
#
# Revision 2.3  2003/03/04 15:16:13  luckmann
# * Support for umlaut tolerance added.
# * Removed morphology search.
#
# Revision 2.2  2003/02/19 14:04:38  luckmann
# * Flag for mono-/bi-directional search added.
# * Flag for morphology search added.
#
# Revision 2.1  2002/05/24 14:45:48  luckmann
# * Completely rewritten.  The request and parsing is now done in
#   a module called Net::Dict::Leo.  This script provides only the
#   user interface.
#
# Revision 1.15  2002/02/13 16:17:42  luckmann
# * Repeated <font> tags now work.  (really??)
#
# Revision 1.14  2001/09/19 14:45:23  luckmann
# * Leo added fourth column.  Fixed.
#
# Revision 1.13  2001/09/13 12:08:49  luckmann
# * Lines are now split instead of cut off if an entry does not fit
#   on a single line.
# * Output width bug when using pipe fixed.
# * Simplified custom user agent code.
# * --version output broken, fixed.
#
# Revision 1.12  2001/08/17 11:44:14  luckmann
# * Calculation of column width had an error.  Fixed.
# * Improved some phrases in the man page.
#
# Revision 1.11  2001/08/16 11:16:44  luckmann
# * Some variables used before definition.  Fixed.
#
# Revision 1.10  2001/08/16 10:07:54  luckmann
# * Now recognizes the terminal width, use Term::Size for it, dynamic format.
# * use integer pragma.
#
# Revision 1.9  2001/08/09 13:57:15  luckmann
# * Now use Getopt::Long for command line arguments.
# * New options -h|--help and -V|--version.
# * Optimized regular expressions.
# * Convert &reg; to .
# * Made $VERSION global variable.
#
# Revision 1.8  2001/07/13 09:00:16  luckmann
# * Leo added Hyperlinks to his output.  Adapted rendering engine.
#
# Revision 1.7  2001/07/10 09:17:09  luckmann
# * Fixed rendering engine.
# * Fixed handling of additional perl module path
#   (did not work if it was empty)
# * Write the word `trans' in the manpage boldly.
#
# Revision 1.6  2001/05/23 14:46:32  luckmann
# Yesterday's version still had bugs.  Fixed.
#
# Revision 1.5  2001/05/22 15:06:49  luckmann
# Leo changed its code.  Adapted.
#
# Revision 1.4  2001/01/31 13:53:22  luckmann
# * Corrected version string quoting.
#
# Revision 1.3  2001/01/25 15:26:17  luckmann
# * Changed rendering engine to perl formats.
#
# * Removed superfluous subroutine list2form.
#
# * Added POD documentation.
#
# Revision 1.2  2000/11/03 10:35:58  luckmann
# Trans produced error messages if no matches were found.
# Now it clearly states that no matches were found.
#
# Revision 1.1  2000/11/02 16:17:55  luckmann
# Initial revision.
#
######################################################################
#
# vim: set filetype=perl:
