#!/usr/bin/env bash # Provides the get_retrofit_dates helper for identifying missing market-data archives on a remote host. get_retrofit_dates() { local host=${1:-} local root_dir=${2:-} local filename_glob=${3:-} local day_count=${4:-} if [[ -z "$host" || -z "$root_dir" || -z "$filename_glob" || -z "$day_count" ]]; then echo "usage: get_retrofit_dates " >&2 return 1 fi if ! [[ "$day_count" =~ ^[0-9]+$ ]] || (( day_count <= 0 )); then echo "get_retrofit_dates: must be a positive integer" >&2 return 1 fi local -a target_dates=() declare -A month_dirs=() if ! date -d "1 day ago" +%Y%m%d >/dev/null 2>&1; then echo "get_retrofit_dates: requires GNU date arithmetic" >&2 return 1 fi local offset date for (( offset = 1; offset <= day_count; offset++ )); do date=$(date -d "$offset day ago" +%Y%m%d) target_dates+=("$date") month_dirs["${date:0:4}/${date:4:2}"]=1 done declare -A existing_dates=() local dir remote_path remote_cmd remote_output entry for dir in "${!month_dirs[@]}"; do remote_path="$root_dir/$dir" printf -v remote_cmd "cd %q 2>/dev/null && LC_ALL=C ls -1" "$remote_path" remote_output=$(ssh "$host" "$remote_cmd" 2>/dev/null || true) if [[ -n "$remote_output" ]]; then while IFS= read -r entry; do [[ -z "$entry" ]] && continue [[ $entry == $filename_glob ]] || continue if [[ "$entry" =~ ^([0-9]{8}) ]]; then existing_dates["${BASH_REMATCH[1]}"]=1 fi done <<<"$remote_output" fi done local -a missing_dates=() local idx for (( idx = ${#target_dates[@]} - 1; idx >= 0; idx-- )); do local dt=${target_dates[$idx]} [[ -n "${existing_dates[$dt]:-}" ]] || missing_dates+=("$dt") done printf '%s\n' "${missing_dates[@]}" }