#!/usr/bin/env perl

use strict;

my $base=$ARGV[0];
my $update=$ARGV[1];
my %base2val;
my %baseval4type;
my %update2val;
my %updateval4type;

if (open(BASE,$base)) {
  while(<BASE>) {
    my ($var,$val)=split(/\s+/,$_);
    $base2val{$var}=$val;
    if ($var =~ m/([A-Z]+)_/) {
      my $type=$1;
      if (defined $baseval4type{$type,$val}) {
        print "INPUT ERROR: $var $val already used\n";
      }
      $baseval4type{$type,$val}=$var;
    }
  }
  close(BASE);
} else {
  print STDERR "unable to open $base - $!\n";
  exit(1);
}

if (open(UPDATE,$update)) {
  while(<UPDATE>) {
    my ($var,$val)=split(/\s+/,$_);
    #print "VAR $var\n";
    $update2val{$var}=$val;
    if ($var =~ m/([A-Z]+)_/) {
      my $type=$1;
      $updateval4type{$type,$val}=$var;
    }
  }
  close(UPDATE);
} else {
  print STDERR "unable to open $update - $!\n";
  exit(1);
}

foreach my $key (sort keys %update2val) {
  my $type;
  if ($key =~ m/([A-Z]+)_/) {
    $type=$1;
  }
  #print STDERR "key $key\n";
  if (!defined $base2val{$key}) {
    print "NEW $key " . $update2val{$key} . "\n";
  } elsif ($base2val{$key} ne $update2val{$key}) {
    print "CLASH $key " . $update2val{$key} . " instead of " . $base2val{$key} . "\n";
  }
}

foreach my $key (sort keys %base2val) {
  my $type;
  my $val;
  my $ukey;
  my $bkey;
  if ($key =~ m/([A-Z]+)_/) {
    $type=$1;
  }
  $val=$base2val{$key};
  #print STDERR "key $key\n";
  if (!defined $update2val{$key}) {
    print "MISSING $key " . $base2val{$key} . "\n";
    next;
  } elsif ($base2val{$key} ne $update2val{$key}) {
    print "CLASH $key " . $update2val{$key} . " instead of " . $base2val{$key} . "\n";
  }

  if ($type =~ m/(CKM|CKR|CKA)/) {

  $ukey=$updateval4type{$type,$val};
  $bkey=$baseval4type{$type,$val};

  print "key=\"$key\" ukey=\"$ukey\" bkey=\"$bkey\"\n";

  if ($updateval4type{$type,$val} ne $key) {
    print "REUSE $key " . $updateval4type{$type,$val} . " instead of " . $baseval4type{$type,$key} . "\n";
  }
  }

}

