#!/usr/bin/env bash
# Background wrapper: retry CICIoT2023 PCAP download until it reports
# a clean "Done." with n_files > 0. Each attempt is delimited in the log
# so the monitor can grep for progress.
#
# Invoked detached (nohup ... &). The inner script is resumable via
# the .part-file convention in cic_download.py.

set -uo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
LOG="${REPO_ROOT}/logs/ciciot2023_pcap.log"

# nohup strips the interactive PATH; re-expose the project venv so
# `python` resolves inside download_ciciot2023.sh.
if [[ -x "${REPO_ROOT}/.venv/bin/python" ]]; then
  export PATH="${REPO_ROOT}/.venv/bin:${PATH:-/usr/local/bin:/usr/bin:/bin}"
fi

# Route through the local proxy; detached bash does not inherit the
# interactive shell's proxy env, and cicresearch.ca's WAF rate-limits
# bare-IP traffic much more aggressively than the proxy exit.
export HTTP_PROXY="http://127.0.0.1:7093"
export HTTPS_PROXY="http://127.0.0.1:7093"
export ALL_PROXY="socks5h://127.0.0.1:7093"
export NO_PROXY="localhost,127.0.0.1,::1"
export http_proxy="${HTTP_PROXY}"
export https_proxy="${HTTPS_PROXY}"
export all_proxy="${ALL_PROXY}"
export no_proxy="${NO_PROXY}"

i=0
while :; do
  i=$((i + 1))
  ts=$(date +%F\ %T)
  printf '\n=== attempt %d %s ===\n' "$i" "$ts" >>"$LOG"
  # Skip bundle zips (e.g. PCAP.zip) — we want per-attack-class .pcap files,
  # not the whole dataset as one archive.
  WHAT=pcap SKIP_EXT="zip,7z" bash "${SCRIPT_DIR}/download_ciciot2023.sh" >>"$LOG" 2>&1
  rc=$?
  # If inner script exited with 0 AND last "Done." line reports >0 files,
  # we consider the listing+walk to have succeeded at least once. Otherwise
  # keep retrying on network/SSL failures.
  last_done=$(grep -E '^Done\. [0-9]+ files processed' "$LOG" | tail -1 || true)
  n=$(printf '%s' "$last_done" | awk '{print $2}')
  if [[ "$rc" -eq 0 && -n "$n" && "$n" -gt 0 ]]; then
    printf '=== loop finished clean %s (files=%s) ===\n' "$(date +%F\ %T)" "$n" >>"$LOG"
    break
  fi
  printf '=== attempt %d ended rc=%s last_done=%q; sleep 60 ===\n' \
    "$i" "$rc" "$last_done" >>"$LOG"
  sleep 60
done
