#! /bin/bash -eu
# Last edited on 2026-01-17 23:31:05 by stolfi

var="$1"; shift # Either 'wff' or 'rff'

HEA_WFF="st_words/hea-parags.${var}"
HEB_WFF="st_words/heb-parags.${var}"

if [[ "/${var}" == "/wff" ]]; then which="words"; else which="roots"; fi

tpref="/tmp/$$"

# Join the frequency files:
afile="${tpref}_a.wff"
cat ${HEA_WFF} | compute_freqs.gawk | sort -b -k3,3 > ${afile}

bfile="${tpref}_b.wff"

cat ${HEB_WFF} | compute_freqs.gawk | sort -b -k3,3 > ${bfile}

jfile="${tpref}_ab.wfff"
join -1 3 -2 3 -a 1 -a 2 -e 0 -o1.2,2.2,0 ${afile} ${bfile} > ${jfile}

export GDFONTPATH="${HOME}/ttf"
tplot="${tpref}_plot.png"

gnuplot <<EOF
  set term png size 1600,1600 linewidth 2.0 font "Arial" 20
  set output "${tplot}"
  set xrange [0.0005:]
  set yrange [0.0005:]
  # ftics = 1.0; mftics = 10
  # set xtics ftics; set mxtics mftics
  # set ytics ftics; set mytics mftics
  set grid xtics mxtics lc rgb "#44aaff", lc rgb "#aaddff"
  set grid ytics mytics lc rgb "#44aaff", lc rgb "#aaddff"
  set lmargin 7
  set logscale 
  plot \
    (x) notitle with lines lc rgb "#ff2200", \
    "${jfile}" using 1:2 title "hea x heb parags ${which} freqs" \
      with points pt 7 ps 2 \
      lc rgb "#aa9966", \
    "" using 1:2:3 notitle \
      with labels offset character 1,0 left \
      lc rgb "#000000"
  quit
EOF

oplot="hea_heb_${var}_plot.jpg"
exdir="${HOME}/projects/voynich/VoynichNinja/092"
mkdir -p ${exdir}
rm -f ${exdir}/${oplot}
if [[ -s ${tplot} ]]; then
  convert ${tplot} -resize '50%' st_words/${oplot}
  cp -av st_words/${oplot} ${exdir}/${oplot}
  display -title '%f' st_words/${oplot}
else
  echo "** ${tplot} not created" 1>&2; exit 1
fi
 
