-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
179 lines (156 loc) · 5.55 KB
/
Copy pathinstall.sh
File metadata and controls
179 lines (156 loc) · 5.55 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/bin/sh
set -eu
# =============================================================================
# aria2s installer — downloads the latest release, verifies checksum, installs
#
# Usage:
# curl -fsSL https://raw.githubusercontent.com/amio/aria2s/main/install.sh | sh
#
# Options (via environment variables):
# VERSION Install a specific version (e.g. "v0.0.3")
# BINDIR Install directory (default: /usr/local/bin)
# =============================================================================
# --- configuration ---
OWNER="amio"
REPO="aria2s"
BINDIR="${BINDIR:-/usr/local/bin}"
# --- color helpers (with terminal detection) ---
if [ -t 1 ]; then
BOLD="$(tput bold 2>/dev/null || printf '')"
BLUE="$(tput setaf 4 2>/dev/null || printf '')"
GREEN="$(tput setaf 2 2>/dev/null || printf '')"
YELLOW="$(tput setaf 3 2>/dev/null || printf '')"
RED="$(tput setaf 1 2>/dev/null || printf '')"
RESET="$(tput sgr0 2>/dev/null || printf '')"
else
BOLD=""; BLUE=""; GREEN=""; YELLOW=""; RED=""; RESET=""
fi
info() { printf '%s==>%s %s%s%s\n' "${BLUE}" "${RESET}" "${BOLD}" "$*" "${RESET}"; }
ok() { printf '%s==>%s %s%s%s\n' "${GREEN}" "${RESET}" "${BOLD}" "$*" "${RESET}"; }
warn() { printf '%sWarning:%s %s\n' "${YELLOW}" "${RESET}" "$*" >&2; }
err() { printf '%sError:%s %s\n' "${RED}" "${RESET}" "$*" >&2; exit 1; }
# --- prerequisite checks ---
need_cmd() {
if ! command -v "$1" >/dev/null 2>&1; then
err "need '$1' (command not found)"
fi
}
need_cmd uname
need_cmd mktemp
need_cmd grep
need_cmd sed
need_cmd awk
need_cmd tar
# --- downloader (curl first, wget fallback) ---
downloader() {
if command -v curl >/dev/null 2>&1; then
curl --fail --silent --show-error --location "$@"
elif command -v wget >/dev/null 2>&1; then
need_cmd wget
# emulate curl usage: downloader "$url" -o "$output"
local outfile=""
local url=""
while [ $# -gt 0 ]; do
case "$1" in
-o) outfile="$2"; shift 2;;
*) url="$1"; shift;;
esac
done
if [ -n "$outfile" ]; then
wget --quiet -O "$outfile" "$url"
else
wget --quiet -O - "$url"
fi
else
err "need curl or wget (neither found)"
fi
}
# --- detect os / arch ---
case "$(uname -s)" in
Darwin) OS="darwin";;
Linux) OS="linux";;
*) err "unsupported OS: $(uname -s)";;
esac
_arch_raw="$(uname -m)"
case "$_arch_raw" in
x86_64|amd64) ARCH="amd64";;
aarch64|arm64) ARCH="arm64";;
*) err "unsupported architecture: $_arch_raw";;
esac
info "detected: ${OS}/${ARCH}"
# --- determine version ---
if [ -n "${VERSION:-}" ]; then
TAG="$VERSION"
info "using specified version: ${TAG}"
else
info "fetching latest release..."
# Resolve /releases/latest via redirect (no GitHub API, no rate limit).
if command -v curl >/dev/null 2>&1; then
RELEASE_URL="$(curl -sS -o /dev/null -L -w '%{url_effective}' \
"https://github.com/${OWNER}/${REPO}/releases/latest")" \
|| err "failed to fetch release info from GitHub"
else
err "curl is required to auto-detect the latest version.\n"\
" Install curl, or set VERSION manually: curl … | VERSION=v0.0.7 sh"
fi
case "$RELEASE_URL" in
*/tag/*) TAG="${RELEASE_URL##*/tag/}" ;;
*) err "no releases found — try installing from source: go install github.com/${OWNER}/${REPO}@latest" ;;
esac
fi
TARBALL="${REPO}_${TAG#v}_${OS}_${ARCH}.tar.gz"
BASE_URL="https://github.com/${OWNER}/${REPO}/releases/download/${TAG}"
URL="${BASE_URL}/${TARBALL}"
CHECKSUMS_URL="${BASE_URL}/checksums.txt"
info "version: ${TAG} → ${TARBALL}"
# --- download ---
TMPDIR="$(mktemp -d)"
trap 'rm -rf "$TMPDIR"' EXIT
info "downloading ${URL}..."
downloader "$URL" -o "${TMPDIR}/${TARBALL}" || err "failed to download ${TARBALL}"
# --- verify checksum ---
# macOS ships /sbin/sha256sum which looks like GNU sha256sum but does NOT
# accept checksum lines on stdin with -c. Use the native shasum on macOS
# and the GNU-style sha256sum on Linux instead.
info "verifying checksum..."
downloader "$CHECKSUMS_URL" -o "${TMPDIR}/checksums.txt" 2>/dev/null \
|| warn "could not download checksums file; skipping verification"
if [ -f "${TMPDIR}/checksums.txt" ]; then
case "$OS" in
darwin)
EXPECTED="$(grep " ${TARBALL}$" "${TMPDIR}/checksums.txt" | awk '{print $1}')"
ACTUAL="$(shasum -a 256 "${TMPDIR}/${TARBALL}" | awk '{print $1}')"
[ "$EXPECTED" = "$ACTUAL" ] || err "checksum mismatch! expected ${EXPECTED}, got ${ACTUAL}"
;;
linux)
# filter the single line we need so sha256sum -c only checks our file
grep " ${TARBALL}$" "${TMPDIR}/checksums.txt" > "${TMPDIR}/checksums_filtered.txt"
(cd "$TMPDIR" && sha256sum -c --quiet checksums_filtered.txt 2>/dev/null) \
|| err "checksum verification failed"
;;
esac
ok "checksum verified"
fi
# --- extract & install ---
info "extracting..."
tar -xzf "${TMPDIR}/${TARBALL}" -C "$TMPDIR"
info "installing to ${BINDIR}/aria2s..."
mkdir -p "$BINDIR" 2>/dev/null || true
if [ -w "$BINDIR" ]; then
cp "${TMPDIR}/aria2s" "${BINDIR}/aria2s"
else
sudo mkdir -p "$BINDIR" 2>/dev/null || true
sudo cp "${TMPDIR}/aria2s" "${BINDIR}/aria2s"
fi
chmod +x "${BINDIR}/aria2s"
ok "aria2s ${TAG} installed to ${BINDIR}/aria2s"
# --- post-install ---
printf '\n'
if [ "$OS" = "linux" ] && ! command -v systemctl >/dev/null 2>&1; then
warn "systemctl not found; background service setup requires systemd --user"
info "To set up the service later: aria2s install --start"
info "To open the terminal dashboard: aria2s"
else
info "setting up aria2c background service..."
"${BINDIR}/aria2s" install --start
fi