#!/usr/bin/env bash # # download-prefill.sh # # Downloads the latest (or specific) release of SteamPrefill # (https://github.com/tpill90/steam-lancache-prefill) into # bin/steam-prefill/SteamPrefill # # Usage: # ./scripts/download-prefill.sh # # Environment: # PREFILL_VERSION - Pin a specific version tag (e.g. v3.4.2) # PREFILL_FORCE - Set to any non-empty value to re-download even if present # set -euo pipefail DEST_DIR="bin/steam-prefill" TARGET="$DEST_DIR/SteamPrefill" mkdir -p "$DEST_DIR" if [[ -x "$TARGET" && -z "${PREFILL_FORCE:-}" ]]; then echo "SteamPrefill already present at $TARGET" echo "Run with PREFILL_FORCE=1 to re-download." exit 0 fi VERSION="${PREFILL_VERSION:-}" OS=$(uname -s | tr '[:upper:]' '[:lower:]') ARCH=$(uname -m) case "$ARCH" in x86_64|amd64) ARCH_NAME="x64" ;; aarch64|arm64) ARCH_NAME="arm64" ;; *) echo "Unsupported architecture: $ARCH"; exit 1 ;; esac case "$OS" in linux) OS_NAME="linux" ;; darwin) OS_NAME="osx" ;; *) echo "Unsupported OS: $OS"; exit 1 ;; esac echo "Resolving SteamPrefill version..." if [[ -z "$VERSION" ]]; then # Follow the /latest redirect to discover the current tag LATEST_URL=$(curl -sIL -o /dev/null -w '%{url_effective}' \ "https://github.com/tpill90/steam-lancache-prefill/releases/latest" 2>/dev/null || true) if [[ "$LATEST_URL" =~ /tag/([^/?#]+) ]]; then VERSION="${BASH_REMATCH[1]}" else echo "Failed to resolve latest version from GitHub redirect." exit 1 fi fi echo "Downloading SteamPrefill $VERSION for ${OS_NAME}-${ARCH_NAME}..." rm -f "$TARGET" "$TARGET.tmp" 2>/dev/null || true DOWNLOADED=0 # Preferred: query the GitHub API for the exact asset list (most reliable) API_URL="https://api.github.com/repos/tpill90/steam-lancache-prefill/releases/tags/${VERSION}" ASSET_URL="" if command -v jq >/dev/null 2>&1; then echo " Querying GitHub API for assets..." ASSET_NAME=$(curl -fsSL "$API_URL" 2>/dev/null | jq -r --arg os "$OS_NAME" --arg arch "$ARCH_NAME" ' .assets[] | select(.name | ascii_downcase | contains($os)) | select(.name | ascii_downcase | contains($arch)) | .name ' | head -1) if [[ -n "$ASSET_NAME" ]]; then ASSET_URL="https://github.com/tpill90/steam-lancache-prefill/releases/download/${VERSION}/${ASSET_NAME}" echo " Found asset via API: $ASSET_NAME" fi fi # Fallback: try common name patterns if API or jq not available if [[ -z "$ASSET_URL" ]]; then echo " Trying common asset name patterns..." CANDIDATES=( "SteamPrefill-${VERSION}-${OS_NAME}-${ARCH_NAME}.zip" "SteamPrefill-${VERSION}-${OS_NAME}-${ARCH_NAME}" "SteamPrefill-${OS_NAME}-${ARCH_NAME}.zip" "SteamPrefill-${OS_NAME}-${ARCH_NAME}" "SteamPrefill-linux-${ARCH_NAME}.zip" "SteamPrefill-linux-${ARCH_NAME}" ) for name in "${CANDIDATES[@]}"; do URL="https://github.com/tpill90/steam-lancache-prefill/releases/download/${VERSION}/${name}" echo " Trying $name ..." if curl -fI -s --retry 2 "$URL" >/dev/null 2>&1; then ASSET_URL="$URL" ASSET_NAME="$name" break fi done fi if [[ -n "$ASSET_URL" ]]; then echo "Downloading $ASSET_NAME ..." if curl -fL --retry 3 --retry-delay 2 -A "Mozilla/5.0 (compatible; SteamPrefill-Downloader)" \ --progress-bar -o "$TARGET.tmp" "$ASSET_URL"; then echo "Download complete." if [[ "$ASSET_NAME" == *.zip ]]; then echo "Extracting..." if ! command -v unzip >/dev/null 2>&1; then echo "Error: unzip is required for this release." rm -f "$TARGET.tmp" exit 1 fi unzip -o -q "$TARGET.tmp" -d "$DEST_DIR" FOUND=$(find "$DEST_DIR" -type f -name "SteamPrefill" | head -1) if [[ -n "$FOUND" ]]; then mv "$FOUND" "$TARGET" fi rm -f "$TARGET.tmp" find "$DEST_DIR" -mindepth 1 -maxdepth 1 -type d -name "SteamPrefill*" -exec rm -rf {} + 2>/dev/null || true else mv "$TARGET.tmp" "$TARGET" fi chmod +x "$TARGET" DOWNLOADED=1 fi fi if [[ $DOWNLOADED -eq 0 ]]; then echo "" echo "Failed to download a matching asset for $VERSION." echo "You can try pinning a different version:" echo " PREFILL_VERSION=vX.Y.Z ./scripts/download-prefill.sh" echo "" echo "Or download manually from:" echo " https://github.com/tpill90/steam-lancache-prefill/releases/tag/${VERSION}" exit 1 fi echo "" echo "Installed SteamPrefill $VERSION → $TARGET" echo "" echo "You can now run it directly, for example:" echo " ./bin/steam-prefill/SteamPrefill --help" echo " ./bin/steam-prefill/SteamPrefill benchmark run ..."