#!/usr/bin/perl

eval 'exec /usr/bin/perl  -S $0 ${1+"$@"}'
    if 0; # not running under some shell

=head1 NAME 

wikipedia - lookup an entry in the wikipedia

=head1 SYNOPSIS

    % wikipedia [-l lang] perl

=head1 DESCRIPTION

wikipedia is a command line tool for looking up a topic in the wikipedia
and printing it to STDOUT. Useful if you spend a lot of time in a shell
and don't want to fire up a web browser to see what something is.

=head1 AUTHORS

=over 4

=item * Slaven Rezic <slaven@rezic.de>

=back

=cut

use strict;
use WWW::Wikipedia;
use Text::Autoformat;
use Pod::Usage;
use Getopt::Std;

my %options;
getopts( 'l:', \%options );

my $term = shift;
pod2usage( { verbose => 1 } ) if !defined( $term );

my $wiki = WWW::Wikipedia->new( language => $options{ l } || 'en' );
my $entry = $wiki->search( $term );

## use a pager if we can
eval( 'use IO::Page' );
if ( $entry ) {
    my $text = $entry->text();
    if ( $text ) { print $text; }
    else {
        print "Specific entry not found, see also:\n";
        foreach ( $entry->related() ) { print " - $_\n"; }
    }
}
else { print qq("$term" not found in wikipedia\n) }

