v4 / scripts /fetch-models.sh
we2app's picture
update
af6b038 verified
Raw
History Blame Contribute Delete
2.36 kB
#!/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