
package taglist;

use strict;
use vars qw($taglist_all $taglist_all_format $taglist_story $taglist_story_format $debug);

use File::stat;
use Data::Dumper;

my $cachefile = $blosxom::taglist_cachefile;
my $charset   = $blosxom::taglist_charset;
my %cache;

if (!defined $taglist_all_format){
  $taglist_all_format = sub {
    my ($alltags_ref) = @_;
    my $ret = '';
    foreach (sort keys %$alltags_ref){
      $ret .= "<a href=\"/$_\">$_($alltags_ref->{$_})</a><br>\n";
    }
    chop $ret;
    return $ret;
  }
}

if (!defined $taglist_story_format){
  $taglist_story_format = sub {
    my ($c_date, $c_tagref, $fname) = @_;
    my $ret = '';
    foreach (@$c_tagref){
       $ret .= "<a href=\"/$_\">$_</a>|";
    }
    chop $ret;
    return $ret;
  }
}

sub start{
  1;
}

sub filter{
  my ($pkg, $files) = @_;
  my (%alltags, $reindex);
  
  #read cachefile
  if ( open ENTRIES, $cachefile ) {
    my $VAR1;
    my $index = join '', <ENTRIES>;
    close ENTRIES;
    $index =~ /\$VAR1 = \{/ and eval($index) and !$@ and %cache = %$VAR1;
  }
  
  foreach my $fn (keys %$files){
    if (-f $fn){
      #if target file is exist
      my @f_tags;
      my $f_date  = stat($fn)->mtime;
      my $c_date  = $cache{$fn}->[0];
      my $c_tagref= $cache{$fn}->[1];
      
      if ($c_date and $c_date == $f_date){
        #target file is not modifieded
        @f_tags = @$c_tagref;
      }else{
        #target file is changed
        #correct tags from path_info
        $fn =~ m!^$blosxom::datadir/(.*)/[^/]*\.$blosxom::file_extension!;
        @f_tags = split '/', $1;
        #correct tags from top line of target file
        if (open ENTRIES, $fn){
          <ENTRIES> =~ m!(\[[^]]+\])*!g;
          close ENTRIES;
          push @f_tags, $& =~ m![^\[\]]+!g;
        }
        sort @f_tags;
        #store to cache
        $cache{$fn} = [$f_date, [@f_tags]];
        $reindex = 1;
      }
      
      foreach (@f_tags){
        $alltags{$_}++;
      }
      
      #delete non-target record
      my @t_tags = split '/', $blosxom::path_info;
      my $t_cnt = 0;
      
      #delete if last is filename
      pop @t_tags if $t_tags[-1] =~ m!$blosxom::flavour$!;
      
      foreach my $t_tag (@t_tags){
        foreach my $f_tag (@f_tags){
          if ($t_tag eq $f_tag){
            $t_cnt++;
            last;
          }
        }
      }
      if ($t_cnt != @t_tags){
        delete $files->{$fn};
      }
    }else{
      #delete record if target file is not exist
      delete $files->{$fn};
#     delete $cache{$fn};
    }
  }
  
  #make $alltags
  $taglist_all = &$taglist_all_format(\%alltags);
  
  #save cachefile if updated
  if ( $reindex ) {
    if ( open ENTRIES, "> $cachefile" ) {
      flock(ENTRIES, 2);
      my $buf = Dumper \%cache;
      $buf =~ s! +! !g;
      print ENTRIES  $buf;
      close ENTRIES;
    }
  }
}

sub story{
  my ($pkg, $path, $fn, $story_ref, $title_ref, $body_ref) = @_;
  my $fname = "$blosxom::datadir$path/$fn.$blosxom::file_extension";
  my $c_date  = $cache{$fname}->[0];
  my $c_tagref= $cache{$fname}->[1];
  
  $taglist_story = &$taglist_story_format($c_date, $c_tagref, $fname);
  $$title_ref =~ s!^(\[[^]]+\])*!!g;
}

1;
