#! /bin/bash
# Last edited on 2014-04-12 21:37:21 by stolfilocal

date="2014-04-11"
bstp=1
hubi=1
icons=1
candlewidth=5   # Pixels per chart bar.
candleunit=6    # Hours per chart bar.

tmp=/tmp/$$

function tscale(){
  # Prints to {stdout} the H-coordinate corresponing to a given hour number.

  # Scaling parameters:
  tshift="$1"; shift # Reference hour.
  hshift="$1"; shift # H-coordinate of reference hour.

  targ="$1"; shift # Given hour.

  echo "enter tscale ${tshift} ${hshift} ${targ}" 1>&2
  harg=`echo "h=${hshift} + (${targ} - ${tshift})*${candlewidth}/${candleunit}; scale=0; h/1.0" | bc -lq`

  # harg=$(( ( ( ${targ} - ${tshift} ) * ${candlewidth} ) + ${hshift} ))

  printf "%d\n" "${harg}"
  echo "exit tscale ${harg}" 1>&2
}

function pscale(){
  # Prints to {stdout} the V-coordinate corresponing to a given price {parg/currf}

  # Scaling parameters:
  pmin="$1"; shift # Low reference price.
  vbot="$1"; shift # V-coordinate of the low reference price.
  pmax="$1"; shift # High reference price.
  vtop="$1"; shift # V-coordinate of the high reference price.

  parg="$1"; shift # Given price.
  currf="$1"; shift # Currency conversion factor.

  # echo "enter pscale ${pmin} ${vbot} ${pmax} ${vtop} ${parg} ${currf}" 1>&2

  varg=`echo "p=${parg}/${currf}; a=${pmin}; b=${pmax}; r=${vbot}; s=${vtop}; v = r + (s-r)*(p-a)/(b-a); scale=0; v/1.0" | bc -lq`

  printf "%d\n" "${varg}"
  # echo "exit pscale ${varg}" 1>&2
}

function make_rect_cmds(){
  # Prints to {stdout} the draw commands for a prediction box with label.

  # Hour scaling parameters:
  tshift="$1"; shift # Reference hour.
  hshift="$1"; shift # H-coordinate of reference hour.

  # Price scaling parameters:
  pmin="$1"; shift # Low reference price.
  vbot="$1"; shift # V-coordinate of the low reference price.
  pmax="$1"; shift # High reference price.
  vtop="$1"; shift # V-coordinate of the high reference price.

  krect="$1"; shift # "@" for old rectangle, "+" for new, "=" for predicted.
  trect="$1"; shift # Hour of rectangle center.
  prect="$1"; shift # Predicted price at ${trect}.
  herr="$1"; shift  # Half-width of rectangle (pixels)
  verr="$1"; shift  # Half-height of rectngle (pixels).

  # echo "enter make_rect_cmds ${pmin} ${vbot} ${pmax} ${vtop} ${krect} ${hrect} ${prect} ${herr} ${verr}" 1>&2

  hrect=`tscale ${tshift} ${hshift} ${trect}`
  vrect=`pscale ${pmin} ${vbot} ${pmax} ${vtop} ${prect} 1.00`
  vrect_top=$(( ${vrect} + ${verr} ))
  vrect_bot=$(( ${vrect} - ${verr} ))
  hrect_lft=$(( ${hrect} - ${herr} ))
  hrect_rht=$(( ${hrect} + ${herr} ))

  if [[ "/${krect}" == "/@" ]]; then
    crect="#5566cc"
    lwdt=2
  elif [[ "/${krect}" == "/+" ]]; then
    crect="#0066ff"
    lwdt=3
  elif [[ "/${krect}" == "/=" ]]; then
    crect="#ff00aa"
    lwdt=3
  else
    echo "** invalid rectange kind" 1>&2 ; exit 1
  fi

  printf "%s %s\n" "-color" "${crect}"
  printf "%s %s\n" "-strokewidth" "${lwdt}"
  printf "%s %s %s  %s %s  %s %s  %s %s\n" "-Q" \
    "${hrect_rht}" "${vrect_top}" \
    "${hrect_rht}" "${vrect_bot}" \
    "${hrect_lft}" "${vrect_bot}" \
    "${hrect_lft}" "${vrect_top}"
  
  # echo "exit make_rect_cmds" 1>&2
}

