#!/usr/bin/perl -s
use warnings;
use strict;
use 5.006;

use Lingua::Identify qw(langof);

our ($a,$d,$h,$l,$m,$p,$v);

show_help()	if $h;
show_version()	if $v;
show_languages()if $l;

$a = 1 if $m;

my @files = @ARGV;

if (@files) {
  for (sort @files) {
    -f && -r && -s || next;
    open (STDIN, $_) || do {print STDERR "Could not open $_ ($!)\n" ; next};
    print $_, ":", (join $", ident()), "\n";
    close (STDIN);
  }
}
else {
  print "", (join $", ident()), "\n";
}

#
# subroutines
#

sub show_help {
  die "Usage: langident file1 file2
 or:   langident -a -p file
 or:   cat file | langident
langident: identifies the languages files are written in

Options:
  -a         show all results
  -d         debug
  -h         displays this messages and exit
  -l         list available languages and exit
  -m=NUMBER  sets maximum number of results (languages) to display
  -p         also show percentages
  -v         show version and exit
"
}

sub show_version {
  die "langident version ", Lingua::Identify->VERSION, "\n";
}

# identify the language
sub ident {
  if ($a || $p) {
    my @results = langof(<>);
    if ($m) {
      @results = @results[0 .. (($m * 2) - 1)];
    }
    @results = @results[0,1] unless $a;
    @results = grep /[a-z]{2}/, @results unless $p;
    for (@results) {$_*=100 if /\d/}
    return @results;
  }
  else {
    return (langof(<>))[0];
  }
}

# show the available languages
sub show_languages {
  use Lingua::Identify qw/get_all_languages name_of/;
  for (sort (get_all_languages())) {
    print uc $_, " - ", ucfirst name_of($_) ,"\n";
  }
  exit;
}

__END__

=head1 NAME

langident - identifies the language files are written in

=head1 SYNOPSIS

langident [OPERATION] file1 [file2 ...]

=head1 DESCRIPTION

Identifies the language files are written in using Perl module
Lingua::Identify.

=head2 OPERATIONS

=head2 -a

show all results

=head2 -d

debug

=head2 -h

displays this messages and exits

=head2 -p

also show percentages

=head2 -v

show version and exit

=head2 -h

prints help and exits

=head1 TO DO

=over 6

=item * Add a -c switch, for confidence levels

=item * Allow activation/deactivation/selection of languages

=item * Allow usage of configuration parameters allowed by Lingua::Identify

=back

=head1 SEE ALSO

Lingua::Identify(3), Text::ExtractWords(3), Text::Ngram(3), Text::Affixes(3).

A linguist and/or a shrink.

The latest CVS version of C<Lingua::Identify> (which includes I<langident>) can
be attained at http://natura.di.uminho.pt/natura/viewcvs.cgi/Lingua/Identify/

=head1 AUTHOR

Jose Alves de Castro, E<lt>cog@cpan.orgE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright 2004 by Jose Alves de Castro

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself. 

=cut
