package Library::GLS;

use 5.008;
use strict;
use warnings;

use Data::Dumper;

require Exporter;

our @ISA = qw(Exporter);
our %EXPORT_TAGS = ( 'all' => [ qw( ) ] );

our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );

our @EXPORT = qw(new
		 get_autor
		 get_titulo
		 get_desc
		 get_keys
		 get_sig
		 get_file_name);

our $VERSION = '0.01';


sub new {
  my $fich = shift;
  my $autor;
  my $titulo;
  my $desc;
  my @texto;
  my $txt;
  my $file_name;
  open (IN,"$fich");
  @texto = <IN>;
  close (IN);
  $txt = shift(@texto);
  if ($txt =~ /\#\#\# Glossary title:(.*)/) {
    $titulo = $1;
    chomp($titulo);
  }
  $txt = shift(@texto);

  if ($txt=~/\#\#\# Author:(.*)/) {
    $autor=$1;
    chomp($autor);
  }
  $txt=shift(@texto);
  if ($txt=~/\#\#\# Description:(.*)/) {
    $desc=$1;
    chomp($desc);
  }
  $txt=shift(@texto);

  while ($txt !~/\#\#\# Glossary section:/) {
    $txt=shift(@texto);
  }

  my $tam=@texto;
  my %todos;
  while ($tam>=3) {
    shift(@texto);
    $txt=shift(@texto);
    my $sig=shift(@texto);
    $tam=@texto;
    $txt=~s/\n//;
    chomp($txt);
    chomp($sig);
    $todos{$txt}=$sig;
  }
  my %gls;
  %{$gls{"glos"}}=%todos;
  %todos=();
  $gls{"autor"}=$autor;
  $gls{"desc"}=$desc;
  if ($fich=~/.*\/(.*)/g) {
    $file_name=$1;
  } else {
    $file_name=$fich;
  }
  $gls{"f_name"}=$file_name;
  $gls{titulo}=$titulo;
  return (\%gls);
}

sub get_file_name {
  my $g = shift;
  my %gls;

  %gls = %{$g};
  return $gls{'f_name'};
}

sub get_autor {
  my $g = shift;
  my %gls;

  %gls = %{$g};
  return $gls{'autor'};
}

sub get_titulo {
  my $g = shift;
  my %gls = %{$g};
  return $gls{'titulo'};
}

sub get_desc {
  my $g = shift;
  my %gls = %{$g};
  return $gls{'desc'};
}

sub get_keys {
  my $g = shift;
  my %gls = %{$g};
  my %glossario = %{$gls{'glos'}};
  my @lista = ();
  for (keys %glossario) {
    push @lista,$_;
  }
  my @res = ();
  @res = ordenar(\@lista);
  return @res;
}

sub get_sig {
  my $g = shift;
  my $pal = shift;
  my %gls = %{$g};
  my %glossario = %{$gls{'glos'}};
  return $glossario{$pal};
}

sub ordenar {
  my $arg = shift;
  my @array = @{$arg};
  my $tam = @array;
  my $pos = -1;
  my $mudar;
  my $sitio;
  my $aux_2;
  my @arr_aux;

  for (@array) {
    $pos++;
    my $aux=$pos-1;
    $mudar=0;
    while ($aux>=0) {
      if (lc $array[$pos] lt lc $array[$aux]) {
	$sitio=$aux;
	$mudar=1;
      }
      $aux--;
    }
    if ($mudar==1) {
      $aux_2=splice @array,$pos,1;
      @arr_aux=splice @array,$sitio;
      push @array,$aux_2;
      push @array,@arr_aux;
      $mudar=0;
    }
  }
  return @array;
}

1;
__END__

=head1 NAME

Library::GLS - Perl extension for managing glossaries

=head1 SYNOPSIS

  use Library::GLS;

=head1 ABSTRACT

  This should be the abstract for Library::GLS.

=head1 DESCRIPTION



=head1 SEE ALSO



=head1 COPYRIGHT AND LICENSE

Copyright 2002 by Alberto Manuel Brandao Simoes

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

=cut