function annot(){
  # Annotate one image.

  ex="$1"; shift       # Exchange code

  # Input image:
  iimg="${date}-history-03m-${ex}"

  # Output image:
  oimg="${date}-history-03m-${ex}-annotated"

  # Horizontal scale information
  if [[ "/$1" == "/-hscale" ]]; then
    shift;
    tshift="$1"; shift # Reference hour.
    hshift="$1"; shift # H-coordinate of reference hour.
  else
    echo "** missing \"-hscale\"" 1>&2
    exit 1
  fi

  # Vertical scale information
  if [[ "/$1" == "/-vscale" ]]; then
    shift;
    pmin="$1"; shift # Low reference price.
    vbot="$1"; shift # V-coordinate of the low reference price.
    pmax="$1"; shift # High reference price.
    vtop="$1"; shift # V-coordinate of the high reference price.
  else
    echo "** missing \"-vscale\"" 1>&2
    exit 1
  fi

  # Critical V-ordinates
  if [[ "/$1" == "/-vvolb" ]]; then
    shift;
    vvolb="$1"; shift # V-coordinate of the bottom of the Volume plot.
  else
    echo "** missing \"-vvolb\"" 1>&2
    exit 1
  fi
  if [[ "/$1" == "/-vicon" ]]; then
    shift;
    vicon="$1"; shift # V-coordinate of the bottom of the Volume plot.
  else
    echo "** missing \"-vicon\"" 1>&2
    exit 1
  fi

  # Decoration
  deco_cmds=( )
  while [[ "/$1" == "/-deco" ]]; do
    shift;
    hpdeco="$1"; shift # H-coordinate of deco (absolute, pixels).
    vpdeco="$1"; shift # V-coordinate of deco (absolute, pixels).
    hadeco="$1"; shift # H-alignment of deco.
    vadeco="$1"; shift # V-alignment of deco.
    imdeco="$1"; shift # Image name of deco.
    mgdeco="$1"; shift # Magnification factor of deco.

    deco_cmds+=( -I ${hpdeco} ${vpdeco} ${hadeco} ${vadeco} ${imdeco} ${mgdeco} )
  done
  echo "deco_cmds=( " "${deco_cmds[@]}" " )" 1>&2

  # Balloon with labels ("-label")
  labt_cmds=( )
  while [[ "/$1" == "/-label" ]]; do
    shift;

    # Label with time of last prediction:
    klabt="$1"; shift # Kind of balloon.
    mlabt="$1"; shift # Text (eg UTC time) to show in balloon.
    tlabt="$1"; shift # Hour of event.
    vlabt_dot="$1"; shift # V-coordinate of dot (pixels).
    vlabt_lab="$1"; shift # V-coordinate of label (pixels).

    if [[ "/${klabt}" == "/@" ]]; then clabt="#af8de1" ; else clabt="#e18050" ; fi

    hlabt=`tscale ${tshift} ${hshift} ${tlabt}`

    labt_cmds+=( -color "${clabt}" -strokewidth 2 )
    labt_cmds+=( -S ${hlabt} "$(( ${vvolb} + 2 ))" )
    labt_cmds+=( -L ${hlabt} ${vlabt_dot}  ${hlabt} ${vlabt_lab}  "${mlabt}" )
  done
  echo "labt_cmds=( " "${labt_cmds[@]}" " )" 1>&2

  # Prediction boxes:
  rect_cmds=( ) # Commands to draw prediction boxes.
  while [[ "/$1" == "/-rect" ]]; do
    shift;
    krect="$1"; shift # "@" for old, "+" for new, "=" for predicted.
    trect="$1"; shift # Hour of rectangle center.
    prect="$1"; shift # Price of rectangle center.
    herr="$1"; shift  # Half-width of box (pixels).
    verr="$1"; shift  # Half-height of box (pixels).

    hrect=$(( ${hrect} + ${hshift} ))

    rect_cmds+=( `make_rect_cmds ${tshift} ${hshift} ${pmin} ${vbot} ${pmax} ${vtop} "${krect}" ${trect} ${prect} ${herr} ${verr}` )
  done
  echo "rect_cmds=( " "${rect_cmds[@]}" " )" 1>&2

  # Currency factor for trend lines:
  if [[ "/$1" == "/-linecurrf" ]]; then
    shift;
    linecurrf="$1"; shift # Currency factor to be applied to prices of trend lines.
  else
    linecurrf=1.00
  fi

  # Trend lines:
  line_cmds=( )
  while [[ "/$1" == "/-line" ]]; do
    shift;
    kline="$1"; shift # "@" for bad, "+" for new.
    tline1="$1"; shift # Hour of line start.
    pline1="$1"; shift # Price of line start.
    tline2="$1"; shift # Hour of line end.
    pline2="$1"; shift # Price of line end.

    if [[ "/${kline}" == "/@" ]]; then cline="#aa552299" ; else cline="#ff660099" ; fi

    hline1=`tscale ${tshift} ${hshift} ${tline1}`
    vline1=`pscale ${pmin} ${vbot} ${pmax} ${vtop} ${pline1} ${linecurrf}`

    hline2=`tscale ${tshift} ${hshift} ${tline2}`
    vline2=`pscale ${pmin} ${vbot} ${pmax} ${vtop} ${pline2} ${linecurrf}`

    line_cmds+=( -strokewidth 3 -color "${cline}" )
    line_cmds+=( -S ${hline1} ${vline1} ${hline2} ${vline2} )
  done
  echo "line_cmds=( " "${line_cmds[@]}" " )" 1>&2

  annotate-pic.sh \
      ${iimg}.png 16 Black Yellow \
      "${deco_cmds[@]}" \
      \
      "${labt_cmds[@]}" \
      \
      "${line_cmds[@]}" \
      \
      "${rect_cmds[@]}" \
      \
    > ${oimg}.png
  gimp ${oimg}.png
}

