#! /bin/bash -eu
# Last edited on 2026-04-21 22:16:07 by stolfi

min_wlen="$1"; shift
max_wlen="$1"; shift
max_tlen="$1"; shift
min_wpl="$1"; shift
run_prefix_A="$1"; shift
title_A="$1"; shift
run_prefix_B="$1"; shift
title_B="$1"; shift
plot_prefix="$1"; shift
top_title="$1"; shift

out_dir="res"
ex_dir="work/VoynichNinja/005"

stats_name_A="${run_prefix_A}_stats"
stats_name_B="${run_prefix_B}_stats"

max_nw=$( echo "scale = 0; (${max_tlen} + 4)/(${min_wlen} + 1)" | bc -lq )
echo "expected max words per line = ${max_nw}" 1>&2

temp="/tmp/$$"

stats_file_A="${out_dir}/${stats_name_A}.txt"
stats_file_B="${out_dir}/${stats_name_B}.txt"
wc -l ${stats_file_A} ${stats_file_B} 1>&2

data_files_A=()
data_files_B=()
for plot in 0 1 2; do
  for curve in A B; do
    echo " extracting data for curves ${plot} ${curve}..." 1>&2
    if [[ ${curve} == "A" ]]; then 
      stats_file="${stats_file_A}"
    else 
      stats_file="${stats_file_B}"
    fi
    data_file="${temp}-${plot}_${curve}.txt"
    cat ${stats_file} \
      | gawk \
          -v maxw=${min_wpl} \
          ' /PLOT *'"${plot}"'/ { 
              ptype = $2; kw = $3+0; 
              if ((ptype == 0) || ((kw <= maxw) && (kw >= -maxw)))
                { print } 
            }
          ' \
      > ${data_file}
    if [[ ${curve} == "A" ]]; then 
      data_files_A+=( ${data_file} )
    else 
      data_files_B+=( ${data_file} )
    fi
  done
done
wc -l "${data_files_A[@]}" "${data_files_B[@]}"

export GDFONTPATH="${HOME}/ttf"

plot_hsize=800
plot_vsize=600
hsize=$(( ${plot_hsize} * 2 ))
vsize=$(( ${plot_vsize} * 2 ))

echo "raw plot of average length ..." 1>&2
temp_plot_file="${temp}_plot.png"

gnuplot <<EOF
  set term pngcairo size ${hsize},${vsize} linewidth 2.0 font "Arial,30" noenhanced
  set output "${temp_plot_file}"

  set multiplot layout 1,2 \
    spacing character 5.0 \
    title "${top_title}"

  set xtics 1
  set nomxtics

  set yrange [1.8:7.2]
  set ytics 0.5 rotate by 90 center
  set mytics 5

  set grid xtics mxtics lt 1 lc rgb "#44aaff", lt 1 lc rgb "#aaddff"
  set grid ytics mytics lt 1 lc rgb "#44aaff", lt 1 lc rgb "#aaddff"

  set ylabel "average length" font "Arial,30" offset +2.5,0

  val(k) = column(4)
  xmax = ${min_wpl} + 1

  set xrange [0:xmax]
  set xlabel "word position from start" font "Arial,30" offset 0,0.3
  set lmargin 6.5; set rmargin 2.0; set bmargin 4.0
  plot \
    "${data_files_A[0]}" using 3:(val(0)) notitle with lines lw 4 lc rgb "#77aa33", \
    "${data_files_B[0]}" using 3:(val(0)) notitle with lines lw 4 lc rgb "#77aa33", \
    "${data_files_A[1]}" using 3:(val(0)) title "${title_A}" \
      with linespoints pt 7 ps 2.0 \
      lc rgb "#ff0000", \
    "${data_files_B[1]}" using 3:(val(0)) title "${title_B}" \
      with linespoints pt 7 ps 2.0 \
      lc rgb "#0033ff"

  set xrange [(-xmax):0]
  set xlabel "word position from end" font "Arial,30" offset 0,0.3
  set lmargin 6.5; set rmargin 2.0
  plot \
    "${data_files_A[0]}" using 3:(val(0)) notitle with lines lw 4 lc rgb "#77aa33", \
    "${data_files_B[0]}" using 3:(val(0)) notitle with lines lw 4 lc rgb "#77aa33", \
    "${data_files_A[2]}" using 3:(val(0)) title "${title_A}" \
      with linespoints pt 7 ps 2.0 \
      lc rgb "#ff0000", \
    "${data_files_B[2]}" using 3:(val(0)) title "${title_B}" \
      with linespoints pt 7 ps 2.0 \
      lc rgb "#0033ff"
  quit
EOF

echo "converting plots of average length ..." 1>&2
plot_name="${plot_prefix}_plot"
plot_file="${out_dir}/${plot_name}.png"
export_plot_file="${ex_dir}/${plot_name}.jpg"
rm -f ${plot_file} ${export_plot_file}
if [[ -s ${temp_plot_file} ]]; then
  convert ${temp_plot_file} -resize '50%' ${plot_file}
  convert ${plot_file} ${export_plot_file}
  display -title '%f' ${plot_file}
else
  echo "** ${temp_plot_file} not created" 1>&2; exit 1
fi

rm -f ${temp}-*
