#! /usr/bin/gawk -f
# Last edited on 1999-01-06 15:33:42 by stolfi

BEGIN {
  
  usage = ( \
    "format-new-concordance \\\n" \
    "  [ -v title=TITLE ] \\\n" \
    "  [ -v prevPage=NUM ] \\\n" \
    "  [ -v nextPage=NUM ] \\\n" \
    "  [ -v majTran=CHAR ] \\\n" \
    "  -v maxLeft=LFT -v maxString=STR -v maxRight=RHT \\\n" \
    "  < INFILE.hoc \\\n" \
    "  > OUTFILE.html" \
  );

  # Each line of INFILE represents one occurrence of some string 
  # in the text, in the format
  # 
  #   LOC TRANS START LENGTH LCTX STRING RCTX PATT STAG PNUM HNUM
  #   1   2     3     4      5    6      7    8    9    10   11  
  #
  # See Note-037.txt for explanation of the fields.
  
  abort = -1;
  
  if (title == "")     { title = "Concordance"; }
  if (maxLeft == "")   { arg_error("must define \"maxLeft\""); }
  if (maxString == "") { arg_error("must define \"maxString\""); }
  if (maxRight == "")  { arg_error("must define \"maxRight\""); }
  if (majTran == "")   
    { print "warning: \"majTran\" empty, all versions hilighted\n" > "/dev/stderr"; }
  if (length(majTran) > 1) { arg-error("bad \"majTran\""); }

  ppat = " @@@ ";
  dashes = "-------------------------------------------------------------";
  
  color_pat     = "\"#ff5522\"";
  color_ctx     = "\"#dddddd\"";
  color_str_ord = "\"#ccbb33\"";
  color_str_maj = "\"#ffee33\"";
  color_dsh     = "\"#777777\"";
  color_loc     = "\"#33bbdd\"";
  color_sec     = "\"#33cc22\"";
  
  make_html_buttons();
  print_html_header();
}

/./ {
  loc = $1; tran = $2;
  lft = $5; str = $6; rht = $7;
  pat = $8; sec = $9;
  if (pat != ppat) 
    { print_pattern_header(pat);
      ppat = pat;
    }
  gsub(/[.]/, " ", lft); gsub(/[.]/, " ", str); gsub(/[.]/, " ", rht);
  print_entry(sec, loc, tran, left, str, rht);
}

function make_html_buttons()
{
  # Prepare the motion buttons:

  if ( prevPage != "" )
    { prevbt = ( "[<a href=\"" prevPage ".html\">PREV</a>] " ); }
  else
    { prevbt = ( "[    ] " ); }
  
  indexbt = ( "[<a href=\"index.html\">INDEX</a>] " );

  if ( nextPage != "" )
    { nextbt = ( "[<a href=\"" nextPage ".html\">NEXT</a>] " ); } 
  else
    { nextbt = ( "[    ] " ); }
}

function print_html_header()
{
  # print header:
  
  print "<html>";
  print "<head>";
  print ( "  <title>" title "</title>" );
  print "</head>";
  printf "<body bgcolor=\"#000000\" text=%s link=\"#00ffdd\" vlink=\"#009966\">",
     color_ctx;
  print "";
  print "<small>";
  printf "Generated %s by stolfi\n", strftime("%Y-%m-%d %H:%M:%S", systime());
  print "</small>";
  print "<b><tt><pre>";
  print "";
  print ( "<h1>" title "</h1>" );
  print "";
  print ( prevbt indexbt nextbt );
  print "";
  print "<small>";
}

function print_pattern_header(pat)
{
  printf "\n<a name=\"%s\"><font color=%s>%.*s ", pat, color_dsh, 25+maxLeft-1, dashes;
  printf "<font color=%s>%s</font>", color_pat, pat;
  printf " %.*s</font>\n\n", 80-(25+maxLeft)-length(pat)-1, dashes;
}

function print_entry(sec, loc, tran, left, str, rht,  cs,n,ent)
{
  n = length(lft); 
  if (n > maxLeft) { lft = substr(lft, 1+n-maxLeft); }
  cs = ( (majTran=="") || (index(tran, majTran) > 0) ? color_str_maj : color_str_ord );
  ent = sprintf("%*s<font color=%s>%s</font>%s", maxLeft, lft, cs, str, rht);
  printf "<font color=%s>%-3s</font> <font color=%s>%-12s %-6s</font>  %s\n",
    color_sec, sec, color_loc, loc, tran, ent;
}
  
function print_html_tailer()
{
  print "</small>";
  print "";
  print ( prevbt indexbt nextbt );
  print "";
  print "</pre></tt></b>";
  print "</body>";
  print "</html>";
}

function error(msg)
{ 
  printf "%s\n", msg > "/dev/stderr"
  abort = 1
  exit
}

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