#! /usr/bin/gawk -f
# Last edited on 1998-12-31 04:42:05 by stolfi

BEGIN {
  abort = -1;
  usage = ( \
      "show-discrepancies \\\n" \
      "  -f tuple-procs.gawk \\\n" \
      "  -v title=TITLE \\\n" \
      "  -v dir=DIR \\\n" \
      "  < EVTFILE " \
    );
    
  # Reads an interlinear file in EVMT format (EVA encoding) 
  # and writes an HTML file that shows the differences 
  # position by position.
  
  if (dir == "") { arg_error("must define \"dir\""); }
  if (title == "") { arg-error("must define \"title\""); }
  
  tup_clear_current_batch();
  
  # Colors
  bg_color      = "\"#000000\"";
  maj_color     = "\"#ffff77\"";
  text_color    = "\"#ffdd55\"";
  link_color    = "\"#77ffff\"";
  vlink_color   = "\"#55ff77\"";
  cons_color    = "\"#ddff11\"";
  loc_color     = "\"#77dd99\"";
  header_color  = "\"#ff7755\"";
  comment_color = "\"#77dd99\"";
  
  page = "";
  ofile = (dir "/legend.html");
  print_html_header();
  print_legend();
  print_html_tailer();
  
  ofile = (dir "/junk.html");
  print_html_header();
  
  npages = 0;
}

//{ if (abort >= 0) { exit abort; } }

# Blank line
/^ *$/ {
  next;
}

