#!/usr/bin/perl

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

require 5.005;
use strict;
use warnings;
use Forest;
use Forest::LP::Parser;
use Forest::RCG::Parser;
use Forest::XTAG::Parser;
use Forest::XML::Parser;
use Forest::XML::Writer;
use Forest::HTML::Writer;
use Forest::Dot::Writer;
use Forest::Dependency::Writer;
use Forest::Tagger::Writer;
use Data::Dumper;

use Getopt::Mixed "nextOption";

my %reader =
  ('lp' => \&Forest::LP::Parser::parse,
   'rcg' => \&Forest::RCG::Parser::parse,
   'xtag' => \&Forest::XTAG::Parser::parse,
   'xml' => \&Forest::XML::Parser::parse
   );

my %writer = 
  ('xml' => \&xml_dump,
   'dep' => \&dependency_dump,
   'xmldep' => \&xmldep_dump,
   'lpdep' => \&lpdep_dump,
   'stats' => \&stats_dump,
   'tree' => \&tree_dump,
   'grammar' => \&html_dump,
   'tagger' => \&tagger_dump,
   'yesno' => \&yesno_dump,
   );

my $iformat = 'lp';
my $oformat = 'xml';


Getopt::Mixed::init( "i=s in>i",
                     "o=s out>o",
                     "f=s from>f",
                     "t=s to>t",
                     );

while (my ($option, $value) = nextOption()) {
    $iformat= $value, next if ($option eq 'f');
    $oformat = $value, next if ($option eq 't');
}

my $forest=$reader{$iformat}->(\*STDIN);

$writer{$oformat}->($forest);

sub dump {
  my $forest = shift;
  my $d = Data::Dumper->new([$forest]);   
  print $d->Dump,"\n\n";
}

sub xml_dump {
  my $forest = shift;
  $forest->pretty_print(@_);
}

sub html_dump {
    $forest->html(@_);
}

sub tree_dump {
  my $forest = shift;
  $forest->dot(@_);
}

sub dependency_dump {
    my $forest = shift;
    $forest->dependency(@_);
}

sub xmldep_dump {
  my $forest = shift;
  $forest->xmldep(@_);
}

sub lpdep_dump {
  my $forest = shift;
  $forest->lpdep(@_);
}

sub tagger_dump {
  my $forest = shift;
  $forest->tagger(@_);
}

sub stats_dump {
    my $forest = shift;
    $forest->stats(@_);
}

sub yesno_dump {
  my $forest = shift;
  print (($forest->{nb} >= 0) ? "Success\n" : "Failure\n");
}
