#! /bin/csh -f
# Last edited on 1999-01-06 08:35:28 by stolfi

set usage = "$0 [ -title TITLE ] [ -prev NUM ] [ -next NUM ] [ -lengths LFT STR RHT ] < INFILE.hoc > 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.

set title = "Concordance"
set maxlft = 30
set maxstr = 20
set maxrht = 30
set prev = ""
set next = ""

while ( ( $#argv >= 1) && ( "x$1" =~ x-* ) ) 
  if ( ( $#argv >= 2) && ( "x$1" == "x-title" ) ) then
    set title = "$2";
    shift; shift;
  else if ( ( $#argv >= 4) && ( "x$1" == "x-lengths" ) ) then
    set maxlft = "$2"; set maxstr = "$3"; set maxrht = "$4"; 
    shift; shift; shift; shift;
  else if ( ( $#argv >= 2) && ( "x$1" == "x-prev" ) ) then
    set prev = "$2";
    shift; shift;
  else if ( ( $#argv >= 2) && ( "x$1" == "x-next" ) ) then
    set next = "$2";
    shift; shift;
  else
    echo "bad option $1"; echo "usage: ${usage}"; exit 1
  endif
end

# Prepare the motion buttons:

if ( "x$prev" != "x" ) then
  set prevbt = '[<a href="'"${prev}"'.html">PREV</a>] '
else
  set prevbt = '[    ] '
endif
set indexbt = '[<a href="index.html">INDEX</a>] ' 
if ( "x$next" != "x" ) then
  set nextbt = '[<a href="'"${next}"'.html">NEXT</a>] ' 
else
  set nextbt = '[    ] '
endif

# Output the HTML file: 

cat <<EOF
<html>
<head>
  <title>${title}</title>
</head>
<body bgcolor=#000000 text=#dddddd link=#00ffdd vlink=#009966>

<b><tt>
<pre>

<h1>${title}</h1>

${prevbt} ${indexbt} ${nextbt}

<small>
EOF

gawk \
  -v maxlft=${maxlft} \
  -v maxstr=${maxstr} \
  -v maxrht=${maxrht} \
  ' BEGIN { ppat = " @@@ "; \
            dashes = "-------------------------------------------------------------"; \
    } \
    /./ { \
      loc = $1; tran = $2; \
      lft = $5; str = $6; rht = $7; \
      pat = $8; sec = $9; \
      if (pat != ppat) { \
          printf "\n<a name=\"%s\"><font color=#777777>%.*s <font color=#ff5522>%s</font> %.*s</font>\n\n", \
            pat, \
            21+maxlft-1, dashes, \
            pat, \
            80-(21+maxlft-1)-length(pat)-1, dashes; \
          ppat = pat; \
        } \
      gsub(/[.]/, " ", lft); gsub(/[.]/, " ", str); gsub(/[.]/, " ", rht); \
      str = ("<font color=#ffee33>" str "</font>"); \
      n = length(lft); if (n > maxlft) { lft = substr(lft, 1+n-maxlft); } \
      printf "<font color=#33cc22>%3s</font> <font color=#33bbdd>%s</font>  %*s%-s\n", \
        sec, loc, maxlft, lft, (str rht); \
    } \
  ' 
  
cat <<EOF
</small>

${prevbt} ${indexbt} ${nextbt}

</pre>
</tt></b>
</body>
</html>
EOF