#!/usr/bin/perl -w

use strict;
use warnings;

use LWP::Simple;
use Getopt::Long;
use Pod::Usage;
use File::Basename;

binmode STDOUT, ":encoding(UTF-8)";

sub usage;

my $direction = 'encz.en';
my $lines = 25;
my $max_lines = 50;
my $long = 0;
my $man = 0;
my $help = 0;
my $host = 'www.slovnik.cz';

GetOptions(
    'long|l'     => \$long,
    'lines|n=i'     => \$lines,
    'direction|d=s' => \$direction,
    'help|?|h'    => \$help,
    'man'       => \$man
) or pod2usage(2);
pod2usage(1) if $help;
pod2usage(-exitstatus => 0, -verbose => 2) if $man;

$lines = $max_lines if $long;

my $word = shift or usage;

my $query  = sprintf(
    '/bin/mld.fpl?vcb=%s&dictdir=%s&lines=%s',
    $word,
    $direction,
    $lines
);

my $url = "http://$host$query";
my @lines = split m|\n|, get($url);
@lines = grep m|class="pair"|, @lines;

foreach (@lines) {
    s/<.*?>//g;
    print;
    print "\n";
}

sub usage {
    printf STDERR "usage: %s [options] word\n", basename($0); exit 1;
}

__END__

=head1 NAME

se - Translate en-* to/from cs-CZ and many other languages using
www.slovnik.cz service

=head1 SYNOPSIS

B<se> [I<OPTIONS>] I<WORD>

=head1 DESCRIPTION

B<se> will take English word, compose a query to dictionary service at
http://www.slovnik.cz/, fetch the English - Czech pairs and display them
to stdout.

Although English to Czech is default, using B<--direction> parameter
you can translate Czech to English or even between Czech and many other
languages.

For I<WORD> with diacritics, you can provide it

=over 8

=item * directly if your terminal supports it (tested with few Czech words
on Debian 6.0 with urxvt)

=item * directly with diacritics stripped, or

=item * using special notation described here:
http://www.slovnik.cz/alternative_chars.html

Few simple examples:

=over 8

=item * C<c<erti/k> - c with caron, i with acute

=item * C<pu@da> -- u with ring above

=item * C<Bu:ro> -- u with diaeresis

=item * C<ce\ramique> -- e with grave

=item * C<valdepen~as> -- n with tilde

=back

=back

=head1 OPTIONS

=over 8

=item B<-d> I<DIR>, B<--direction=>I<DIR>

Direction of translation.  Aside from the default encz.en, it can be
one of following:

    encz.en, encz.cz, gecz.ge, gecz.cz, frcz.fr, frcz.cz,
    itcz.it, itcz.cz, spcz.sp, spcz.cz, rucz.ru, rucz.cz,
    lacz.la, lacz.cz, eocz.eo, eocz.cz, eosk.eo, eosk.sk,
    plcz.pl, plcz.cz

=item B<-l>, B<--long>

Same as C<--lines=50>, which is maximum supported by API

=item B<-n> I<LINES>, B<--lines=>I<LINES>

Number of lines fetched fronm the service and displayed to stdout.
Default is 25, but since www.slovnik.cz provides simplistic interface
with only one term per line, so for words with lot of synonyms, you
might want to specify more.

=item B<--help>

Prints a brief help message and exits.

=item B<--man>

Prints the manual page and exits.

=back

=head1 THANKS

Big thanks to Martin Vi/t, LangSoft and everybody who contributed to
existence of this awesome service.

    http://www.slovnik.cz/about.html

I have been using www.slovnik.cz for B<many years> now, mostly as a custom
search in Opera, so I grew so accustomed to its reliability and simple
interface that I can't even imagine using anything else.  (Or remember
I ever had).  I could never repay.

=head1 AUTHOR

...of this scriptie: Alois Mahdal at zxcvb tec<ka cz

=cut