hscale_HUBI=( 2379 2310 )
hscale_BSTP=( 2379 2308 )

vscale_HUBI=( 2500  480  6000   87 )
vscale_BSTP=(  400  458  1000   83 )

#   -deco 4088  88 0.5 0.5 ../icons/helix-wp-edit-200.png 0.60 \
#   -deco 4088 368 0.5 0.5 ../icons/ophthalmosaurus-wp-edit-200.png 0.60 \
#   -deco  740 280 0.5 0.5 ../icons/germanodactylus-wp-edit-200.png 0.60 \
#   -deco  740 485 0.5 0.5 ../icons/raphus-wp-edit-200.png 0.60 \
       
if [[ ${hubi} -ne 0 ]]; then
  annot HUBI \
    -hscale ${hscale_HUBI[@]} \
    -vscale ${vscale_HUBI[@]} \
    -vvolb 642 \
    -vicon 540 \
     \
    -label "@" "Jan/31"  735  195  80 \
    -label "@" "Feb/05"  861  195  80 \
    -label "@" "Feb/10"  969  238  80 \
    -label "@" "Feb/20" 1209  317  80 \
    -label "+" "Feb/24" 1299  313  80 \
    -label "+" "Mar/15" 1479  290  80 \
    -label "@" "Mar/26" 2037  341  80 \
    -label "@" "Apr/02" 2196  407  80 \
    -label "@" "Apr/10" 2380  438  80 \
    -label "@" "Apr/11" 2403  440 120
fi
       
if [[ ${bstp} -ne 0 ]]; then
  annot BSTP \
    -hscale ${hscale_BSTP[@]} \
    -vscale ${vscale_BSTP[@]} \
    -vvolb 609 \
    -vicon 513 \
     \
    -label "@" "Jan/31"  735  184  80 \
    -label "@" "Feb/05"  861  184  80 \
    -label "@" "Feb/10"  969  246  80 \
    -label "@" "Feb/20" 1209  298  80 \
    -label "+" "Feb/24" 1299  306  80 \
    -label "+" "Mar/15" 1479  246  80 \
    -label "@" "Mar/26" 2037  325  80 \
    -label "@" "Apr/02" 2196  380  80 \
    -label "@" "Apr/10" 2380  415  80 \
    -label "@" "Apr/11" 2403  423 120
fi
