#! /bin/bash -eu
# Last edited on 2026-01-17 13:30:07 by stolfi

for ophd in "-v:body" ":head"; do 
  op="${ophd/:*}"
  hd="${ophd/*:}"
  printf "#0                                       ${hd}                              \n" 1>&2
  printf "#1       -------------------------------------------------------------------\n" 1>&2
  printf "#2 sec " 1>&2 
  printf "     nl    nlp  %%lp    nlt  %%lt      nc    ncp  %%cp    nct  %%ct   %%cg" 1>&2
  printf "\n" 1>&2
  printf "#3 --- " 1>&2 
  printf "  -----  ----- ----  ----- ----   -----  ----- ----  ----- ----  ----" 1>&2
  printf "\n" 1>&2

  for f in st_files/*-parags.ivt; do
    sec=$( echo "$f" | sed -e 's:^.*/::g' -e 's:[-].*$::g' )
    printf "|  ${sec} " 1>&2
    # Extract body or head lines: 
    cat $f \
      | egrep ${op} -e '<%>' \
      | gawk '//{ print $2 }' \
      > .tut
    nl=$( cat .tut | wc -l )
    nc=$( cat .tut | tr -c -d 'a-z\012' | wc -c )
    printf "  %5d" ${nl} 1>&2 
    for chars in "pfzw" "kt"; do 
      # Count lines with puffs or kites:
      nl_x=$( cat .tut | egrep -e "[${chars}]" | wc -l )
      pl_x=$( echo "99.9*${nl_x}/(${nl}+0.01)" | bc -lq )
      printf "  %5d %4.1f" ${nl_x} ${pl_x} 1>&2 
    done
    nc=$( cat .tut | tr -c -d 'a-z' | wc -c )
    printf "   %5d" ${nc} 1>&2 
    for chars in "pfzw" "kt"; do 
      # Count puffs and kites:
      nc_x=$( cat .tut | tr -c -d "${chars}" | wc -c )
      pc_x=$( echo "99.9*${nc_x}/(${nc}+0.01)" | bc -lq )
      printf "  %5d %4.1f" ${nc_x} ${pc_x} 1>&2
    done
    # Count total gallows:
    nc_g=$( cat .tut | tr -c -d 'ktpfzw' | wc -c )
    pc_g=$( echo "99.9*${nc_g}/(${nc}+0.01)" | bc -lq )
    printf "  %4.1f" ${pc_g} 1>&2
    printf "\n" 1>&2
  done
  printf "\n" 1>&2
done    
