#!/usr/bin/perl

## make sure we can run this from the web interface
use lib qw(/home/ernad/ernad/perl /home/ernad/usr/share/perl /home/ernad/lib/perl);

use strict;
use warnings;

use Carp qw(cluck longmess shortmess croak confess);
use Data::Dumper;
use Date::Format;
use Getopt::Std;
use List::Util qw(shuffle);

use Ernad::Common;
use Ernad::Constant;
use Ernad::Email;
use Ernad::Erimp;
use Ernad::Files;

my %o;
&getopts('fr:', \%o);
my $do_redo=$o{'f'} // '';
my $do_only_repcode=$o{'r'} // '';

my $impna=shift;
if(not $impna) {
  print "I need an impna here.\n";
  exit;
}

our $e=Ernad::Erimp->new({'impna'=> $impna, 'verbose'=>1});
if($e->{'conf'}->{'no_leafs'}) {
  exit;
}

foreach my $repcode ($e->{'r'}->list()) {
  my $papers=&issue_files($repcode,$do_redo);
  my $doc=$e->{'r'}->doc($repcode) // confess "I don't have a repdoc";
  my $amf_ns=$Ernad::Constant::c->{'amf_ns'} // confess "I don't see the amf_ns";
  my $report_ele=$doc->getElementsByTagNameNS($amf_ns,'collection')->[0];
  if(not $report_ele) {
    confess "I don't see a main collection in $doc";
  }
  foreach my $date (reverse sort keys %$papers) {
    my $colpart_ele=$doc->createElementNS($amf_ns,'haspart');
    $colpart_ele->setAttribute('from',$date);
    $colpart_ele->setAttribute('until',$date);
    my $issue_ele=$doc->createElementNS($amf_ns,'collection');
    my $count_texts=0;
    foreach my $pid (@{$papers->{$date}}) {
      my $haspart_ele=$doc->createElementNS($amf_ns,'haspart');
      my $paper_ele=$doc->createElementNS($amf_ns,'text');
      $count_texts++;
      $paper_ele->setAttribute('ref',$pid);
      $haspart_ele->appendTextNode("\n");
      $haspart_ele->addChild($paper_ele);
      $haspart_ele->appendTextNode("\n");
      $issue_ele->addChild($haspart_ele);
      $issue_ele->appendTextNode("\n");
      $colpart_ele->addChild($issue_ele);
      $colpart_ele->appendTextNode("\n");
    }
    if(not $count_texts) {
      next;
    }
    $report_ele->addChild($colpart_ele);
    $report_ele->appendTextNode("\n");
  }
  $doc->toFile("/tmp/test_rss.xml");
  my $rss_doc=$e->{'t'}->transform($doc,'rss');
  my $out_file=$e->{'report'}->{$repcode}->{'dir'}->{'leafs'}.'/rss.xml';
  &Krichel::File::prepare($out_file);
  $rss_doc->toFile($out_file);
  $e->echo(__LINE__,"I wrote $out_file.");
}

sub issue_files {
  my $repcode=shift // $e->{'repcode'} // confess "I need a repcode.";
  my $do_redo=shift // '';
  my $rerc=$e->{'report'}->{$repcode};
  if(not $rerc) {
    $e->{'r'}->load($repcode);
    $rerc=$e->{'report'}->{$repcode};
  }
  if(not $rerc) {
    confess "I don't know about your report '$repcode'.";
  }
  my $sent_dir=$rerc->{'dir'}->{'sent'};
  my $rifs=$e->{'d'}->get_latest_rif_from_each_issue_in_subdir($sent_dir);
  my $p;
  foreach my $rif (shuffle @$rifs) {
    my $issuedate=$e->{'f'}->issuedate($rif);
    my $out_file=$rerc->{'dir'}->{'blatt'}.'/'.$issuedate.'.html';
    my $papids=$e->{'x'}->papids($rif,'array');
    $p->{$issuedate}=$papids;
    if(not $papids) {
      $e->echo(__LINE__,"The rif $rif is empty.");
      if(-f $out_file) {
        $e->echo(__LINE__,"I remove the file $out_file.");
        unlink $out_file;
      }
      next;
    }
    if(not $do_redo and not &Ernad::Common::does_file_need_renewal($out_file,$rif)) {
      next;
    }
    my $doc=$e->{'t'}->transform_file($rif,'email_html_issue');
    &Krichel::File::prepare($out_file);
    $doc->toFile($out_file,1);
    $e->echo(__LINE__,"I wrote $out_file.");
  }
  return $p;
}
