#! /bin/bash
# Last edited on 2021-01-05 21:31:11 by jstolfi

sids=( "$@" ) # Series IDS alternating with titles.

tmp="/tmp/$$"

pref="00-DATA/split"
show=1

# Generate the plot command by looping on sids:
gfile="${tmp}.plt"
rgbs=( "ff0000" "ff7700" "338800" "5500ff" "aa0066" "aa2200" "557700" "117700" "007722" "005588" "2222ff" "880088" "007755" "0033ff" )
isid=0
irgb=0
printf "plot " > ${gfile}
sep=""
while [[ ${isid} -lt ${#sids[@]} ]]; do
  sid="${sids[${isid}]}"
  isid=$(( ${isid} + 1 ))
  tit="${sids[${isid}]}"
  rgb="${rgbs[${irgb}]}"
  dfile="${pref}/${sid}.txt"
  printf "%s%s\n" "${sep}" "\\" >> ${gfile}
  printf "  \"%s\" using 1:5 title \"%s\"" "${dfile}" "${tit} ${sid}" >> ${gfile}
  printf " with linespoints  lt 1 ps 0.75 pt 7 lc rgb '%s'" "#${rgb}" >> ${gfile}
  sep=","
  isid=$(( ${isid} + 1 ))
  irgb=$(( ${irgb} + 1 ))
done
printf "\n" >> ${gfile}

tfile="${tmp}_series.png"
pfile="${pref}_series.png"

export GDFONTPATH="."

gnuplot << EOF
  set term png truecolor size 1800,800 font "arial,18"
  set output "${tfile}"
  set title "Monthly Relative Price Series"
  set key bottom right
  set mxtics 10
  set grid xtics mxtics ytics lt 0
  
  load "${gfile}"
EOF

if [[ -s ${tfile} ]]; then
  convert ${tfile} -resize '50%' ${pfile}
  if [[ ${show} -ne 0 ]]; then display ${pfile} ; fi
  rm ${tfile}
fi
