#! /bin/bash -eu
# Last edited on 2026-06-02 16:30:26 by stolfi

# Arguments are a name and title  for the whole plot, then 
# names and titles of two files with repeated sequence counts.
# 
# Reads two files with histograms of repeated sequence counts.
# 
# File 1 is "res/{hist_name_1}.rct" and similarly of the other.
# Each file should have two columns {k} {ct[k]} where {ct[k]}
# is supposed to be the number of repeated {k}-token sequences 
# in some text, for {k} between 2 and 10.
#
# Plots those histograms side by side on the same plot, with various colors.
# 

plot_name="$1"; shift    # Name (sans folder and extension) of output plot file.
plot_title="$1"; shift   # Title for plot as a whole.

hist_name_0="$1"; shift  # Name (sans folder and extension) of data file for histogram 0.
title_0="$1"; shift      # Title for legend of that histogram.

hist_name_1="$1"; shift  # Name (sans folder and extension) of data file for histogram 1.
title_1="$1"; shift      # Title for legend of that histogram.

echo "=== $0 ===" 1>&2
echo "  hist_name_0 = '${hist_name_0}'  title_0 = '${title_0}'" 1>&2
echo "  hist_name_1 = '${hist_name_1}'  title_1 = '${title_1}'" 1>&2

temp="/tmp/$$"

hist_names=( ${hist_name_0} ${hist_name_1} )
titles=( "${title_0}" "${title_1}" )

hpoly_files=()
for which in 0 1; do
  hist_name="${hist_names[$which]}"
  hist_file="res/${hist_name}.rct"

  echo "converting ${hist_file} into polygonal line ..." 1>&2 
  hpoly_file="${temp}_${hist_name}.dat"
  cat ${hist_file} \
    | gawk \
        ' /^ *[0-9]/ { 
            k = $1; ct = $2; 
            printf "%5.1f %5.1f %5.1f %d\n", 
              k-0.5, k, k+0.5, ct
          }
        ' \
    | turn_histogram_into_polygonal_line.gawk \
        -i error_funcs.gawk \
        -v num=2 -v which=${which} \
    > ${hpoly_file}
  hpoly_files+=( ${hpoly_file} )
done

export GDFONTPATH=ttf

colors=( '#0077ff' '#bb3300' )

temp_plot="${temp}-big.png"
echo "creating plot ..." 1>&2 
gnuplot <<EOF

which = ${which}

big_hsize = 2400
big_vsize = 500

set term pngcairo size (big_hsize),(big_vsize) font "arial,20" noenhanced
set output "${temp_plot}"

set xrange [1.5:12.5]
set xtics 1
set nomxtics 

gol(ct) = log(0.5 + ct)
golc(k) = gol(column(k))

set yrange [(gol(-0.1)):(gol(11000))]
set ytics ( \
    "0" (gol(0)) 0, \
    "1" (gol(1)) 0, \
    "2" (gol(2)) 1, \
    "5" (gol(5)) 1, \
    "10" (gol(10)) 0, \
    "20" (gol(20)) 1, \
    "50" (gol(50)) 1, \
    "100" (gol(100)) 0, \
    "200" (gol(200)) 1, \
    "500" (gol(500)) 1,  \
    "1000" (gol(1000)) 0, \
    "2000" (gol(2000)) 1, \
    "5000" (gol(5000)) 1, \
    "10000" (gol(10000)) 0, \
    "20000" (gol(20000)) 1, \
    "50000" (gol(50000)) 1 \
  )

set xzeroaxis
set grid ytics
set grid xtics
set xlabel "Repeat length (tokens)"
set ylabel "Num repeats"

set title "${plot_title}"

plot \
  "${hpoly_files[0]}" using 1:(golc(2)) title "${titles[0]}" with filledcurves lw 2 lc rgb '${colors[0]}', \
  "${hpoly_files[1]}" using 1:(golc(2)) title "${titles[1]}" with filledcurves lw 2 lc rgb '${colors[1]}'

quit
EOF

echo "rescaling plot ..." 1>&2 
if [[ -s ${temp_plot} ]]; then
  good_plot="res/${plot_name}-rep-hist.png"
  convert ${temp_plot} -resize '50%' ${good_plot}
  display ${good_plot}
  rm ${temp}-*
else
  echo "** ${temp_plot} not generated" 1>&2; exit 1
fi
