#!/usr/bin/env bash
# Clear local capture files on Pi before a new test run (does not touch scripts).
#
# Usage:
#   cd ~/EtherGold/4-Board\ Test\ Fixture/sync
#   bash clear_pi_capture.sh

if grep -q $'\r' "${BASH_SOURCE[0]}" 2>/dev/null; then
  sed -i 's/\r$//' "${BASH_SOURCE[0]}"
  exec bash "${BASH_SOURCE[0]}" "$@"
fi

set -euo pipefail

TIMING="$(cd "$(dirname "${BASH_SOURCE[0]}")/../fixture-timing" && pwd)"
DATA="${TIMING}/data"
LOGS="${TIMING}/logs"

mkdir -p "${DATA}" "${LOGS}"

for f in packets.raw events.jsonl fixture_stats.json; do
  path="${DATA}/${f}"
  if [[ -f "${path}" ]]; then
    rm -f "${path}"
    echo "removed ${path}"
  fi
done

for f in "${LOGS}"/*; do
  [[ -e "${f}" ]] || continue
  rm -f "${f}"
  echo "removed ${f}"
done

echo "Pi capture cleared under ${DATA} and ${LOGS}"
echo "Upload fresh run: cd $(dirname "${BASH_SOURCE[0]}") && ./sync_to_dropbox.sh"
