#!/usr/bin/env bash
# Download CICIoT2023 (PCAP + CSV) from UNB CIC via cic_download.py.
#
# Prereq: submit the form at
#   https://www.unb.ca/cic/datasets/iotdataset-2023.html
# in a browser, then save the issued Token cookie in Netscape format as
#   scripts/download/cookies_ciciot2023.txt
# The cookie domain must be .cicresearch.ca and the name must be "Token".
#
# Usage:
#   bash download_ciciot2023.sh            # both PCAP and CSV
#   WHAT=pcap bash download_ciciot2023.sh  # PCAP only
#   WHAT=csv  bash download_ciciot2023.sh  # CSV only
#   DRY_RUN=1 bash download_ciciot2023.sh  # enumerate without downloading

set -euo pipefail

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

DEST_ROOT="${DEST:-${REPO_ROOT}/datasets/ciciot2023/raw}"
COOKIES="${COOKIES:-${SCRIPT_DIR}/cookies_ciciot2023.txt}"
BASE="https://cicresearch.ca/IOTDataset/CIC_IOT_Dataset2023/"
WHAT="${WHAT:-both}"

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 "${DEST_ROOT}/pcap" ;;
  csv)  run CSV  "${DEST_ROOT}/csv"  ;;
  both) run PCAP "${DEST_ROOT}/pcap"
        run CSV  "${DEST_ROOT}/csv"  ;;
  *) echo "Unknown WHAT=${WHAT} (expected pcap|csv|both)" >&2; exit 1 ;;
esac
