#! /bin/bash 
# Last edited on 2015-02-21 18:16:16 by stolfilocal

# Reads a list of wallet operations, generates histograms
# of the transaction sizes in a given date range

wallet="$1"; shift
iniDate="$1"; shift    # First date to consider.
finDate="$1"; shift    # Last date to consider.
priceFile="$1"; shift; # File with BTC prices in USD

# The wallet operations are read from "txt/${wallet}/monthly/20??-??-?.txt".
# The histogram data is written to "out/${wallet}/${iniDate}--${finDate}-u${usdBins}-hist.txt"
# The hitogram plot is written to "out/${wallet}/${iniDate}--${finDate}-u${usdBins}-{nop,btc,usd}-{bin,cum}-hist.{png,jpg}"

tdir="txt/${wallet}/monthly"
pdir="out/${wallet}"

mkdir -p ${pdir}

tmp=/tmp/$$

cfile="${tmp}-all.dat"                           # Detailed transaction file.

cat `ls ${tdir}/20??-??-?.txt | sort` > ${cfile}

hfile="${pdir}/${iniDate}--${finDate}-u${usdBins}-hist.txt"  # File with histogram of operations.

for usdBins in 0 1 ; do 
  echo "=== generating histogram with {usdBins=${usdBins}}" 1>&2
  extract_wallet_histogram.gawk \
      -v wallet="${wallet}" \
      -v iniDate="${iniDate}" \
      -v finDate="${finDate}" \
      -v priceFile="${priceFile}" \
      -v usdBins=${usdBins} \
      ${cfile} \
    > ${hfile}

  for cum in 0 1 ; do
    for var in 0 1 2 ; do 
      plot_wallet_histogram.sh \
         "${hfile}" ${usdBins} ${cum} ${var} \
         "${iniDate}" "${finDate}" \
         "${wallet}" "${pdir}"
    done
  done
done
