#!/bin/sh set -eu retry_max=5 base=${RTME_INSTALL_BASE_URL:-https://rtme.sh/send} os=$(uname -s | tr '[:upper:]' '[:lower:]') arch=$(uname -m) case "$arch" in x86_64|amd64) arch=x64 ;; aarch64|arm64) arch=arm64 ;; *) echo "error: unsupported arch: $arch" >&2; exit 1 ;; esac case "$os" in darwin|linux) ;; *) echo "error: unsupported OS: $os" >&2; exit 1 ;; esac bin=rtme.sh url="${base}/install.bin?os=${os}&arch=${arch}" retry() { attempt=$1 phase=$2 detail=$3 [ "$attempt" -lt "$retry_max" ] || return 1 echo "→ retrying $phase ($((attempt + 1))/$retry_max): $detail" >&2 sleep "$attempt" } probe_status() { curl -sSLI -o /dev/null -w '%{http_code}' "$1" 2>/dev/null || printf 000; } attempt=1 while :; do meta=$(curl -sSLI -w ' http_code: %{http_code} url_effective: %{url_effective} ' "$url" | tr -d ' ') || { code=$? retry "$attempt" "metadata lookup" "curl exit $code" || { echo "error: failed to check installer release metadata" >&2; exit 1; } attempt=$((attempt + 1)) continue } status=$(printf '%s ' "$meta" | sed -n 's/^http_code: //p' | tail -n 1) case "$status" in 2??|3??) break ;; 404) echo "error: no binary for ${os}/${arch}" >&2; exit 1 ;; 5??) retry "$attempt" "metadata lookup" "HTTP ${status}" || { echo "error: failed to check installer release metadata (HTTP ${status})" >&2; exit 1; }; attempt=$((attempt + 1)); continue ;; 4??) echo "error: failed to check installer release metadata (HTTP ${status})" >&2; exit 1 ;; *) retry "$attempt" "metadata lookup" "HTTP ${status:-000}" || { echo "error: failed to check installer release metadata" >&2; exit 1; }; attempt=$((attempt + 1)); continue ;; esac done version=$(printf '%s ' "$meta" | sed -n 's/^[Xx]-[Rr][Tt][Mm][Ee]-[Vv][Ee][Rr][Ss][Ii][Oo][Nn]: //p' | head -n 1) [ "$version" ] || { echo "error: failed to check installer release metadata" >&2; exit 1; } final_url=$(printf '%s ' "$meta" | sed -n 's/^url_effective: //p' | tail -n 1) total=$(printf '%s ' "$meta" | sed -n 's/^[Cc][Oo][Nn][Tt][Ee][Nn][Tt]-[Ll][Ee][Nn][Gg][Tt][Hh]: //p' | tail -n 1) echo "→ downloading ${bin} ${version} for ${os}/${arch}…" if [ "$final_url" ]; then show() { size=$(wc -c "$bin" 2>/dev/null | sed 's/ .*//'); size=${size:-0} if [ "${total:-0}" -gt 0 ] 2>/dev/null; then pct=$((size * 100 / total)); fill=$((pct / 5)); empty=$((20 - fill)) bar=$(printf '%*s' "$fill" '' | tr ' ' '#')$(printf '%*s' "$empty" '' | tr ' ' '-') printf ' [%s] %3d%% %3d/%3d MiB' "$bar" "$pct" $((size / 1048576)) $(((total + 1048575) / 1048576)) >&2 else printf ' %3d MiB' $((size / 1048576)) >&2 fi } else : fi download_url=${final_url:-$url} attempt=1 while :; do rm -f "$bin" if [ "$final_url" ]; then curl -fsSL "$final_url" -o "$bin" & pid=$! if [ -t 2 ]; then while kill -0 "$pid" 2>/dev/null; do show; sleep 0.2; done; fi if wait "$pid"; then if [ -t 2 ]; then show; printf ' ' >&2; fi break else code=$? if [ -t 2 ]; then printf ' ' >&2; fi fi else if curl -fL --progress-bar "$url" -o "$bin"; then break else code=$? fi fi rm -f "$bin" status=$(probe_status "$download_url") case "$status" in 404) echo "error: failed to download ${bin} ${version} (HTTP ${status})" >&2; exit 1 ;; 5??) detail="HTTP ${status}" ;; *) detail="curl exit ${code}" ;; esac retry "$attempt" "download" "$detail" || { case "$status" in 5??|4??) echo "error: failed to download ${bin} ${version} (HTTP ${status})" >&2 ;; *) echo "error: failed to download ${bin} ${version}" >&2 ;; esac exit 1 } attempt=$((attempt + 1)) done chmod +x "$bin" echo "→ installed ./${bin}"