#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper; 
use File::Basename;
use File::Path;  
use Time::Duration;
use XML::LibXML;
use XML::LibXSLT;
    
my $home_dir=$ENV{'HOME'};
my $reports_dir="$home_dir/ernad/var/reports";
my $out_dir="$home_dir/nep/public_html/issue_evolution";
my $style_file="$home_dir/xslt/issue_evolution.xslt.xml";
my $style_file_cover="$home_dir/xslt/issue_evolution_cover.xslt.xml";

my $xslt = XML::LibXSLT->new();
my $style_doc = XML::LibXML->load_xml(location=>$style_file, no_cdata=>1);
my $stylesheet = $xslt->parse_stylesheet($style_doc);
my $style_doc_cover = XML::LibXML->load_xml(location=>$style_file_cover, no_cdata=>1);
my $stylesheet_cover = $xslt->parse_stylesheet($style_doc_cover);


my @dirs=reverse ('mail', 'sent', 'ordered', 'selected', 'created', 'source/ps', 'source/us');

## only do current year
my $year=`date +%Y`;
chomp $year;
### do all years!
#$year='';
###

## report struture
my $r;

my $dom=XML::LibXML::Document->new();

foreach(`$home_dir/ernad/perl/list_reports.pl`) {
  if(m|nep-xxx| or m|nep-zzz|) {
    next;
  }
#  if(not m|nep-all|) {
#    next;
#  }
  chomp;
  my $report=$_;
  #print "report is $report\n";
  &do_report($report);
}

&make_index_file;

exit;

sub do_report {
  my $report=shift;
  my $current_year;
  ## gather data
  my $year_done;
  foreach my $dir (@dirs) {
    my $full_dir="$reports_dir/$report/$dir";
    foreach my $file (`ls $full_dir/*.xml 2> /dev/null`) {
      chomp $file;
      #print "file is $file\n";   
      if($year) {
        $year_done->{$year}=1;
        $current_year=$year;
      }
      if($year and (not $file=~m|/$year\-|)) {
        next;
      }      
      else {
        $file=~m|/(\d{4})\-|;        
        $current_year=$1;
        $year_done->{$current_year}=1;
      }
      #print "current year is $current_year\n";
      my $bn=basename($file); 
      $bn=~m|(\d{4})-(\d{2})-(\d{2})_(\d+)\.amf.xml$| or print "dodgy file: $file\n";
      my $date_number="$1$2$3";
      my $date_string="$1\x{2012}$2\x{2012}$3";
      my $time=$4;
      my $number=`grep -o '</text>' $file | grep -c ^`;
      chomp $number;
      $r->{$report}->{$current_year}->{$date_string}->{$dir}->{$time}=$number;
    }
  }
  foreach my $year (reverse sort keys %{$r->{$report}}) {
    my $report_element=$dom->createElement('report');
    $report_element->setAttribute('id',$report);
    $report_element->setAttribute('year',$year);
    #print "writing year $year for $report\n";
    foreach my $date (reverse sort keys %{$r->{$report}->{$year}}) {
      my $issue_element=$dom->createElement('issue');
      $issue_element->setAttribute('date',$date);
      foreach my $dir (@dirs) {
        if(not defined($r->{$report}->{$year}->{$date}->{$dir})) {
          next;
        }
        my $stage_element=$dom->createElement('stage');
        $stage_element->setAttribute('label',$dir);
        foreach my $time (sort keys %{$r->{$report}->{$year}->{$date}->{$dir}}) {
          my $action_element=$dom->createElement('action');
          $action_element->setAttribute('number',$r->{$report}->{$year}->{$date}->{$dir}->{$time});
          if(defined($r->{$report}->{$year}->{$date}->{'created'})) {
            #my @times_created=keys %{$time - $r->{$report}->{$year}->{$date}->{'created'}};
            #my $time_creation=$times_created[0];
            my $time_created=$time - $r->{$report}->{$year}->{$date}->{'created'};
            #print "time_create is $time_created\n";
            #print "time is $time\n";
            my $time_since_creation=$time-$time_created;
            #print "time since creation is $time_since_creation\n";
            if($time_since_creation>0) {
              $action_element->setAttribute('ago',ago($time_since_creation));
            }          
          }
          $stage_element->appendChild($action_element);
        }
        $issue_element->appendChild($stage_element);
      }
      $report_element->appendChild($issue_element);
    }
    $dom->setDocumentElement($report_element);
    #print $dom->toString(1);
    my $results = $stylesheet->transform($dom);
    if(not -d "$out_dir/$report") {
      mkpath("$out_dir/$report") ;
    }
    $results->toFile("$out_dir/$report/$year.html",1);
  }
}


## makes an index thta lists avaiable data
sub make_index_file {
  my $reports;
  my $years;
  foreach my $html_file (`find ~/nep/public_html/issue_evolution/ -name '*.html'`) {
    chomp $html_file;
    #print "found file $html_file\n";
    $html_file=~m|/(nep-\w{3})/(\d{4})\.html$| or next;
    $years->{$1}->{$2}=1;
    $reports->{$2}->{$1}=1;
  }
  
  my $reports_list_doc=$dom->createDocument('1.0','utf-8');
  my $reports_element=$dom->createElement('reports');
  foreach my $report (sort keys %$years) {
    my $report_element=$dom->createElement('report');
    $report_element->setAttribute('handle',$report);
    foreach my $year (sort {$b <=> $a} keys %{$years->{$report}}) {
      my $year_element=$dom->createElement('year');
      $year_element->setAttribute('count',$year);
      $report_element->appendChild($year_element);
    }
    $reports_element->appendChild($report_element);
  } 
  $reports_list_doc->setDocumentElement($reports_element);
  my $results = $stylesheet_cover->transform($reports_list_doc);
  $results->toFile("$out_dir/index.html",1);
}

