#!/usr/bin/env bash
# Download CICDDoS2019 (CSV, optionally PCAP) from UNB CIC via cic_download.py.
#
# Prereq: submit the form at
#   https://www.unb.ca/cic/datasets/ddos-2019.html
# in a browser, then save the issued Token cookie (Netscape format) as
#   scripts/download/cookies_cicddos2019.txt
# Tokens are scoped per-dataset — the CICIoT2023 / ISCXTor cookies will NOT
# work here.
#
# PCAPs for this dataset are already downloaded (see datasets/cicddos2019/raw/
# pcap/). Default WHAT=csv reflects that. Switch to WHAT=pcap or WHAT=both if
# you need to re-fetch.
#
# Usage:
#   bash download_cicddos2019.sh                    # CSVs only (default)
#   WHAT=pcap bash download_cicddos2019.sh          # PCAPs only
#   WHAT=both bash download_cicddos2019.sh          # everything
#   DRY_RUN=1 bash download_cicddos2019.sh          # enumerate without downloading
#   CSV_ROOT=CSV bash download_cicddos2019.sh       # override root if server uses a different name
#
# First-time tip: run with DRY_RUN=1 to discover the exact remote root names.
# The CIC site is inconsistent across datasets (CSV / CSVs / CSV-01-12 ...).

set -euo pipefail

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

DEST_ROOT="${DEST:-${REPO_ROOT}/datasets/cicddos2019/raw}"
COOKIES="${COOKIES:-${SCRIPT_DIR}/cookies_cicddos2019.txt}"
BASE="https://cicresearch.ca/CICDataset/CICDDoS2019/"
WHAT="${WHAT:-csv}"

# Default root names. Override via env if dry-run shows a different layout.
PCAP_ROOT="${PCAP_ROOT:-PCAPs}"
CSV_ROOT="${CSV_ROOT:-CSVs}"

EXTRA=()
[[ "${DRY_RUN:-}" == "1" ]] && EXTRA+=(--dry-run)
[[ -n "${LIMIT:-}" ]] && EXTRA+=(--limit "${LIMIT}")
[[ -n "${SKIP_EXT:-}" ]] && EXTRA+=(--skip-ext "${SKIP_EXT}")

run() {
  local root="$1" dest="$2"
  echo "=== ${root} -> ${dest} ==="
  python -u "${SCRIPT_DIR}/cic_download.py" \
    --cookies "${COOKIES}" --base "${BASE}" \
    --root "${root}" --dest "${dest}" "${EXTRA[@]}"
}

case "${WHAT}" in
  pcap) run "${PCAP_ROOT}" "${DEST_ROOT}/pcap" ;;
  csv)  run "${CSV_ROOT}"  "${DEST_ROOT}/csv"  ;;
  both) run "${PCAP_ROOT}" "${DEST_ROOT}/pcap"
        run "${CSV_ROOT}"  "${DEST_ROOT}/csv"  ;;
  *) echo "Unknown WHAT=${WHAT} (expected pcap|csv|both)" >&2; exit 1 ;;
esac
