#!/bin/bash
# Summarizes one or more run.sh CSV files: drain in mAh and percent, average power, and projected hours.
#
#   ./summarize.sh results/*.csv
set -euo pipefail
[ $# -gt 0 ] || { echo "usage: ./summarize.sh <file.csv>..." >&2; exit 1; }

printf "%-28s %8s %8s %9s %9s %9s %9s %10s\n" "run" "minutes" "mAh" "percent" "avg W" "CPU app" "CPU WS" "hours*"
for f in "$@"; do
  label=$(sed -n 's/^# label=\([^ ]*\).*/\1/p' "$f")
  max=$(sed -n 's/.*raw_max_mah=\([0-9]*\).*/\1/p' "$f")
  awk -F, -v label="$label" -v max="$max" '
    /^#/ || /^elapsed/ { next }
    { n++; if (n == 1) { t0 = $1; p0 = $2; m0 = $3 } t1 = $1; p1 = $2; m1 = $3; w += $6; ca += $7; cw += $8 }
    END {
      if (n < 2) { printf "%-28s not enough samples\n", label; exit }
      mins = (t1 - t0) / 60; used = m0 - m1
      # Hours from full, at this run'\''s average drain rate, using the battery'\''s current full charge.
      hours = used > 0 ? (max / used) * (mins / 60) : 0
      printf "%-28s %8.1f %8d %9d %9.2f %9.1f %9.1f %10.1f\n", label, mins, used, p0 - p1, w / n, ca / n, cw / n, hours
    }' "$f"
done
echo "* Projected hours on a full charge at the run's average rate. Treat it as a comparison between runs, not a battery life claim."
