#!/usr/bin/perl -w

use strict;
use warnings;

use Data::Dumper;
use File::Slurper;
use Ernad::Erimp;
use Krichel::File;

my $date_base_file;
my $date_base;

my $out_file='/tmp/out.json';

my $dbf=$ARGV[0] // '';
##  first argument
if(not -f $dbf) {
  print "I need a db file as the first argument\n";
  exit;
}

my $missing_file=$ARGV[1] // '';
if(not -f $missing_file) {
  print "I need a missisng file as the second argument\n";
}

my $db = Krichel::File::load( $dbf );

my @missing=read_lines($missing_file);

## test if all missing are actually there

foreach my $m (@missing) {
  chomp $m;
  if(not $db->{$m}) {
    print "The handle $m is $missing_file is not in the database\n";
  }
}

my $o;
my $miss;
foreach my $m (@missing) {
  chomp $m;
  if(not $db->{$m}) {
    print "The handle $m is $missing_file is not in the database\n";
    exit;
  }
  $miss->{$m}=1;
}

foreach my $h (keys %{$db}) {
  if($miss->{$h}) {
    next;
  }
  $o->{$h}=$db->{$h};
}

&Krichel::File::save($out_file,$o)

print "I write $out_file\n";

exit;
