#! /usr/bin/gawk -f
# Last edited on 1998-12-12 05:43:02 by stolfi

# Recoding the bug log (f0.W) from EVA back to FSG,
# so that the bug fixes can be applied to the interlinear.

function eva_to_fsg(txt)
{
  # Converts a chunk of comment-free text from EVA to FSG 
  
  # We discard  "%" and "!" since the conversion
  # will destroy synchronism anyway.
  gsub(/[% !]/, "", txt);
  
  # Now convert 
  gsub(/iiil/,  "IIIE", txt);
  gsub(/iiim/,  "IIIK", txt);
  gsub(/iiin/,  "IIIL", txt);
  gsub(/iiir/,  "IIIR", txt);
  gsub(/iil/,   "IIE",  txt);
  gsub(/iim/,   "IIK",  txt);
  gsub(/iin/,   "M",    txt);
  gsub(/iir/,   "IIR",  txt);
  gsub(/il/,    "IE",   txt);
  gsub(/im/,    "IK",   txt);
  gsub(/in/,    "N",    txt);
  gsub(/ir/,    "IR",   txt);
  gsub(/cth/,   "HZ",   txt);
  gsub(/cph/,   "PZ",   txt);
  gsub(/ckh/,   "DZ",   txt);
  gsub(/cfh/,   "FZ",   txt);
  gsub(/sh/,    "S",    txt);
  gsub(/ch/,    "T",    txt);
  gsub(/s/,     "2",    txt);
  gsub(/q/,     "4",    txt);
  gsub(/g/,     "6",    txt);
  gsub(/j/,     "7",    txt);
  gsub(/d/,     "8",    txt);
  gsub(/a/,     "A",    txt);
  gsub(/e/,     "C",    txt);
  gsub(/l/,     "E",    txt);
  gsub(/t/,     "H",    txt);
  gsub(/k/,     "D",    txt);
  gsub(/p/,     "P",    txt);
  gsub(/f/,     "F",    txt);
  gsub(/y/,     "G",    txt);
  gsub(/i/,     "I",    txt);
  gsub(/m/,     "K",    txt);
  gsub(/n/,     "L",    txt);
  gsub(/o/,     "O",    txt);
  gsub(/r/,     "R",    txt);
  gsub(/v/,     "V",    txt);
  gsub(/x/,     "Y",    txt);
  
  return txt
}

function convert_evmt(old,  i,neu)
{
  # Converts an EVMT string, possibly with {}-comments, while
  # preserving the text outside.
  neu = "";
  while (length(old) != 0)
    { i = index(old, "{");
      if (i == 0)
        { neu = (neu eva_to_fsg(old)); old = ""; }
      else if (i > 1)
        { neu = (neu eva_to_fsg(substr(old, 1, i-1)));
          old = substr(old, i);
        }
      else
        { match(old, /^{[^}]*}/);
          if (RSTART > 0) 
            { neu = (neu substr(old, 1, RLENGTH));
              old = substr(old, RLENGTH + 1);
            }
          else
            { printf "line %d, missing `\}'\n", FNR > "/dev/stderr";
              neu = (neu old); old = "";
            }
        }
    }
  return neu;
}

function convert_log_entry(old,  i,neu)
{
  # Converts the text inside quoted strings, leaving the rest unchanged.
  neu = "";
  while (length(old) != 0)
    { i = index(old, "\"");
      if (i == 0)
        { neu = (neu old); old = ""; }
      else if (i > 1)
        { neu = (neu substr(old, 1, i-1));
          old = substr(old, i);
        }
      else
        { i = index(substr(old, 2), "\"") + 1;
          if (i < 2) 
            { printf "line %d, missing `\"'\n", FNR > "/dev/stderr";
              neu = (neu old); old = "";
            }
          else
            { neu = (neu convert_evmt(substr(old, 1, i)));
              old = substr(old, i + 1);
            }
        }
    }
  return neu;
}

/^ *$/ { print; next }

/./ { print convert_log_entry($0); next; }