#!/usr/bin/env bash
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PORT="${PORT:-/dev/ttyACM0}"
ESPTOOL="${ESPTOOL:-$HOME/.arduino15/packages/esp32/tools/esptool_py/5.1.0/esptool}"
BAUD="${BAUD:-921600}"
OUT_BIN="${ROOT_DIR}/factory-full-16mb.bin"

echo "== Port: ${PORT}"
echo "== Output: ${OUT_BIN}"
echo "== Esptool: ${ESPTOOL}"
echo "== Baud: ${BAUD}"

"${ESPTOOL}" --port "${PORT}" --baud "${BAUD}" read-flash 0 0x1000000 "${OUT_BIN}"
sha256sum "${OUT_BIN}" | tee "${OUT_BIN}.sha256"

echo
echo "== Backup done."
