#!/bin/sh -Cefu set -Cefu cd /home/ketas/files/nps || exit 1 export TZ=Europe/Tallinn _tomorrow() { date -j -v +1d -f '%F %T' "`date +%F` 00:00:00" +%F } _start_of_day() { local date="$1" date -j -Iseconds -f '%F %T' "$date 00:00:00" } _end_of_day() { local date="$1" date -j -Iseconds -f '%F %T' "$date 23:59:59" } _start_of_year() { local year="$1" date -j -Iseconds -f '%F %T' "$year-01-01 00:00:00" } _end_of_year() { local year="$1" date -j -Iseconds -f '%F %T' "$year-12-31 23:59:59" } arg="${1:-}" if [ ! "$arg" ] then arg="`_tomorrow`" fi case "$arg" in *-*-*) date -j -f %F "$arg" > /dev/null start_date="`_start_of_day \"$arg\"`" end_date="`_end_of_day \"$arg\"`" ;; *) date -j -f %Y "$arg" > /dev/null start_date="`_start_of_year \"$arg\"`" end_date="`_end_of_year \"$arg\"`" ;; esac start_date="`urlencode \"$start_date\"`" end_date="`urlencode \"$end_date\"`" url="https://dashboard.elering.ee/api/nps/price?start=$start_date&end=$end_date" output_file=- case "$*" in *save*) output_file="data/$arg.json" ;; esac fetch -q -o "$output_file" "$url"