#!/usr/bin/env bash
# Re-downloadable manifest fetcher. Resumes partial files; skips complete ones.
set -uo pipefail
cd "$(dirname "$0")/.."
LOG=tools/fetch.log; : > "$LOG"
while IFS=$'\t' read -r dir url; do
  case "${dir:-}" in ""|\#*) continue;; esac
  [ -z "${url:-}" ] && continue
  f="$dir/${url##*/}"
  mkdir -p "$dir"
  echo "== $f" | tee -a "$LOG"
  curl -fL --retry 5 --retry-delay 5 --retry-all-errors -C - --connect-timeout 30 \
       -o "$f" "$url" >>"$LOG" 2>&1 \
    && echo "   OK   $(stat -c%s "$f") bytes" | tee -a "$LOG" \
    || echo "   FAIL $url" | tee -a "$LOG"
done < tools/manifest.tsv
echo "ALL DONE" | tee -a "$LOG"
