#! /bin/bash -f
# Last edited on 2014-02-20 01:34:14 by stolfilocal

tmp="/tmp/$$"
show="SHOW"

gawk \
  ' BEGIN \
      { nx = 50;
        nn = 10000;
        Pi = 3.1415926535897932384;
        for (ix = 0; ix < nx; ix++)
          { xp = (ix + 0.5)/nx;   # {x/Pi}
            x = xp*Pi; 
            f = 1.0; # Accum product.
            for (k = 1; k < nn; k += 2)
              { d = xp*(1 - xp)/(k*(k+1));
                t = 1.0 + d;
                f = f * t;
              }
            printf "%10.7f %10.7f\n", x, f;
          }
        fflush("/dev/stdout");
      } 
    ' > ${tmp}.dat
    
export GDFONTPATH=.:..

gnuplot <<EOF
set term png size 2400,800 font "courbd,24"
set output "${tmp}-full.png"
set title "Marcus of Augustus's avatar function"
set grid ytics lt 1 lw 3 lc rgb '#ffddaa', lt 1 lw 1.5 lc rgb '#ffddaa'
set grid mytics

set xtics 1.0
set grid xtics lt 1 lw 3 lc rgb '#ffddaa', lt 1 lw 1.5 lc rgb '#ffddaa'
set grid mxtics
set key top left

plot \
  "${tmp}.dat" using 1:2 title "moa(x)" \
    with linespoints pt 7 ps 1.5 lt 1 lw 1.0 lc rgb '#0044dd', \
  (1 + 0.18*sin(x)) title "1 + 0.18*sin(x)" with lines lt 1 lw 1.0 lc rgb '#dd4400'
    
quit
EOF

plotfile="${tmp}.png"
convert ${tmp}-full.png -resize '50%' ${plotfile}

if [[ "/${show}" == "/SHOW" ]]; then
  display ${plotfile}
fi

cat ${plotfile}

rm -fv ${tmp}{-*,}.*
    
        
