#!/usr/bin/env bash set -euo pipefail ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" ARCHIVE_DIR="${ROOT_DIR}/data" DATA_ROOT="${1:-${ROOT_DIR}/data}" mkdir -p "${DATA_ROOT}" declare -A selected_archives=() while IFS= read -r archive; do filename="$(basename "${archive}")" base="${filename}" if [[ "${filename}" == *.tar.zst ]]; then base="${filename%.tar.zst}" elif [[ "${filename}" == *.tar ]]; then base="${filename%.tar}" else continue fi if [[ -n "${selected_archives[${base}]:-}" ]]; then echo "found multiple archive variants for ${base} under ${ARCHIVE_DIR}; keep only one of .tar or .tar.zst" >&2 exit 1 fi selected_archives["${base}"]="${archive}" done < <(find "${ARCHIVE_DIR}" -maxdepth 1 -type f \( -name '*.tar' -o -name '*.tar.zst' \) | sort) if [[ "${#selected_archives[@]}" -eq 0 ]]; then echo "no archives found under ${ARCHIVE_DIR}" >&2 exit 1 fi mapfile -t archive_bases < <(printf '%s\n' "${!selected_archives[@]}" | sort) for base in "${archive_bases[@]}"; do archive="${selected_archives[${base}]}" echo "extracting ${archive}" case "${archive}" in *.tar.zst) tar --zstd -xf "${archive}" -C "${DATA_ROOT}" ;; *.tar) tar -xf "${archive}" -C "${DATA_ROOT}" ;; *) echo "unsupported archive format: ${archive}" >&2 exit 1 ;; esac done echo "raw data extracted into ${DATA_ROOT}"