#!/usr/bin/perl

use strict;
use warnings;

use Carp qw(confess);
use Data::Dumper;
use File::Path;
use File::Basename;
#use Getopt::Std;

use Ernad::Args;
use Ernad::Erimp;
use Ernad::Enesei;
use Ernad::Dates;
use Ernad::Presort::Seeds;



our ($impna, $e, $repcode);
&Ernad::Args::parse();
$e=Ernad::Erimp->new({'impna'=> $impna,
                      'verbose'=>3});
die;
#my $archive_dir=$e->{'dir'}->{'confs_archive'};
my $archive_dir='/home/bims/archive';

my $xpc;

foreach my $file (sort `ls /home/bims/etc/old/*`) {
  die $file;
  chomp $file;
  my $bana=basename($file);
  if(not $bana=~m|^reports_(\d{4}-\d{2}-\d{2})\.amf\.xml$|) {
    if($bana=~m|~$|) {
      next;
    }
    print "I skip $file.";
    next;
  }
  my $date=$1;
  my $enesei=&Ernad::Enesei::file($file);
  foreach my $repcode (keys %$enesei) {
    my $dom=$enesei->{$repcode};
    &archive_dom($repcode,$dom,$date);
  }
}

sub archive_dom {
  my $repcode=shift // confess "I need a repcode";
  my $input=shift // confess "I need a dom";
  if(ref($input) ne 'XML::LibXML::Document') {
    if(not -f $input) {
      if(ref($input) ne 'XML::LibXML::Document') {
        confess "I need a documnent here";
      }
      $input=&remove_password($input);
    }
  }
  else {
    $input=&remove_password($input);
  }
  my $date=shift;
  if(not &Ernad::Dates::is($date)) {
    confess "I neeed a date here.";
  }
  my $out_file="$archive_dir/$repcode"."_$date.amf.xml";
  my $last_file=&get_last($repcode);
  if(not $last_file) {
    my $input_txt;
    if(ref($input)) {
      $input_txt=$input->toString(1);
    }
    else {
      $input_txt=&File::Slurp::read_file($input);
    }
    $input_txt=&Ernad::Enesei::canonical_string($input_txt);
    $input_txt=~s|\n *\n||g;
    $input_txt=~s|\n *||g;
    &Ernad::Enesei::save($input_txt,$out_file);
    return 1;
  }
  my $last_txt=&File::Slurp::read_file($last_file);
  my $input_txt;
  if(not ref($input) and -f $input) {
    $input_txt=&File::Slurp::read_file($input);
  }
  else {
    $input_txt=$input->toString(1);
  }
  $input_txt=&Ernad::Enesei::canonical_string($input_txt);
  ## extra fo storage
  $input_txt=~s|\n *||g;
  if(&is_different_without_blank($input_txt,$last_txt)) {
    if(not ref($input) and -f $input) {
      $input_txt=&File::Slurp::read_file($input);
      $input_txt=&Ernad::Enesei::canonical_string($input_txt);
      $input_txt=~s|\n *\n||g;
      &Ernad::Enesei::save($input_txt,$out_file);
      return 1;
    }
    else {
      $input_txt=$input->toString(1);
      $input_txt=&Ernad::Enesei::canonical_string($input_txt);
      $input_txt=~s|\n *|\n|g;
      &Ernad::Enesei::save($input_txt,$out_file);
      return 1;
    }
  }
  print "I keep the input\n";
  return 0;
}

sub remove_password {
  my $in=shift;
  if(not $xpc) {
    $xpc=$e->{'xpc'};
  }
  my $count_pw;
  foreach my $pw_ele ($xpc->find('//e:password',$in)->get_nodelist) {
    $count_pw++;
    $pw_ele->parentNode->removeChild($pw_ele);
  }
  #print "no password in $in";
  return $in;
}

sub get_last {
  my $repcode=shift;
  my $glob="$archive_dir/$repcode"."_*.amf.xml";
  my @files=glob($glob);
  my $last_file=$files[$#files];
  return $last_file;
}

sub is_different_without_blank {
  my $i1=shift;
  my $i2=shift;
  $i1=~s|\s+||g;
  $i2=~s|\s+||g;
  if($i1 ne $i2) {
    return 1;
  }
  return 0;
}



__END__;