# `##'-comment (page header)
/^([#][#] *|)<[^<>;.]*>/ {
  txt = $0;
  gsub(/^## */, "", txt);
  gsub(/[<>]/, "", txt);
  gsub(/{[^{}]*}/, "", txt);
  gsub(/[ ]/, "", txt);
  tup_process_current_batch("");
  print_html_tailer();
  page = txt;
  pglist[npages] = page; npages++;
  ofile = (dir "/" page ".html");
  print_html_header()
  print_page_header(("page " txt));
  printf "page = \"%s\"\n", page > "/dev/stderr";
  next;
}

# Other `#'-comment
/^[#]/ {
  if (tup_nb == 0) 
    { print_comment($0); }
  else
    { tup_append_line_to_batch($0); }
  next;
}

# Text line
/^</ {
  tup_process_variant($0,"");
  next;
}

# Other lines
/./ {
  fatal_error("bad line format"); 
  print_line();
  next;
}

END {
  if (abort >=0) { exit abort; }
  tup_process_current_batch("");
  print_html_tailer();
  
  # Generate index page:
  ofile = (dir "/index.html");
  print_html_header();
  print_page_header("Index");
  printf "</pre><font color =%s>\n", header_color > ofile;
  print_index_entry("legend", "trans codes");
  for (i=0; i<npages; i++)
    { print_index_entry(pglist[i], pglist[i]); }
  printf "</font><pre>\n" > ofile;
  print_html_tailer();
}

# Client functions called by tup_process_current_batch:

function process_batch_texts(loc,txt,trn,nv,nc,   i,col)
{
  # Print them nicely formatted
  compare_versions(txt,nv,nc);
  if (last_type != "loc") { printf "</font><font color=%s>", loc_color > ofile; }
  printf "\n" > ofile;
  printf "\n" > ofile;
  if (substr(loc,1,length(page)+1) == (page ".")) 
    { loc = substr(loc,length(page)+2); }
  if (match(loc, /[0-9]$/)) { loc = (loc " "); }
  loc = sprintf("%-7s", loc);
  for(i=0; i<nv; i++)
    { if (i == 0)
        { col = maj_color; }
      else if (i == nv-1) 
        { col = cons_color; }
      else
        { col = text_color; }
      printf "%10s %s  |<font color=%s>%s</font>|\n", \
        loc, substr("ABCDEFGHIJKLMNOPQRSTUVWXYZ",trn[i],1), col, protect(txt[i]) \
        > ofile;
      loc = "";
    }
  last_type = "loc";
}

function process_batch_lines(batch,nb, i)
{
  # Called with the original (and new) lines, including comments.
  # Print the comments only
  for (i=0; i<nb; i++)
    { lin = batch[i];
      if (lin ~ /^#/) { print_comment(lin); }
    }
}

function print_html_header()
{
  # Print header:
  printf "<html>\n" >> ofile;
  printf "<head>\n" > ofile;
  printf "<title>%s</title>\n", protect(title) > ofile;
  printf "</head>\n" > ofile;
  printf "<body bgcolor=%s text=%s link=%s vlink=%s>\n", \
    bg_color, comment_color, link_color, vlink_color > ofile;
  printf "<small><p>Created %s by J. Stolfi</small></p>\n", \
    protect(strftime("%Y-%m-%d %H:%M:%S")) > ofile;
  printf "<h1><font color=%s>%s</font></h1>\n", header_color, protect(title) > ofile;
  printf "<b><tt><pre><font color=%s>\n", comment_color > ofile;
  last_type = "comment";
}

function print_html_tailer()
{
  printf "</font>\n" > ofile;
  printf "</pre></tt></b>\n" > ofile;
  printf "</body>\n" > ofile;
  printf "</html>\n" > ofile;
  close(ofile);
}

function print_legend()
{
  print_page_header("Transcription codes") > ofile;
  printf "<font color=%s>  A = majority of independent versions.</font>\n", maj_color > ofile;
  printf "\n" > ofile;
  printf "<font color=%s>  C: Second Study Group punchcards (P. Currier, M. D'Imperio, et al.).</font>\n", text_color > ofile;
  printf "<font color=%s>  F: First Study Group punchcards (W. Friedman et al.).</font>\n", text_color > ofile;
  printf "<font color=%s>  H: Takeshi Takahashi.</font>\n", text_color > ofile;
  printf "<font color=%s>  J: Jim Reeds.</font>\n", text_color > ofile;
  printf "<font color=%s>  K: Karl Kluge.</font>\n", text_color > ofile;
  printf "<font color=%s>  L: Don Latham.</font>\n", text_color > ofile;
  printf "<font color=%s>  N: Gabriel Landini.</font>\n", text_color > ofile;
  printf "<font color=%s>  P: Father Th. Petersen (reported by K. Kluge).</font>\n", text_color > ofile;
  printf "<font color=%s>  R: Mike Roe.</font>\n", text_color > ofile;
  printf "<font color=%s>  T: John Tiltman.</font>\n", text_color > ofile;
  printf "<font color=%s>  U: Jorge Stolfi.</font>\n", text_color > ofile;
  printf "<font color=%s>  V: John Grove.</font>\n", text_color > ofile;
  printf "<font color=%s>  X: Denis V. Mardle.</font>\n", text_color > ofile;
  printf "<font color=%s>  Z: Rene Zandbergen.</font>\n", text_color > ofile;
  printf "\n" > ofile;
  printf "<font color=%s>  D: second choice from [|] in "C" lines.</font>\n", text_color > ofile;
  printf "<font color=%s>  G: second choice from [|] in "F" lines.</font>\n", text_color > ofile;
  printf "<font color=%s>  I: second choice from [|] in "J" lines.</font>\n", text_color > ofile;
  printf "<font color=%s>  M: second choice from [|] in "L" lines.</font>\n", text_color > ofile;  
  printf "<font color=%s>  Q: second choice from [|] in "K" lines.</font>\n", text_color > ofile;
  printf "\n" > ofile;
  printf "<font color=%s>  Y = consensus of all versions.</font>\n", cons_color > ofile;
  printf "\n" > ofile;
}

function print_page_header(title)
{
  printf "</font></pre>\n" > ofile;
  printf "<h2><font color=%s>%s</font></h2>\n", header_color, protect(title) > ofile;
  printf "<pre><font color=%s>\n", comment_color > ofile;
  last_type = "comment";
}

function print_comment(lin)
{
  if (last_type != "comment") 
    { printf "</font><font color=%s>\n", comment_color > ofile; }
  gsub(/^#[ ]?/, "", lin);
  printf "%s\n", protect(lin) > ofile;
  last_type = "comment";
}

function print_index_entry(fname, title)
{
  printf "[<a href=\"%s.html\">%s</a>] ", fname, title > ofile;
}

function protect(lin)
{
  gsub(/[&]/, "\\&amp;", lin);
  gsub(/[<]/, "\\&lt;", lin);
  gsub(/[>]/, "\\&gt;", lin);
  return(lin);
}

function compare_versions(txt,nv,nc,  i,j,new,m,c,d,and,tup)
{
  # For each character position, compares the versions "ver[i]", "i=0..ntr-1".
  # If all versions agree, erases all characters except that of "ver[0]". 
  # If there is any disagreement, leaves that character unchanged in all versions.
  
  # Initialize all "new" entries
  split("", new);
  new[0] = "";
  for (i=0; i<nv; i++) 
    { new[i] = "";
      if (nc != length(txt[i])) 
        { fatal_error(("diff lengths [" txt[i] "]")); }
    }

  for (j=1; j<=nc; j++)
    { c = "%";
      and = 1;
      for (i=0; i<nv; i++) 
        { d = substr(txt[i], j,1);
          if (c == "%")
            { c = d; }
          else if (d == "%")
            { }
          else
            { if (c != d) { and = 0; } }
        }
      
      if (and)
        { new[0] = (new[0] substr(txt[0], j,1))
          for (i=1; i<nv-1; i++) { new[i] = (new[i] " "); }
          new[nv-1] = (new[nv-1] substr(txt[nv-1], j,1))
        }
      else
        { for (i=0; i<nv; i++) { new[i] = (new[i] substr(txt[i], j,1)); } }
      
    }
  for (i=0; i<nv; i++) { txt[i] = new[i]; }
}

function arg_error(msg)
{
  printf "*** %s\n", msg > "/dev/stderr"; 
  printf "usage: %s\n", usage > "/dev/stderr"; 
  abort = 1; exit abort;
}

function fatal_error(msg)
{
  printf "file %s, line %d: *** %s\n", FILENAME, FNR, msg > "/dev/stderr"; 
  abort = 1; exit abort;
}
    
function format_error(msg)
{
  printf "file %s, line %d: %s\n", FILENAME, FNR, msg > "/dev/stderr";
}

function print_line()
{
  printf "file %s, line %d: %s\n", FILENAME, FNR, $0 > "/dev/stderr";
  printf "\n" > "/dev/stderr";
}