#! /bin/bash -u
# Last edited on 2026-10-30 19:53:41 by stolfi

eph="$1"; shift

file_c="/tmp/.cost-c-${eph}.txt"
file_py="/tmp/.cost-py-${eph}.txt"

temp="/tmp/$$"
temp_plot_file="${temp}-big.png"

hsize=1600
vsize=1200

xeph=$( echo "${eph}/10000" | bc -lq )
xeph=$( printf "%.4f" "${xeph}" )

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

  set title "C vs python gap costs - eph = ${xeph}"

  set xtics 5
  set mxtics 5
  set logscale y
  set yrange [1:1000]
  # set xrange [0:20]

  v(k) = ((1+column(k))/1000.0)
  plot \
    "${file_c}"  using 1:(v(2)) notitle with lines lw 3 lc rgb "#77aa33", \
    ""           using 1:(v(3)) notitle with lines lw 3 lc rgb "#77aa33", \
    ""           using 1:(v(4)) notitle with lines lw 3 lc rgb "#77aa33", \
    ""           using 1:(v(5)) notitle with lines lw 3 lc rgb "#77aa33", \
    ""           using 1:(v(6)) notitle with lines lw 3 lc rgb "#77aa33", \
    "${file_py}" using 1:(v(2)) notitle with lines lw 3 lc rgb "#aa33ff", \
    ""           using 1:(v(3)) notitle with lines lw 3 lc rgb "#aa33ff", \
    ""           using 1:(v(4)) notitle with lines lw 3 lc rgb "#aa33ff", \
    ""           using 1:(v(5)) notitle with lines lw 3 lc rgb "#aa33ff", \
    ""           using 1:(v(6)) notitle with lines lw 3 lc rgb "#aa33ff"
  quit
EOF

echo "converting plot ..." 1>&2
plot_file="${temp}-sma.png"
rm -f ${plot_file}
if [[ -s ${temp_plot_file} ]]; then
  convert ${temp_plot_file} -resize '50%' ${plot_file}
  display -title '%f' ${plot_file}
  cat ${plot_file}
else
  echo "** ${temp_plot_file} not created" 1>&2; exit 1
  echo "FAILED"
fi

rm -f ${temp}-*

