#! /bin/bash
# Last edited on 2020-01-17 14:13:48 by jstolfi

xmin="$1"; shift
xmax="$1"; shift
ymin="$1"; shift
ymax="$1"; shift

name="$1"; shift 

ext="eps"
# ext="png"

tmp="/tmp/$$"
tfile="${tmp}_t.${ext}"

export GDFONTPATH="."
gnuplot <<EOF
  # set term png truecolor size 1200,1200 font "arial,24"
  set term postscript eps color background rgb '#ffffff' rounded size 6,6 font "Courier,24"
  set size ratio -1
  set output "${tfile}"
  set xrange [${xmin}:${xmax}]
  set yrange [${ymin}:${ymax}]
  set tics scale -0.8,-0.8
  set xtics 1; set mxtics 5
  set format x "%+g"
  set ytics 1; set mytics 5
  set format y "%+g"
  set xlabel "{/Symbol-Oblique \153}"
  set ylabel "{/Symbol-Oblique \154}" rotate by 90
  set dashtype 11 (10,10)
  set dashtype 12 (1,1.5)

  sel(i,v,j) = (column(i) == v ? column(j) : 0/0)
  heps = -0.002
  veps = +0.007
  set nokey
  set style circle radius screen 0.015
  plot \
    "out/${name}.txt" using (sel(3,11,1)):(sel(3,11,2)) with filledcurve fc rgb '#ffeeaa', \
    "out/${name}.txt" using (sel(3,12,1)):(sel(3,12,2)) with filledcurve fc rgb '#cceeaa', \
    "out/${name}.txt" using (sel(3,13,1)):(sel(3,13,2)) with filledcurve fc rgb '#aaccff', \
    "out/${name}.txt" using (sel(3,14,1)):(sel(3,14,2)) with filledcurve fc rgb '#eeaaff', \
    "out/${name}.txt" using (sel(3,15,1)):(sel(3,15,2)) with filledcurve fc rgb '#ffaaaa', \
    \
    "out/${name}.txt" using (sel(3,8,1)):(sel(3,8,2)) with lines lw 2.0 lc rgb '#cccccc', \
    "out/${name}.txt" using (sel(3,9,1)):(sel(3,9,2)) with lines lw 2.0 lc rgb '#cccccc', \
    \
    "out/${name}.txt" using (sel(3,0,1)):(sel(3,0,2)) with lines lw 4 lc rgb '#aa2200', \
    "out/${name}.txt" using (sel(3,1,1)):(sel(3,1,2)) with lines dashtype 12 lw 3 lc rgb '#aa2200', \
    "out/${name}.txt" using (sel(3,6,1)):(sel(3,6,2)) with lines dashtype 11 lw 2 lc rgb '#000066', \
    \
    "out/${name}.txt" using (sel(3,16,1)):(sel(3,16,2)) with linespoints lw 1.0 pt 7 ps 0.75 lc rgb '#000000', \
    \
    "out/${name}.txt" using (sel(3,20,1)):(sel(3,20,2)) with filledcurve fc rgb '#ffffff', \
    "out/${name}.txt" using (sel(3,3,1)):(sel(3,3,2)) with lines lw 1.5 lc rgb '#6666cc', \
    "out/${name}.txt" using (sel(3,4,1)):(sel(3,4,2)) with lines lw 1.0 lc rgb '#aaaaff', \
    "out/${name}.txt" using (sel(3,21,1)):(sel(3,21,2)):4 with labels font "Courier,6" tc rgb '#000088', \
    "out/${name}.txt" using (sel(3,22,1)):(sel(3,22,2)) with lines lw 1.5 lc rgb '#ff4400', \
    "out/${name}.txt" using (sel(3,7,1)):(sel(3,7,2))   with lines lw 2.0 lc rgb '#336600', \
    "out/${name}.txt" using (sel(3,5,1)):(sel(3,5,2))   with lines lw 2.5 lc rgb '#000066', \
    \
    "out/${name}.txt" using (sel(3,2,1)):(sel(3,2,2)) with circles fill solid fc rgb '#444444', \
    "out/${name}.txt" using (sel(3,2,1)):(sel(3,2,2)) with circles lc rgb '#000000', \
    "out/${name}.txt" using (sel(3,2,1)+heps):(sel(3,2,2)+veps):4 with labels font "Courier,24" tc rgb '#ffffff'
    
  quit
EOF

if [[ -s ${tfile} ]]; then
  if [[ "/${ext}" == "/eps" ]]; then
    convert ${tfile} -density 600 -resize '50%' out/${name}.png 
    mv ${tfile} out/${name}.eps
    atril out/${name}.eps
  elif [[ "/${ext}" == "/png" ]]; then
    convert ${tfile} -resize '50%' out/${name}.png 
    display -title '%f' out/${name}.png
  else
    echo "**bug" 1>&2 ; exit 1
  fi
  
fi
