#!/usr/bin/perl

use Getopt::Std;
use Text::Mining;
use strict;
use warnings;

our $verbose  = 0;
our $function = '';
our $corpus_name = '';

our $features = { new           => \&new_corpus,
                  all           => \&show_all_corpuses,
                  get_corpus_id => \&get_corpus_id,
                  delete        => \&delete_corpus,
		};

get_options();

$features->{ $function }->();

exit;


sub new_corpus {
	my $corpus = Text::Mining::Corpus->new({ corpus_name => $corpus_name });
	print "new_corpus " . $corpus->get_name() . " (" . $corpus->get_id() . ") \n";
}

sub show_all_corpuses {
	my $tmlibrary = Text::Mining->new();
	my $corpuses  = $tmlibrary->get_all_corpuses();
	foreach my $corpus ( @{ $corpuses }) {
		my $name = $corpus->get_name();
		my $id   = $corpus->get_id();
		print "  $name ($id) \n";
	}
}

sub delete_corpus {
	print "delete_corpus $corpus_name \n";
}

sub get_corpus_id() {
	my $tmlibrary = Text::Mining->new();
	print $tmlibrary->get_corpus_id({ corpus_name => $corpus_name }) || '0';
}

sub get_options {
	my $opt_string = 'n:d:i:avh';
	my %opt        = ();

	getopts( "$opt_string", \%opt ) or usage();
	
	# Handle "immediate" switches
	usage() if $opt{h};
	$verbose    = 1 if defined $opt{v};

	# Handle assigned switches
	if    ( $opt{a} ) { $function = 'all'; }
	elsif ( $opt{n} ) { $function = 'new';    $corpus_name = $opt{n}; }
	elsif ( $opt{i} ) { $function = 'get_corpus_id'; $corpus_name = $opt{i}; }
	elsif ( $opt{d} ) { $function = 'delete'; $corpus_name = $opt{d}; }
	else              { usage(); exit; }

	if ( $verbose ) { print STDERR "  Function '$function' called.\n"; }
}

sub usage {
	print STDERR << "EOF";

usage: corpus [-vh] -[n|c|d] corpus_name

 -i corpus_name   : Returns the corpus_id given the corpus_name
 -v               : verbose output (when available)
 -h               : this (help) message

example: tmlibrary -i Dev
         tmlibrary -v -i Dev
         tmlibrary -h

EOF
    exit;
}


