#!/bin/bash

if [[ $# -ne 1 ]]; then
    echo "usage: $0 <DATA.csv>"
else
    data="$1";
    png="`echo $1 | sed -s "s/\.csv$//"`" # strip ending
    (cat <<EOD
set datafile separator ","
f(x)=a*x+b
fit f(x) "$data" every ::1 using 2:5 via a,b
set terminal png
set xlabel 'regularity'
set ylabel 'steps'
set output "${png}_regularity-vs-steps.png"
plot "$data" every ::1 using 2:5 title "data", f(x) title "lin. fit" lc rgb "black"
g(x)=aa*x+bb
fit g(x) "$data" every ::1 using 4:5 via aa,bb
set xlabel 'improvement potential'
set ylabel 'steps'
set output "${png}_improvement-vs-steps.png"
plot "$data" every ::1 using 4:5 title "data", g(x) title "lin. fit" lc rgb "black"
h(x)=aaa*x+bbb
fit h(x) "$data" every ::1 using 4:6 via aaa,bbb
set xlabel 'improvement potential'
set ylabel 'evolution error'
set output "${png}_improvement-vs-evo-error.png"
plot "$data" every ::1 using 4:6 title "data", h(x) title "lin. fit" lc rgb "black"
EOD
) > "${png}.gnuplot.script" 
    gnuplot "${png}.gnuplot.script" 2> "${png}.gnuplot.log"
    mv fit.log "${png}.gnuplot.fit.log"
fi
