File size: 2,357 Bytes
af6b038
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env bash
# fetch-models.sh — download every link in models.txt into $MODELS_DIR.
# Skips files that already exist (use --force to re-download).
#
# Usage: fetch-models.sh [--dry-run] [--force] [--list] [models.txt]
# Env: MODELS_TXT (default: models.txt), MODELS_DIR (default: /models)
set -euo pipefail

MODELS_DIR="${MODELS_DIR:-/models}"
LIST="${MODELS_TXT:-models.txt}"
FORCE=0
DRY=0
ONLY_LIST=0

for arg in "$@"; do
  case "$arg" in
    -h|--help)
      echo "Usage: $0 [--dry-run] [--force] [--list] [models.txt]"
      exit 0
      ;;
    --dry-run|-n) DRY=1 ;;
    --force|-f) FORCE=1 ;;
    --list|-l) ONLY_LIST=1 ;;
    --*)
      echo "error: unknown option '$arg'" >&2
      exit 1
      ;;
    *) LIST="$arg" ;;
  esac
done

if [ ! -f "$LIST" ]; then
  echo "error: link file not found: $LIST" >&2
  exit 1
fi

# --- parse models.txt into parallel arrays (no URLs hardcoded here) ---
f_url=()
f_file=()

basename_of_url() {
  local u="${1%%\?*}"
  echo "${u##*/}"
}

while IFS= read -r line || [ -n "$line" ]; do
  line="${line%$'\r'}"
  line="${line%% #*}"
  case "$line" in
    ''|'#'*) continue ;;
  esac
  url="${line%% *}"
  rest="${line#"$url"}"
  rest="${rest#"${rest%%[![:space:]]*}"}"
  if [ -n "$rest" ]; then
    file="${rest%%[[:space:]]*}"
  else
    file="$(basename_of_url "$url")"
  fi
  f_url+=("$url")
  f_file+=("$file")
done < "$LIST"

if [ "${#f_url[@]}" -eq 0 ]; then
  echo "error: no links found in $LIST" >&2
  exit 1
fi

fetch_one() {
  local url="$1" dest="$2"
  if [ -s "$dest" ]; then
    if [ "$FORCE" -eq 1 ]; then
      echo "re-downloading: $dest"
    else
      echo "skip (exists): $dest"
      return 0
    fi
  fi
  if [ "$DRY" -eq 1 ]; then
    echo "would download: $url -> $dest"
    return 0
  fi
  echo "downloading: $url"
  mkdir -p "$(dirname "$dest")"
  if command -v wget >/dev/null 2>&1; then
    wget -q --show-progress "$url" -O "$dest.tmp"
    mv "$dest.tmp" "$dest"
  else
    curl -fL --progress-bar "$url" -o "$dest.tmp"
    mv "$dest.tmp" "$dest"
  fi
}

mkdir -p "$MODELS_DIR"

i=0
while [ "$i" -lt "${#f_url[@]}" ]; do
  dest="$MODELS_DIR/${f_file[$i]}"
  if [ "$ONLY_LIST" -eq 1 ]; then
    echo "${f_url[$i]} -> $dest"
  else
    fetch_one "${f_url[$i]}" "$dest"
  fi
  i=$((i + 1))
done

if [ "$ONLY_LIST" -eq 0 ]; then
  echo "done: ${#f_url[@]} files in $MODELS_DIR"
fi