#!/bin/sh
# Gather bulk detailed by-minute Eddi stats for the stated month or year.
# This will all be more sane if the eddi doesn't do DST, ie stays on UTC.
# Picks up credentials from ~/.netrc so may not be suitable for batch.

# Usage:
#     $0 YYYY MM
# or:
#     $0 YYYY

# Returns a line per day in date order,
# each line a JSON array of maps in time order of the form:
#     {"yr":2022,"mon":12,"dom":31,"dow":"Sat","min":20,"imp":6360,"pect1":6300,"hsk":288,"v1":2443,"frq":4997}

YYYY=
MM=
if [ "$#" -lt 1 ]; then
    echo "ERROR: please supply YYYY MM or YYYY" 1>&2
    exit 2
elif [ "$#" -lt 2 ]; then
    YYYY=$1
else
    YYYY=$1
    MM=$2
fi

. script/myenergi/.config

month=1
if [ "" != "$MM" ]; then month="$MM"; fi
while [ "$month" -lt 13 ];
    do
    day=1
    while [ "$day" -lt 32 ];
        do
        # Extract records that have our serial number and flatten to an array.
        sh script/myenergi/eddiStatsByMin-netrc.sh $YYYY-$month-$day | \
            jq -c '.U'$eddisn | \
            egrep '^[[]..*[]]$'
        day="$(expr $day + 1)"
        done
    if [ "" != "$MM" ]; then break; else month="$(expr $month + 1)"; fi
    done

exit 0


##########
# May be used / adapted / etc without any promise of fitness for purpose
# under the terms of the Apache License Version 2.0, January 2004
#     http://www.apache.org/licenses/LICENSE-2.0
##########
