#! /bin/csh 
# Last edited on 2025-05-01 18:24:02 by stolfi

set usage = "$0 [ -lc ] [ -dg ] DIR LNUM RNUM HMIN NCOLORS HMAX"

set lcase = 0; set dgspaces = '0123456789'
while ( ( $#argv > 0) && ("x$1" =~ x-* ) )
  if ( ( $#argv >= 1) && ("x$1" == "x-lc" ) ) then
    set lcase = 1; shift;
  else if ( ( $#argv >= 1) && ("x$1" == "x-dg" ) ) then
    set dgspaces = ''; shift;
  else
    echo "usage: $usage"; exit 1
  endif
end

if ($#argv < 6) then
  echo "usage: $usage"; exit 1
endif

set dir  = "$1"; shift;
set lnum = "$1"; shift;
set rnum = "$1"; shift;
set hmin  = "$1"; shift;
set ncol  = "$1"; shift;
set hmax  = "$1"; shift;

if ( ! ( -d $dir ) ) then
  echo "$dir not a directory"; exit 1
endif

@ order = ${lnum} + 1 + ${rnum}
@ place = ${lnum} + 1

if ( $#argv > 0 ) then
  echo "usage: ${usage}"; exit 1;
endif

set name = "${dir}/L${lnum}R${rnum}";
set tmp = "/tmp/$$"

echo "  extract-signif-chars|gather-tuples|uniq --> ${name}.cts"
cat ${dir}/full.txt \
  | nice extract-signif-chars \
      -v blanks="${dgspaces}" \
      -v normal="-'" \
      -v errors='_' \
  | nice gather-tuples \
      -v order=${order} \
      -v filler='_' \
      -v lowercase=${lcase} \
  | sort | uniq -c | expand \
  > ${name}.cts

echo "  compute-cond-tuple-info --> ${name}.inf"
 ( cat ${name}.cts \
    | nice compute-cond-tuple-info-new \
        -v order=${order} \
        -v place=${place} \
    | sort -k1gr \
    > ${name}.inf \
 ) >& ${name}.cmt

set ofile = "${name}.html"

echo "  extract-signif-chars|colorize-text-by-tuple --> ${ofile}"
echo "<html><head><title>${name} context: L ${lnum}  R ${rnum}  ord ${order}</title></head>" > ${ofile}
echo "<body bgcolor=#000000>" >> ${ofile}
echo "<b><tt><pre>" >> ${ofile}
echo "<font color=#b4b4b4>" >> ${ofile}
cat ${dir}/page.txt \
  | nice extract-signif-chars \
      -v blanks="${dgspaces}" \
      -v normal="-'" \
      -v errors='_'  \
  | nice colorize-text-by-tuple-new \
      -v order=${order} \
      -v place=${place} \
      -v filler='_' \
      -v lowercase=${lcase} \
      -v table=${name}.inf \
      -v hmin=${hmin} \
      -v hmax=${hmax} \
      -v ymin=0.350 \
      -v colors=${ncol} \
  >> ${ofile}
cat ${name}.cmt \
  >> ${ofile}
echo "</font>" >> ${ofile}
echo "</pre></tt></b>" >> ${ofile}
echo "</body>" >> ${ofile}
echo "</html>" >> ${ofile}

# /bin/rm ${tmp}-full.txt ${tmp}-page.txt

