#!/usr/bin/perl -w

use strict;

use Getopt::Long;
use Config::General;
use Pod::Usage;

# use Data::Dumper;
use File::Basename;
# use Sys::Hostname;

# use Alvis::Convert;

# use Alvis::NLPPlatform;
# use Alvis::NLPPlatform::Convert;

use Lingua::Ogmios;

# use Encode;

my $man = 0;
my $help = 0;
my $rcfile="etc/ogmios/nlpplatform.rc" ;

my $current_dir;
my $dirfile;
my $file;

warn "\nRunning Ogmios (NLP Platform) version " . $Lingua::Ogmios::VERSION . "\n\n";

GetOptions('help|?' => \$help, man => \$man, "rcfile=s" => \$rcfile) or pod2usage(2);
pod2usage(1) if $help;
pod2usage(-exitstatus => 0, -verbose => 2) if $man;

if ((defined $rcfile) && (($rcfile eq "") || (!(-f $rcfile)))) {
    warn "No such config file or config file is not set\n";
    pod2usage(1);
} 


if (scalar @ARGV == 0) {
    warn "Missing file or directory to analyse\n";
    die "Try '" . basename($0) . " --help' for more information.\n";
}



foreach $file (@ARGV) {
    if (-f $file) {

	warn "Creating Ogmios\n";
	my $NLPPlatform = Lingua::Ogmios->new('rcfile' => $rcfile);

	warn "loading document(s) from $file\n";

	$NLPPlatform->loadDocuments([$file]);
	
	$NLPPlatform->tokenisation;

	$NLPPlatform->linguisticProcessing;

	if (!$NLPPlatform->getConfig->xmloutput_noStdXmlOutput) {
	    print $NLPPlatform->XMLout;	
	}
	$NLPPlatform->getTimer->_printTimesInLine("Ogmios");

    } else { 
 	if (-d $file) {
	    warn "Scruting $file\n";
 	    $current_dir = $file;
 	    opendir DIR, $file;
 	    while ($dirfile = readdir DIR) {
 		if (($dirfile ne ".") && ($dirfile ne "..")) {
		    warn "-> $current_dir/$dirfile\n";
  		    push @ARGV, "$current_dir/$dirfile";
 		}
 	    }
 	    closedir DIR;
 	}
    }
}

exit 0;


__END__


# my %config = &Alvis::NLPPlatform::load_config($rcfile);



# my $mm = Alvis::NLPPlatform::Convert::load_MagicNumber(\%config);

# my $AlvisConverter = Alvis::NLPPlatform::Convert::html2alvis_init(\%config);

# my $HOSTNAME=hostname;
# my $doc;

# if (scalar @ARGV == 0) {

#     warn "Missing file or directory to analyse\n";
#     die "Try '" . basename($0) . " --help' for more information.\n";
# }


# foreach $file (@ARGV) {
#     if (-f $file) {
# 	print STDERR "Converting the file $file ...\n";

# 	# Insure that Input files will be returned

# 	$config{"CONVERTERS"}->{"StoreInputFiles"} = 0;

#  	$doc =  Alvis::NLPPlatform::Convert::conversion_file_to_alvis_xml($file, $AlvisConverter, \%config, $mm);

#  	Alvis::NLPPlatform::standalone(\%config, $HOSTNAME, $doc);
# 	print STDERR "\tdone\n";
	
#     } else { 
# 	if (-d $file) {
# 	    $current_dir = $file;
# 	    opendir DIR, $file;
# 	    while ($dirfile = readdir DIR) {
# 		if (($dirfile ne ".") && ($dirfile ne "..")) {
# 		    push @ARGV, "$current_dir/$dirfile";
# 		}
# 	    }
# 	    closedir DIR;
# 	}
#     }
# }

# exit 0;


# __END__

=encoding utf8

=head1 NAME

ogmios-standalone - Perl script for linguistically annotating textual documents

=head1 SYNOPSIS

ogmios-standalone [options] [Input_document | Directory] 

=head1 OPTIONS

=over 4

=item    B<--help>            brief help message

=item    B<--man>             full documentation

=item    B<--rcfile=file>     read the given configuration file

=back

=head1 DESCRIPTION

This script linguistically annotates the free-text documents given in
argument.  Free-text documents are represented as XML files
corresponding to the Alvis XML schema (or Ogmios XML format).  The annotated documents are
sent to the standard output. The linguistic annotation depends on the
configuration of the platform which is specified in a configuration
file provided with the C<rcfile> option.

Arguments can be either Ogmios XML files or directories containing XML
files.

=head1 SEE ALSO

Thierry Hamon, Adeline Nazarenko, Thierry Poibeau, Sophie Aubin,
Julien Derivière "A Robust Linguistic Platform for Efficient and
Domain specific Web Content Analysis". Proceedings of RIAO 2007 -
Session Poster. 30 may - 1 june 2007. Pittsburgh,
USA. L<https://perso.limsi.fr/hamon/publications/PDF/RIAO2007-HamonEtAl.pdf>

Thierry Hamon et Adeline Nazarenko. "Le développement d'une
plate-forme pour l'annotation spécialisée de documents web: retour
d'expérience", Traitement Automatique des Langues
(TAL). 2008. 49(2). pages 127-154.
L<https://perso.limsi.fr/hamon/publications/PDF/TAL-2008-49-2-05-Hamon.pdf>

=head1 AUTHORS

Thierry Hamon, E<lt>thierry.hamon@limsi.frE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2012 Thierry Hamon

This is free software; you can redistribute it and/or modify it under
the same terms as Perl itself, either Perl version 5.8.4 or, at your
option, any later version of Perl 5 you may have available.

=cut
