#!/usr/bin/perl -w
# HOWTO: 1. only keep chardef. 
#        2. ./filter.pl other.cin original.cin > new.cin

open F, "cat $ARGV[0] |";
my (%soure, %target, @tmp);
while (<F>) {
  @tmp = split (' ', $_);
  if (exists($source{$tmp[0]})) {
    if ($source{$tmp[0]} =~ m/$tmp[1]/) { 
    }
    else {
      $source{$tmp[0]} .= " $tmp[1]";
    }
  }
  else {
    $source{$tmp[0]} = $tmp[1];
  }
}
close F;

open G, "cat $ARGV[1] |";
while (<G>) {
  @tmp = split (' ', $_);
  if (exists($target{$tmp[0]})) {
    if ($target{$tmp[0]} =~ m/$tmp[1]/) { 
    }
    else {
      $target{$tmp[0]} .= " $tmp[1]";
    }
  }
  else {
    $target{$tmp[0]} = $tmp[1];
  }
}
close G;

while ( ($first, $second) = each %source ) {
  if (exists($target{$first})) {
    if (defined($target{$first})) {
      @tmp = split (' ', $second);
      foreach $word (@tmp) {
        if ($target{$first} =~ m/$word/) { 
        }
        else {
          $target{$first} .= " $word";
        }
      }
    }
    else {
      $target{$first} = $second;
    }
  }
  else {
    $target{$first} = $second;
  }
}

print "%gen_inp\n";
print "%ename dayi3\n";
print "%cname 易(3)\n";
print "%selkey '[]-\\\"{}_\n";
print "%space_style 1\n";
print "%keep_key_case\n\n";

print "\#============BEGIAN OXIM==================\n";
print "\#%selkey '[]-\\\\\"\{\}\_\n";
print "\#%setting begin\n";
print "\#    AutoCompose \"Yes\"\n";
print "\#    AutoUpChar \"Yes\"\n";
print "\#    AutoFullUp \"Yes\"\n";
print "\#    SpaceAutoUp \"No\"\n";
print "\#    SelectKeyShift \"Yes\"\n";
print "\#    SpaceIgnore \"Yes\"\n";
print "\#    SpaceReset \"Yes\"\n";
print "\#    WildEnable \"Yes\"\n";
print "\#    EndKey \"No\"\n";
print "\#    DisableSelectList \"None\"\n";
print "\#%setting end\n";
print "\#============END OXIM==================\n";
print "\n";
print "%keyname begin\n";
print "1 言\n";
print "2 牛\n";
print "3 目\n";
print "4 四\n";
print "5 王\n";
print "6 門\n";
print "7 田\n";
print "8 米\n";
print "9 足\n";
print "0 金\n";
print "a 人\n";
print "b 馬\n";
print "c 七\n";
print "d 日\n";
print "e 一\n";
print "f 土\n";
print "g 手\n";
print "h 鳥\n";
print "i 木\n";
print "j 月\n";
print "k 立\n";
print "l 女\n";
print "m 雨\n";
print "n 魚\n";
print "o 口\n";
print "p 耳\n";
print "q 石\n";
print "r 工\n";
print "s 革\n";
print "t 糸\n";
print "u 艸\n";
print "v 禾\n";
print "w 山\n";
print "x 水\n";
print "y 火\n";
print "z 心\n";
print "` 巷\n";
print "- 鄉\n";
print "= 區\n";
print "[ 路\n";
print "] 街\n";
print "; 虫\n";
print "' 號\n";
print ", 力\n";
print ". 點\n";
print "/ 竹\n";
print "\\ 鎮\n";
print "%keyname end\n";
print "%chardef begin\n";

foreach $first ( sort keys %target) {
  @tmp = split (' ', $target{$first});
  foreach $word (@tmp) {
    print "$first $word\n";
  }
}

print "%chardef end\n";
