#! /bin/bash
# Last edited on 2014-04-07 00:35:59 by stolfilocal

# Arguments:
#
#   {SHOW} {BESTSALE} {TITLE} {MDPRICE} {LOPRICE} {HIPRICE} {PREFIX}
#
# where
#
#   {SHOW} "SHOW" to display, "NOSHOW" not to.
#   {BESTSALE} assumes a "best subsequent sale price" instead of {MDPRICE}, {LOPRICE}, {HIPRICE}.
#   {TITLE} the plot title.
#   {MDPRICE}, {LOPRICE}, {HIPRICE} are the three reference prices.
#   {PREFIX} is the in/out file name prefix.
#
# Reads {PREFIX}_{IREF}.txt for {IREF} = 0,1,2, which is supposed to contain lines in the format
#   "{DATE[i]} {TIME[i]} {DELTA[i]} {PR[i]} {APLC[i]} {ARPC[i]}"
# Plots {ARPC[i]} as a function of {DELTA[i]}.
# Writes {PREFIX}.png

show=$1; shift
bestsale="$1"; shift
title="$1"; shift
mdprice="$1"; shift
loprice="$1"; shift
hiprice="$1"; shift
prefix="$1"; shift

if [[ ${bestsale} -ne 0 ]]; then 
  refindices=( 0 )
else
  refindices=( 0 1 2 )
fi

for iref in ${refindices[@]} ; do
  infile="${prefix}_${iref}.txt"
  if [[ ! ( -e ${infile}) ]]; then
    echo "** no file \"${infile}\"" 1>&2 ; exit 1
  fi
done

tmp="/tmp/$$"

pngfile="${prefix}.png"

export GDFONTPATH=.

gnuplot <<EOF
set term png size 2400,1200 font "courbd,18"
set output "${tmp}.png"
set title "${title}"

bestsale=${bestsale}

set xdata time
ttics = 90   # Number of days per tic interval
tmtics = 10  # Number of minor tic intervals per major tic interval
tticstep = 60*60*24*ttics
set xtics tticstep
set format x "%Y-%m-%d"
set mxtics tmtics
set grid xtics lt 1 lw 3 lc rgb '#ffddaa', lt 1 lw 1.5 lc rgb '#ffddaa'

set yrange [0.0000001:10000000.0]
set ytics ( 0.000001, 0.0000001, 0.00001, 0.0001, 0.001, 0.01, 0.1, 1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0, 10000000.0 ) 
set logscale y
# set ytics ( \
#   0.00001, 0.000012, 0.000015, 0.00002, 0.00003, 0.00004, 0.00005, 0.00006, 0.00007, 0.00008, 0.00009, \
#   0.0001, 0.00012, 0.00015, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008, 0.0009, \
#   0.001, 0.0012, 0.0015, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, \
#   0.01, 0.012, 0.015, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, \
#   0.1, 0.12, 0.15, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, \
#   1, 1.2, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, \
#   10, 12, 15, 20, 30, 40, 50, 60, 70, 80, 90, \
#   100, 120, 150, 200, 300, 400, 500, 600, 700, 800, 900 \
#   1000, 1200, 1500, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000 \
#   10000, 12000, 15000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000 \
# )
# Input time format:
set timefmt "%Y-%m-%d %H:%M:%S"

set grid ytics lt 1 lw 3 lc rgb '#ffddaa', lt 1 lw 1.5 lc rgb '#ffddaa'

if (bestsale) {
  plot \
    (1.00)                              notitle with lines lt 1 lw 1.5 lc rgb '#333333', \
    "${prefix}_0.txt" using (timecolumn(1)):6 notitle with lines lt 1 lw 2.0 lc rgb '#882200'
} else {
  plot \
    (1.00)                              notitle with lines lt 1 lw 1.5 lc rgb '#333333', \
    "${prefix}_0.txt" using (timecolumn(1)):6 title "sell at ${mdprice}" with lines lt 1 lw 2.0 lc rgb '#882200', \
    "${prefix}_1.txt" using (timecolumn(1)):6 title "sell at ${loprice}" with lines lt 1 lw 2.0 lc rgb '#77aa88', \
    "${prefix}_2.txt" using (timecolumn(1)):6 title "sell at ${hiprice}" with lines lt 1 lw 2.0 lc rgb '#7788aa'
}
quit
EOF

convert ${tmp}.png -resize '50%' ${pngfile}

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

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