#!/bin/sh
# Extracts grid-tie PV generation data to graph.
# This uses the RPi log files.

# This outputs to stdout in-order lines of the form:
#     20140713T23:00Z 3011
# from yesterday (if possible) and then today.

# Logging directory.
LOGDIR=/var/log/SunnyBeam
# Last data: not guaranteed valid.
# Format:
#    20140722T14:09Z 2810
#LASTDATA="${LOGDIR}/LASTDATA.flag"
#LASTSAMPLEDATA="$(cat $LASTDATA)"

# Compute UTC YYYYMMDD date to match data logging.
UTCDATE="$(date -u +%Y%m%d)"
# Yesterday in same format as UTCDATE (if easy to compute), else blank.
#YESTERDAY="$(echo $UTCDATE | awk '$3%100 != 01 { printf("%8.8d", $1-1); }')"
YESTERDAY="$(date -u --date yesterday +%Y%m%d)"
# What should be today's log file.
LOGFILE="${LOGDIR}/${UTCDATE}.log"
# Yesterday's log file, if it exists, else blank.
YLOGFILE=""
POSSIBLEYLF="${LOGDIR}/${YESTERDAY}.log"
if [ -e "${POSSIBLEYLF}" ]; then YLOGFILE=${POSSIBLEYLF}; fi

exec cat "${YLOGFILE}" "${LOGFILE}"
