#!/usr/bin/perl -w

use strict;
use warnings;

use Carp qw(confess);
use Data::Dumper;
use Date::Parse;
use Date::Format;
use File::Basename;
use Storable;
use List::Util qw(shuffle);

use Ernad::Erimp;
use Ernad::Dates;

my $impna=$ARGV[0];
if(not defined($impna)) {
  print "I need the impna.\n";
  exit;
}

our $e=Ernad::Erimp->new({'impna'=>$impna,
                          'verbose'=>4});

## can be 1 or 2
my $verbose=0;

my $today=&Ernad::Dates::today;

## suspend
if(Ernad::Dates::compare_dates($today,'2018-07-01')) {
  exit;
}


my $home='/home/ernad';
my $reports_dir="$home/ernad/var/nep/reports";
my $web_dir="$home/nep/public_html/issues";
my $archives_dir="/var/lib/mailman/archives/private";
my $cache_dump="$home/var/cache/viviana_bug.dump";

binmode(STDOUT,"utf8");
binmode(STDERR,"utf8");

## grace time. how long will it be at most that an issue appears in the archive
my $grace_time=1800;

## minimum.
my $seconds_per_month=28*24*60*60;

## time for checking that we don't send off documents to rapidly
my $now=time;

## retrieve dump
my $cache;
## time in seconds since the script was last run
my $last_run_age;
if(-f $cache_dump) {
  $cache=retrieve $cache_dump;
  $last_run_age = -M $cache_dump;
  $last_run_age = $last_run_age * 24 * 60 * 60 ;
}
else {
  $last_run_age = 0;
}

$last_run_age = 0;

#my @repcodes=shuffle $e->get_curpu_repcodes();
my @repcodes=shuffle $e->list_repcodes();
foreach my $repcode (@repcodes) {
  ## temporary fix to prevent allport appearing
  if(not $e->{'conf'}) {
    confess "I need this here.";
  }
  if($repcode eq $e->{'conf'}->{'allport_repcode'}) {
    next;
  }
  ## populates cache
  &do_report($repcode);
}
store $cache, $cache_dump;

# check a report
sub do_report {
  my $repcode=shift;
  ## list mail files
  my $mailvault_dir=$e->{'dir'}->{'mailvault'} // confess "I have no mailvault dir.";
  $mailvault_dir.='/'.$repcode;
  my @mail_files=glob("$mailvault_dir/*.mail");
  ## foreach mail file
  foreach my $mail_file (@mail_files) {
    ## find the date through ia regex
    my $bana=basename($mail_file);
    $bana=~m|^(\d{4}-\d{2}-\d{2})|;
    my $date=$1;
    if(not $date) {
      if($verbose) {
        print "I don't see a date on $mail_file\n";
      }
    }
    my $ago=&Ernad::Dates::diff_dates($date,$today);
    ## skip if cached
    if($ago > 30) {
      if($verbose > 1) {
        print "date $ago is $ago, too old.\n";
      }
      next;
    }
    #if($cache->{$repcode}->{$date}) {
    #  next;
    #}
    ##print "grep $date $archives_dir/$repcode/index.html\n";
    if(&is_it_in_mailman_archive($repcode,$date,6)) {
      $cache->{$repcode}->{$date}=1;
      if($verbose) {
        print "$repcode of $date is in the archive\n";
      }
      next;
    }
    elsif($verbose) {
      print "I can't find issue $date of $repcode\n";
    }
    #print "could nto find $repcode $date\n";
    #next;
    # now consider age as factor
    my $age = -M $mail_file;
    $age = $age * 24 * 60 * 60 ;
    print "$date of $repcode of age $age is not in the archive\n";
    ## the age of the file must be higher than the grace period
    ## and the script must have run last time longer than
    ## grace_time ago
    warn "I may need to remail $repcode $date.";
    if($age > $grace_time and $last_run_age > $grace_time) {
      my $s="$home/perl/remail_a_nep_report_issue $repcode $date";
      #print "$s\n";
      die "I need to remail $repcode $date";
      ###system($s);
    }
  }
}


sub is_it_in_mailman_archive {
  my $repcode=shift;
  my $date=shift;
  my $max_forward=shift;
  my $count_months=-1;
  while($count_months++ < $max_forward) {
    my $time=str2time($date,'UTC')+$seconds_per_month*$count_months;
    my $month=time2str("%B",$time);
    my $year=time2str("%Y",$time);
    my $mailman_log_file="$archives_dir/$repcode/$year-$month.txt";
    if($time>$now) {
      return 0;
    }
    if(not -f $mailman_log_file) {
      if($verbose) {
        print "I don't see the file $mailman_log_file\n";
      }
      next;
    }
    my $grep_command="grep $date $mailman_log_file";
    if($verbose) {
      print "$grep_command\n";
    }
    my $grep=`$grep_command`;
    if($grep) {
      if($verbose) {
        print "I found $repcode $date found at $count_months later\n";
      }
      return 1;
    }
    my $pretty_date=$date;
    $pretty_date=~s|-|\x{2012}|g;
    $grep_command="grep $pretty_date $mailman_log_file";
    ## if found, mark as such in the cache
    $grep=`$grep_command`;
    if($verbose) {
      print "$grep_command\n";
    }
    if($grep) {
      if($verbose) {
        print "found at $count_months\n";
      }
      return 1;
    }
    my $wordy_date=$date;
    $wordy_date=~s|-|.|g;
    $wordy_date="\"Issue of $wordy_date\"";
    $grep_command="grep $wordy_date $mailman_log_file";
    ## if found, mark as such in the cache
    $grep=`$grep_command`;
    if($verbose) {
      print "$grep_command\n";
    }
    if($grep) {
      if($verbose) {
        print "I found $repcode $date found at $count_months later\n";
      }
      return 1;
    }
    my $tab_date=$date;
    $tab_date=~s|-|.|g;
    $tab_date="\"Date:      $tab_date\"";
    $grep_command="grep $tab_date $mailman_log_file";
    ## if found, mark as such in the cache
    $grep=`$grep_command`;
    if($verbose) {
      print "$grep_command\n";
    }
    if($grep) {
      if($verbose) {
        print "I found $repcode $date found at $count_months later\n";
      }
      return 1;
    }
  }
  return 0;
}
