#!/usr/bin/perl

use strict;
use File::Basename;
use File::Compare;
use File::Find;
use Data::Dumper;
use List::Util qw(shuffle);
use Getopt::Std;

use Ernad::Args;
use Ernad::Erimp;

my %o;
&Getopt::Std::getopts('arvz', \%o);
my $do_all = $o{'a'} // 0;
my $do_real = $o{'r'} // 0;
# my $do_verbose = $o{'v'} // 0;
my $need_compressed = $o{'z'} // 0;

our ($in_file, $impna, $repcode, $issuedate, $e);
&Ernad::Args::parse();
if(not $ARGV[0]) {
  print "I need an argument.\n";
  exit;
}
if(not $impna) {
  print "I don't have a impna.\n";
  exit;
}

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

my $var_dir=$e->{'dir'}->{'reports'};

## previous file, initialize at the root
my $previous_file='/';

my @repcodes;
if($repcode) {
  @repcodes=($repcode);
}
else {
  @repcodes=shuffle ($e->list_repcodes());
}

foreach my $repcode (@repcodes) {
  $e->echo(__LINE__,"report is $repcode.",10);
  &deal_with_duplicates($repcode);
  ## do just one report
  if(not $do_all and scalar @repcodes > 1) {
    $e->echo(__LINE__,"I only did $repcode.");
    exit;
  }
}

exit;


## FixMe:: this uses
sub deal_with_duplicates {
  my $repcode=shift;
  my $old_issuedate='';
  my $old_file='';
  my $old_size='';
  foreach my $dir (`find $var_dir/$repcode -type d`) {
    chomp $dir;
    if($dir=~m|/opt/|) {
      next;
    }
    ## fixme, should not be there
    if($dir=~m|/blatt/|) {
      next;
    }
    $e->echo(__LINE__,"I look in $dir.",10);
    foreach my $file (glob("$dir/*")) {
      if(-d $file) {
        next;
      }
      my $issuedate=$e->{'f'}->issuedate($file);
      if(not $issuedate) {
        $e->echo(__LINE__,"I see no issuedate on $file, skip");
      }
      if(not $old_issuedate) {
        $old_issuedate=$issuedate;
        $old_file=$file;
        $old_size=-s $file;
        next;
      }
      if($old_issuedate eq $issuedate
         and $old_size == -s $file) {
        #print `ls -l $old_file $file`;
        ## don't remove duplicates unless they are so old that
        ## they got compressed.
        if($need_compressed) {
          if(not ($file=~m|\.gz$|) or not ($old_file=~m|\.gz$|)) {
            next;
          }
        }
        my $new_tmp_file="/tmp/remove_duplicates_in_ernad_var_repcodes.new.xml";
        my $old_tmp_file="/tmp/remove_duplicates_in_ernad_var_repcodes.old.xml";
        if($file=~m|\.gz$|) {
          system("/bin/zcat $file > $new_tmp_file");
        }
        else {
          system("/bin/cat $file > $new_tmp_file");
        }
        if($old_file=~m|\.gz$|) {
          system("/bin/zcat $old_file > $old_tmp_file");
        }
        else {
          system("/bin/cat $old_file > $new_tmp_file");
        }
        my $diff=`/usr/bin/diff -aw $new_tmp_file $old_tmp_file`;
        chomp $diff;
        if(not $diff) {
          $e->echo(__LINE__,"Looks like $old_file and $file are the same.",5);
          if($do_real) {
            $e->echo(__LINE__,"I delete $file.");
            unlink $file;
          }
          else {
            $e->echo(__LINE__,"I would delete $file.");
          }
        }
        else {
          $e->echo(__LINE__,"$old_file are $file are different.");
        }
        #if(compare($file,$previous_file) == 0) {
        #  print "same\n";
        #}
        #else {
        #  print "no same\n";
        #}
        next;
      }
      my $size=-s $file;
      $old_issuedate=$issuedate;
      $old_file=$file;
      $old_size=$size;
    }
  }
}
