#! /bin/bash
# Last edited on 2014-09-07 00:33:58 by stolfilocal

pref="2014-09-06-blockchain"
crop=0

function draw_vlines(){
  # Draws vertical lines at month starts and at days 11 and 21.
  # Input: "${pref}-${suff}-crop.png", output "${pref}-${suff}-annotated.png"
  suff="$1"; shift;
  hmons=( $@ ); # H-coords at start of each month (assumed day 1, Oct/2013 to Sep/2014).
  
  vtop=126; # Top V-coord of lines.
  vbot=480  # Bottom V-coord of lines.
  
  args=( ); # Plot arguments.
  n=${#hmons[@]}
  n1=$(( $n - 1 ));
  
  # Determine the horizontal increment for 10 and 20 days:
  scale=`echo "s = (${hmons[$n1]} - ${hmons[0]})/335; scale=3; (s+0.0005)/1" | bc -lq`
  hd10=`echo "s=${scale}; h = 10*s; scale=0; (h+0.5)/1" | bc -lq`
  hd20=`echo "s=${scale}; h = 20*s; scale=0; (h+0.5)/1" | bc -lq`
  echo "${suff}: scale = ${scale}  hd10 = ${hd10}  hd20 = ${hd20}" 1>&2 
  i=-1;
  while [[ ${i} -lt ${n} ]]; do
    if [[ i -lt 0 ]]; then
      # Draw bars for Sep/2013:
      h01=$(( ${hmons[0]} - ${hd10} - ${hd20} ))
    else
      h01=${hmons[$i]}
      args+=( -color '#ddaa22' )
      args+=( -S ${h01} ${vtop} ${h01} ${vbot} )
    fi
    h11=$(( ${h01} + ${hd10} ))
    h21=$(( ${h01} + ${hd20} ))
    args+=( -color '#eeddcc' )
    args+=( -S ${h11} ${vtop} ${h11} ${vbot} )
    if [[ ${h21} -le 940 ]]; then
      args+=( -S ${h21} ${vtop} ${h21} ${vbot} )
    fi
    i=$(( $i + 1 ))
  done
  annotate-pic.sh \
    ${pref}-${suff}-crop.png 14 Black Yellow -strokewidth 1 \
    ${args[@]} \
    > ${pref}-${suff}-annotated.png
  gimp ${pref}-${suff}-annotated.png
}

if [[ ${crop} -ne 0 ]]; then
  for suff in vol-tx est-vol-BTC est-vol-USD ; do
    img="${pref}-${suff}"
      convert ${img}.png -crop '1000x571+1+109' +repage ${img}-crop.png
      gimp ${img}-crop.png
  done
else
  draw_vlines vol-tx      136 208 277 349 421 486 557 627 699 768 840 911   
  draw_vlines est-vol-USD 161 231 298 368 437 500 569 636 706 773 843 912 
  draw_vlines est-vol-BTC 142 213 282 353 424 490 560 629 700 769 840 912 
fi
