#!/bin/sh
# Reproduce the measured-energy benchmark without a repository checkout.
# Requires curl, Python 3 and Node.js 20 or newer. Installs nothing.
# Run from any writable directory, including an empty one:
#   sh reproduce-research-benchmark.sh
# All downloaded and generated files stay in a new, uniquely named directory.
set -eu
umask 077

for bqc_command in curl python3 node mktemp; do
  if ! command -v "$bqc_command" >/dev/null 2>&1; then
    printf '%s\n' "Missing required command: $bqc_command" >&2
    exit 1
  fi
done
python3 -c 'import sys; assert sys.version_info.major == 3, "Python 3 is required"'
node -e 'if (Number(process.versions.node.split(".")[0]) < 20) { console.error("Node.js 20 or newer is required"); process.exit(1); }'

bqc_workspace=$(mktemp -d "$PWD/battery-quote-check.XXXXXXXX")
bqc_public="$bqc_workspace/public/tools/battery-quote-check"
bqc_research="$bqc_workspace/research/battery-quote-check/real-data"
bqc_archive="$bqc_workspace/Ausgrid_solar_home_data.zip"
mkdir -p "$bqc_public" "$bqc_research"

printf '%s\n' \
  "Workspace: $bqc_workspace" \
  'Downloads: five source files from https://artwaste.land and the approximately 54 MiB archive below.' \
  'Archive: https://pierreh.eu/downloads/Ausgrid_solar_home_data.zip' \
  'Creator: Ausgrid. Preservation custodian: Pierre Haessig. Licence: CC BY 3.0 Australia.' \
  'Licence: https://creativecommons.org/licenses/by/3.0/au/' \
  'Metadata: https://data.gov.au/data/en/dataset/nsw-solar-home-electricty-data' \
  'The archive is a third-party preservation copy; the former Ausgrid download returned 404.' \
  'This uses measured gross consumption/generation, an artificial clock and invented tariffs.' \
  'It does not establish actual net-meter exchange, historical DST, household savings or a real quote.' \
  'No packages are installed. Files remain in the workspace if any step fails.'

bqc_download() {
  curl --fail --show-error --location --proto '=https' --proto-redir '=https' \
    --connect-timeout 30 --max-time 600 "$1" --output "$2"
}

# Preserve these paths: the benchmark imports the three engine modules relatively.
for bqc_module in engine.mjs certificate.mjs rational.mjs; do
  bqc_download "https://artwaste.land/tools/battery-quote-check/$bqc_module" "$bqc_public/$bqc_module"
done
for bqc_source in extract.py benchmark.mjs; do
  bqc_download "https://artwaste.land/checks/research/battery-quote-check/real-data/$bqc_source" "$bqc_research/$bqc_source"
done
bqc_download 'https://pierreh.eu/downloads/Ausgrid_solar_home_data.zip' "$bqc_archive"

# extract.py rejects every archive except SHA-256:
# 5a766f52b6c8b3b72730380f4422e478934bc94640a4b089dd0e0e3c055c5d82
# It validates channels, dates, quality and source values, then writes the subset
# and manifest next to itself. No data is fetched through the source-only mirror.
python3 "$bqc_research/extract.py" "$bqc_archive"
node "$bqc_research/benchmark.mjs" > "$bqc_research/benchmark-result.json"
cat "$bqc_research/benchmark-result.json"
printf '%s\n' \
  "Results: $bqc_research/benchmark-result.json" \
  "Provenance: $bqc_research/manifest.json" \
  'Expected exact ceiling: 616477/2000 AUD = 308.2385 AUD.' \
  'Expected empty-start policy: 608377/2000 AUD = 304.1885 AUD.' \
  'Difference: 81/20 AUD = 4.05 AUD, the free initial-charge allowance.' \
  'These values belong to the stated derived model, not an observed household bill.'
