#!/usr/bin/env bash
# Download ISCXTor2016 (PCAP + CSV) from UNB CIC via cic_download.py.
#
# Prereq: submit the form at
#   https://www.unb.ca/cic/datasets/tor.html
# in a browser, then save the issued Token cookie (Netscape format) as
#   scripts/download/cookies_iscxtor2016.txt
# Tokens are scoped per-dataset — the CICIoT2023 cookie will NOT work here.
#
# Usage:
#   bash download_iscxtor2016.sh
#   WHAT=pcap|csv|both DEST=... COOKIES=... DRY_RUN=1 LIMIT=N
#   PCAP_ROOT=... CSV_ROOT=... SKIP_EXT=zip,7z
#
# Note: the remote sub-path names ("Pcaps" / "CSVs" or similar) are only
# visible after authenticating. Run with DRY_RUN=1 first to confirm the
# tree; if the roots differ, set PCAP_ROOT=... and/or CSV_ROOT=....

set -euo pipefail

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

DEST_ROOT="${DEST:-${REPO_ROOT}/datasets/iscxtor2016/raw}"
COOKIES="${COOKIES:-${SCRIPT_DIR}/cookies_iscxtor2016.txt}"
BASE="https://cicresearch.ca/CICDataset/ISCX-Tor-NonTor-2017/"
WHAT="${WHAT:-both}"

# Default root names (override via env if the server uses different casing)
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}")

resolve_python() {
  if [[ -n "${PYTHON:-}" ]]; then
    printf '%s\n' "${PYTHON}"
    return
  fi
  if [[ -x "${REPO_ROOT}/.venv/bin/python" ]]; then
    printf '%s\n' "${REPO_ROOT}/.venv/bin/python"
    return
  fi
  if command -v python >/dev/null 2>&1; then
    command -v python
    return
  fi
  if command -v python3 >/dev/null 2>&1; then
    command -v python3
    return
  fi
  echo "ERROR: no Python interpreter found. Set PYTHON=/path/to/python." >&2
  exit 127
}

PYTHON_BIN="$(resolve_python)"

run() {
  local root="$1" dest="$2"
  echo "=== ${root} -> ${dest} ==="
  "${PYTHON_BIN}" -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
