#! /bin/csh -f
# Last edited on 2000-07-10 08:46:30 by stolfi

set usage = "$0 [-v VAR=VALUE]... PAGES"

# Colorizes the specified pages and writes them to 
# files pages-html/${fnum}.html

set vars = ( )

while ( ( $#argv > 0 ) && ( "x$1" =~ x-* ) )
  if ( ( $#argv >= 2 ) && ( "x$1" == "x-v" ) ) then
    set vars = ( ${vars} "$1" "$2" ); shift; shift
  else
    echo "usage: ${usage}"; exit 1
  endif
end

set user = "${USER}"
set date = "`date +'%Y-%m-%d %H:%M:%S'`"

set hdir = "pages-html"
set cdir = "pages-clr"
set ddir = "pages-clr"
set edir = "pages-evt"
set ndir = "pages-pwct"

set prevs = ( "f0" $* )
set fnums = ( $* )
set nexts = ( $* f999 ); set nexts = ( ${nexts[2-]} )

foreach fnum ( ${fnums} )
  echo ${fnum}
  set pfnum = "${prevs[1]}"; set prevs = ( ${prevs[2-]} )
  set nfnum = "${nexts[1]}"; set nexts = ( ${nexts[2-]} )
  set hfile = "${hdir}/${fnum}.html"
  set cfile = "${cdir}/${fnum}.clr"
  set dfile = "${ddir}/${fnum}.spw"
  set efile = "${edir}/${fnum}.evt"
  cat page-header.html \
    | replace-html-vars \
        ${vars} \
        -v fnum="${fnum}" -v user="${user}" -v date="${date}" \
    > ${hfile}
  
  # Navigation menu
  if ( ${pfnum} != "f0" ) then
    echo '<b><a href="'"${pfnum}"'.html">Prev</a></b>' >> ${hfile}
  endif
  echo '<b><a href="index.html">Index</a></b>' >> ${hfile}
  if ( ${nfnum} != "f999" ) then
    echo '<b><a href="'"${nfnum}"'.html">Next</a></b>' >> ${hfile}
  endif

  # Text
  echo "<b><pre>" >> ${hfile}
  cat ${efile} \
    | colorize-text -f eva2erg.gawk \
        -v colorTable="${cfile}" \
        ${vars} \
    >> ${hfile}
  echo "</pre></b>" >> ${hfile}
  
  # Special word list
  echo "<h2>Special words</h2>" >> ${hfile}
  echo "<b><pre>" >> ${hfile}
  cat ${dfile} \
    | sort -b +2 -3gr \
    | gawk \
        ${vars} \
        ' BEGIN { \
            printf "%*s%-16s count totct strangss words\n", indent, "", "pattern"; \
            printf "%*s%-16s ----- ----- -------- -----\n", indent, "", "---------------"; \
          } \
          /./{ \
            pgCt=$1; scCt=$2; strang=$3; color=$4; fnum=$5; knum=$6; pat=$7; wds=$8; \
            printf "%*s<font color=\"#%s\">%-16s %5d %5d %8.4f %s</font>\n", \
              indent, "", color, ( pat "~"), pgCt, scCt, strang, wds; \
          } \
          END { \
          } \
        ' \
    >> ${hfile}

  # Count words
  cat ${ndir}/${fnum}.pwct \
    | gawk \
        ${vars} \
        ' /./{ s += $1; } \
          END{ \
            printf "\n%*sTotal tokens in page = %d\n", indent, "", s+0; \
          } \
        ' \
    >> ${hfile}

  echo "</pre></b>" >> ${hfile}

  # Close off page
  cat page-trailer.html \
    | replace-html-vars \
        ${vars} -vfnum="${fnum}" -v user="${user}" -v date="${date}" \
    >> ${hfile}

end