#! /bin/bash -eu
# Last edited on 2026-01-20 10:35:31 by stolfi

mkdir -p out

# Set of sections and its p(normal) value:
sps=str:0.8222
# sps=hea:0.8962

tag="${sps/:*}"
pnorm="${sps/*:}"

secs=( $( echo ${tag} | tr '\055' ' ' ) )

tmp="/tmp/$$"

echo "combining sections ${secs[@]} ..." 1>&2
dfile="out/${tag}.ivt"
rm -f ${dfile}
for sec in ${secs[@]} ; do 
  ifile="st_unparsed/${sec}-parags.ivt"
  cat ${ifile} >> ${dfile}
done

echo "extracting the paragraphs as word lists ..." 1>&2
# Must do this part separately so that the shell fails
# if {words_from_ivt.py} fails.
wfile="out/${tag}.wds"
cat ${dfile} \
  | gawk \
      ' // {
          gsub(/[{}]/, "", $2)
          $2 = tolower($2)
          gsub(/[,]oqo/, ".oqo", $2)
          gsub(/[,]qo/, ".qo", $2)
          print
        }
      ' \
  | words_from_ivt.py \
      -showParags \
      -dubiousSpaces 0 \
  > ${wfile}
  
echo "collecting the gap data ..." 1>&2
gfile="out/${tag}.gaps"
cat ${wfile} \
  | gawk '//{ ipar = $2; if (ipar != 0) { print } }' \
  | count_qq_gaps.py \
  > ${gfile}

for type in BQ QQ QE BE ; do
  pfile="out/${tag}-${type}-hist.png"
  cat ${gfile} \
    | plot_qq_gap_histogram.sh ${tag} ${type} ${pnorm} \
    > ${pfile}
done
