Files

266 lines
11 KiB
Bash
Raw Permalink Normal View History

2026-05-12 15:12:52 +09:00
#!/usr/bin/env bash
# shellcheck disable=SC2086
# Downloads and installs the latest Nightly version from
# https://github.com/ClementTsang/bottom/releases
2026-05-12 15:12:52 +09:00
#
# Supported platforms:
2026-05-12 15:12:52 +09:00
# macOS : Apple Silicon (arm64), Intel (x86_64)
# Linux : x86_64, aarch64(RPi4/5), armv7(RPi2/3 32bit), armv6(RPi Zero/1), i686
# FreeBSD : x86_64
# Windows : Git Bash / MSYS2 / Cygwin (.zip install)
# (Native PowerShell/CMD not supported → use WSL)
2026-05-12 15:12:52 +09:00
set -euo pipefail
# ────────────────────────────────────────────
# 상수 정의
# ────────────────────────────────────────────
REPO="ClementTsang/bottom"
API_URL="https://api.github.com/repos/${REPO}/releases"
FALLBACK_TAG="0.12.3"
2026-05-12 15:12:52 +09:00
INSTALL_DIR="${HOME}/.local/bin"
# ────────────────────────────────────────────
# 유틸리티 함수 (모두 stderr 출력)
# ────────────────────────────────────────────
info() { echo "[INFO] $*" >&2; }
warn() { echo "[WARN] $*" >&2; }
error() { echo "[ERROR] $*" >&2; exit 1; }
success() { echo "[OK] $*" >&2; }
# ────────────────────────────────────────────
# 필수 명령어 확인
# ────────────────────────────────────────────
for cmd in curl uname; do
command -v "$cmd" &>/dev/null || error "'$cmd' 명령어가 필요합니다."
done
# Fallback: use python3 or grep if jq is not available
2026-05-12 15:12:52 +09:00
if command -v jq &>/dev/null; then
_json_nightly_tag() {
jq -r '[.[] | select((.assets | length) > 0 and ((.prerelease==true and (.tag_name | startswith("nightly-"))) or .prerelease==false))] | .[0].tag_name'
2026-05-12 15:12:52 +09:00
}
elif command -v python3 &>/dev/null; then
2026-05-12 15:12:52 +09:00
_json_nightly_tag() {
python3 -c "
import sys, json
data = json.load(sys.stdin)
for r in data:
if r.get('assets') and ((r.get('prerelease') and r.get('tag_name','').startswith('nightly-')) or not r.get('prerelease')):
2026-05-12 15:12:52 +09:00
print(r['tag_name'])
break
"
}
else
_json_nightly_tag() {
grep -oE '"tag_name":[[:space:]]*"[^"]+"' | head -1 | sed -E 's/.*"([^"]+)"$/\1/'
}
2026-05-12 15:12:52 +09:00
fi
# ────────────────────────────────────────────
# OS / 아키텍처 감지
# ────────────────────────────────────────────
# 반환값 형식: "<플랫폼 트리플>:<압축포맷>"
# 압축포맷 = tar.gz | zip
detect_platform() {
local os arch
os="$(uname -s)"
arch="$(uname -m)"
case "$os" in
# ── macOS ──────────────────────────────────────────────
Darwin)
case "$arch" in
arm64) echo "aarch64-apple-darwin:tar.gz" ;;
x86_64) echo "x86_64-apple-darwin:tar.gz" ;;
*) error "지원하지 않는 macOS 아키텍처: $arch" ;;
esac
;;
# ── Linux (라즈베리파이 전 모델 포함) ──────────────────
Linux)
case "$arch" in
x86_64|amd64)
echo "x86_64-unknown-linux-musl:tar.gz" ;;
aarch64|arm64)
# RPi 4/5, 64bit OS
echo "aarch64-unknown-linux-musl:tar.gz" ;;
armv7l)
# RPi 2/3, 32bit OS (ARMv7)
echo "armv7-unknown-linux-musleabihf:tar.gz" ;;
armv6l)
# RPi Zero / 1 (ARMv6)
# No pre-built binaries available for ARMv6.
# (armv7 musl/gnu binaries don't work on ARMv6)
error "No pre-built binaries for ARMv6 (RPi Zero/1). Build from source: 'cargo install bottom'" ;;
2026-05-12 15:12:52 +09:00
i686|i386)
echo "i686-unknown-linux-musl:tar.gz" ;;
*)
error "지원하지 않는 Linux 아키텍처: $arch" ;;
esac
;;
# ── FreeBSD ────────────────────────────────────────────
FreeBSD)
case "$arch" in
amd64|x86_64) echo "x86_64-unknown-freebsd:tar.gz" ;;
*) error "FreeBSD는 x86_64만 지원합니다 (현재: $arch)" ;;
esac
;;
# ── Windows (Git Bash / MSYS2 / Cygwin) ───────────────
# Native PowerShell/CMD don't have bash, so this script won't run
# WSL is detected as Linux, not Windows
2026-05-12 15:12:52 +09:00
MINGW*|MSYS*|CYGWIN*)
warn "Windows environment (Git Bash/MSYS2/Cygwin) detected."
warn "WSL (Windows Subsystem for Linux) is recommended."
2026-05-12 15:12:52 +09:00
# Windows에는 tar.exe(Windows 10 1803+)가 있어 .zip 처리 가능
case "$arch" in
x86_64) echo "x86_64-pc-windows-msvc:zip" ;;
aarch64) echo "aarch64-pc-windows-msvc:zip" ;;
i686) echo "i686-pc-windows-msvc:zip" ;;
*) error "지원하지 않는 Windows 아키텍처: $arch" ;;
esac
;;
*)
error "지원하지 않는 OS: $os (uname -a: $(uname -a))"
;;
esac
}
# ────────────────────────────────────────────
# Get latest Nightly release tag
2026-05-12 15:12:52 +09:00
# ────────────────────────────────────────────
get_latest_nightly_tag() {
info "Fetching latest release from GitHub API..."
2026-05-12 15:12:52 +09:00
local releases="" tag=""
releases="$(curl -fsSL -H "User-Agent: install-bottom-sh" --connect-timeout 10 "${API_URL}" 2>/dev/null)" || true
2026-05-12 15:12:52 +09:00
if [[ -n "$releases" ]]; then
tag="$(echo "$releases" | _json_nightly_tag 2>/dev/null || true)"
fi
2026-05-12 15:12:52 +09:00
if [[ -z "$tag" || "$tag" == "null" ]]; then
warn "Could not fetch release tag from GitHub API. Using fallback tag: ${FALLBACK_TAG}"
tag="${FALLBACK_TAG}"
fi
2026-05-12 15:12:52 +09:00
echo "$tag"
}
# ────────────────────────────────────────────
# Build download URL
2026-05-12 15:12:52 +09:00
# ────────────────────────────────────────────
build_download_url() {
local tag="$1"
local triple="$2" # Platform triple (e.g. x86_64-apple-darwin)
2026-05-12 15:12:52 +09:00
local ext="$3" # tar.gz | zip
local filename="bottom_${triple}.${ext}"
echo "https://github.com/${REPO}/releases/download/${tag}/${filename}"
}
# ────────────────────────────────────────────
# Download, extract & install
2026-05-12 15:12:52 +09:00
# ────────────────────────────────────────────
download_and_install() {
local url="$1"
local ext="$2" # tar.gz | zip
local is_windows="${3:-false}"
local fallback_url="${4:-}"
2026-05-12 15:12:52 +09:00
local tmpdir
tmpdir="$(mktemp -d)"
trap '[[ -d "${tmpdir:-}" ]] && rm -rf "$tmpdir"' EXIT
local filename
filename="${url##*/}"
info "Downloading: $url"
if ! curl -fsSL --progress-bar -o "${tmpdir}/${filename}" "$url"; then
if [[ -n "$fallback_url" && "$fallback_url" != "$url" ]]; then
warn "Primary download failed. Retrying with fallback URL: $fallback_url"
curl -fsSL --progress-bar -o "${tmpdir}/${filename}" "$fallback_url" \
|| error "Download failed: $fallback_url"
else
error "Download failed: $url"
fi
fi
2026-05-12 15:12:52 +09:00
info "Extracting..."
2026-05-12 15:12:52 +09:00
case "$ext" in
tar.gz)
command -v tar &>/dev/null || error "'tar' command is required."
2026-05-12 15:12:52 +09:00
tar -xzf "${tmpdir}/${filename}" -C "$tmpdir"
;;
zip)
if command -v unzip &>/dev/null; then
unzip -q "${tmpdir}/${filename}" -d "$tmpdir"
elif command -v tar &>/dev/null; then
# Windows 10+ tar.exe supports zip
2026-05-12 15:12:52 +09:00
tar -xf "${tmpdir}/${filename}" -C "$tmpdir"
else
error "'unzip' or 'tar' (Windows 10+) is required."
2026-05-12 15:12:52 +09:00
fi
;;
*)
error "Unknown archive format: $ext"
2026-05-12 15:12:52 +09:00
;;
esac
# Windows uses btm.exe, others use btm
2026-05-12 15:12:52 +09:00
local bin_name="btm"
[[ "$is_windows" == "true" ]] && bin_name="btm.exe"
local bin_src
bin_src="$(find "$tmpdir" -maxdepth 3 -type f -name "$bin_name" | head -1)"
[[ -z "$bin_src" ]] && error "Cannot find '$bin_name' in the archive."
2026-05-12 15:12:52 +09:00
# Create install directory
2026-05-12 15:12:52 +09:00
mkdir -p "$INSTALL_DIR"
local install_path="${INSTALL_DIR}/${bin_name}"
info "Installing: ${install_path}"
2026-05-12 15:12:52 +09:00
cp "$bin_src" "$install_path"
chmod +x "$install_path"
success "Installation complete!"
info "Version check:"
2026-05-12 15:12:52 +09:00
"$install_path" --version || true
# PATH instructions
2026-05-12 15:12:52 +09:00
if ! echo ":${PATH}:" | grep -q ":${INSTALL_DIR}:"; then
warn "'${INSTALL_DIR}' is not in your PATH."
warn "Add this line to your ~/.bashrc or ~/.zshrc:"
2026-05-12 15:12:52 +09:00
warn " export PATH=\"\$HOME/.local/bin:\$PATH\""
fi
}
# ────────────────────────────────────────────
# Main
2026-05-12 15:12:52 +09:00
# ────────────────────────────────────────────
main() {
local platform_info triple ext is_windows tag url
platform_info="$(detect_platform)"
triple="${platform_info%%:*}" # Before ':'
ext="${platform_info##*:}" # After ':'
2026-05-12 15:12:52 +09:00
info "Detected platform: $triple (format: $ext)"
2026-05-12 15:12:52 +09:00
# Check if Windows
2026-05-12 15:12:52 +09:00
is_windows="false"
[[ "$triple" == *"windows"* ]] && is_windows="true"
tag="$(get_latest_nightly_tag)"
info "Release tag: $tag"
2026-05-12 15:12:52 +09:00
url="$(build_download_url "$tag" "$triple" "$ext")"
local fallback_url
fallback_url="$(build_download_url "$FALLBACK_TAG" "$triple" "$ext")"
download_and_install "$url" "$ext" "$is_windows" "$fallback_url"
2026-05-12 15:12:52 +09:00
}
main "$@"