forked from nxtrace/NTrace-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnt_install.sh
More file actions
435 lines (390 loc) · 12.5 KB
/
nt_install.sh
File metadata and controls
435 lines (390 loc) · 12.5 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#!/bin/sh
# This file is also served at https://nxtrace.org/nt .
# Keep README one-key install snippets in sync with this script.
set -eu
API_HOST="www.nxtrace.org"
API_PATH="/api/dist/core"
# Fallback deliberately targets the stable NTrace-core release channel.
GITHUB_RELEASE_BASE="https://github.com/nxtrace/NTrace-core/releases/latest/download"
DEFAULT_PROTOCOL="https"
DEFAULT_FLAVOR="full"
SYSTEM_BIN_DIR_DEFAULT="/usr/local/bin"
USER_BIN_DIR_DEFAULT="${HOME:-}/.local/bin"
PROTOCOL="${DEFAULT_PROTOCOL}"
FLAVOR="${DEFAULT_FLAVOR}"
INSTALL_MODE="auto"
BIN_DIR=""
ARCH_OVERRIDE=""
USE_SOFTFLOAT=0
DOWNLOADER=""
BIN_NAME=""
TARGET_OS=""
TARGET_ARCH=""
ASSET_NAME=""
INSTALL_PATH=""
TEMP_FILE=""
CANDIDATE_FILE=""
SYSTEM_BIN_DIR="${NT_INSTALL_SYSTEM_BIN_DIR:-${SYSTEM_BIN_DIR_DEFAULT}}"
USER_BIN_DIR="${NT_INSTALL_USER_BIN_DIR:-${USER_BIN_DIR_DEFAULT}}"
info() {
printf '%s\n' "==> $*"
}
warn() {
printf '%s\n' "warning: $*" >&2
}
die() {
printf '%s\n' "error: $*" >&2
exit 1
}
cleanup() {
if [ -n "${TEMP_FILE}" ] && [ -f "${TEMP_FILE}" ]; then
rm -f "${TEMP_FILE}" >/dev/null 2>&1 || true
fi
if [ -n "${CANDIDATE_FILE}" ] && [ -f "${CANDIDATE_FILE}" ]; then
rm -f "${CANDIDATE_FILE}" >/dev/null 2>&1 || true
fi
}
trap cleanup EXIT INT TERM
print_help() {
cat <<'EOF'
NextTrace one-key installer
Usage:
curl -sL https://nxtrace.org/nt | bash
curl -sL https://nxtrace.org/nt | bash -s -- --flavor tiny
curl -sL https://nxtrace.org/nt | bash -s -- --flavor ntr
Options:
--flavor full|tiny|ntr Install the selected release flavor
--user Install to ~/.local/bin
--system Install to /usr/local/bin
--bin-dir <path> Install to a specific directory
--arch <arch> Override detected release arch
--softfloat Use _softfloat asset for linux mips/mipsle
--help Show this help message
Compatibility:
http Legacy compatibility flag. Use http for the API endpoint.
EOF
}
command_exists() {
command -v "$1" >/dev/null 2>&1
}
normalize_arch() {
case "$1" in
amd64|x86_64) printf '%s\n' "amd64" ;;
386|i386|i486|i586|i686) printf '%s\n' "386" ;;
arm64|aarch64) printf '%s\n' "arm64" ;;
armv5|armv5l|armv5tel) printf '%s\n' "armv5" ;;
armv6|armv6l) printf '%s\n' "armv6" ;;
armv7|armv7l|armv7ml|armv8l) printf '%s\n' "armv7" ;;
mips) printf '%s\n' "mips" ;;
mipsel|mipsle) printf '%s\n' "mipsle" ;;
mips64) printf '%s\n' "mips64" ;;
mips64el|mips64le) printf '%s\n' "mips64le" ;;
loongarch64|loong64) printf '%s\n' "loong64" ;;
ppc64) printf '%s\n' "ppc64" ;;
ppc64le) printf '%s\n' "ppc64le" ;;
riscv64) printf '%s\n' "riscv64" ;;
s390x) printf '%s\n' "s390x" ;;
*)
return 1
;;
esac
}
detect_os() {
case "$(uname -s 2>/dev/null || printf 'unknown')" in
Linux) printf '%s\n' "linux" ;;
Darwin) printf '%s\n' "darwin" ;;
FreeBSD) printf '%s\n' "freebsd" ;;
OpenBSD) printf '%s\n' "openbsd" ;;
DragonFly) printf '%s\n' "dragonfly" ;;
*)
return 1
;;
esac
}
detect_arch() {
if [ -n "${ARCH_OVERRIDE}" ]; then
normalize_arch "${ARCH_OVERRIDE}" || die "unsupported arch override: ${ARCH_OVERRIDE}. Use one of the release arch names."
return 0
fi
normalize_arch "$(uname -m 2>/dev/null || printf 'unknown')" || die "unsupported architecture: $(uname -m 2>/dev/null || printf 'unknown'). Use --arch or download the release manually."
}
resolve_bin_name() {
case "${FLAVOR}" in
full) printf '%s\n' "nexttrace" ;;
tiny) printf '%s\n' "nexttrace-tiny" ;;
ntr) printf '%s\n' "ntr" ;;
*)
die "unsupported flavor: ${FLAVOR}"
;;
esac
}
resolve_asset_name() {
if [ "${TARGET_OS}" = "darwin" ]; then
printf '%s\n' "${BIN_NAME}_darwin_universal"
return 0
fi
asset="${BIN_NAME}_${TARGET_OS}_${TARGET_ARCH}"
if [ "${USE_SOFTFLOAT}" -eq 1 ]; then
case "${TARGET_OS}:${TARGET_ARCH}" in
linux:mips|linux:mipsle)
asset="${asset}_softfloat"
;;
*)
die "--softfloat is only supported for linux mips/mipsle."
;;
esac
fi
printf '%s\n' "${asset}"
}
pick_downloader() {
if command_exists curl; then
DOWNLOADER="curl"
return 0
fi
if command_exists wget; then
DOWNLOADER="wget"
return 0
fi
die "curl or wget is required to download NextTrace."
}
fetch_text() {
url="$1"
if [ "${DOWNLOADER}" = "curl" ]; then
curl -fsSL --connect-timeout 10 --max-time 30 "${url}"
return $?
fi
wget -q -O - --timeout=10 "${url}"
}
download_file() {
url="$1"
output="$2"
if [ "${DOWNLOADER}" = "curl" ]; then
curl -fsSL --connect-timeout 10 --max-time 180 --retry 3 --retry-delay 1 -o "${output}" "${url}"
return $?
fi
wget -q -O "${output}" --timeout=10 --tries=3 --waitretry=1 "${url}"
}
mkdir_or_die() {
dir="$1"
label="$2"
if [ -d "${dir}" ]; then
return 0
fi
if ! mkdir -p "${dir}" >/dev/null 2>&1; then
die "unable to create ${label}: ${dir}"
fi
}
ensure_writable_dir() {
dir="$1"
label="$2"
mkdir_or_die "${dir}" "${label}"
[ -w "${dir}" ] || die "${label} is not writable: ${dir}"
}
can_use_dir() {
dir="$1"
if [ ! -d "${dir}" ]; then
mkdir -p "${dir}" >/dev/null 2>&1 || return 1
fi
[ -w "${dir}" ]
}
find_existing_binary() {
existing="$(command -v "${BIN_NAME}" 2>/dev/null || true)"
if [ -n "${existing}" ] && [ -f "${existing}" ]; then
printf '%s\n' "${existing}"
fi
}
resolve_install_path() {
case "${INSTALL_MODE}" in
system)
ensure_writable_dir "${SYSTEM_BIN_DIR}" "system install directory"
printf '%s/%s\n' "${SYSTEM_BIN_DIR}" "${BIN_NAME}"
return 0
;;
user)
[ -n "${HOME:-}" ] || die "HOME is not set. Use --bin-dir or --system."
ensure_writable_dir "${USER_BIN_DIR}" "user install directory"
printf '%s/%s\n' "${USER_BIN_DIR}" "${BIN_NAME}"
return 0
;;
auto)
existing="$(find_existing_binary)"
if [ -n "${existing}" ]; then
existing_dir="$(dirname "${existing}")"
if [ -w "${existing_dir}" ]; then
printf '%s\n' "${existing}"
return 0
fi
die "existing ${BIN_NAME} at ${existing} is not writable. Re-run with sudo, or use --user / --bin-dir."
fi
if can_use_dir "${SYSTEM_BIN_DIR}"; then
printf '%s/%s\n' "${SYSTEM_BIN_DIR}" "${BIN_NAME}"
return 0
fi
[ -n "${HOME:-}" ] || die "HOME is not set. Use --bin-dir or run with sudo for a system install."
ensure_writable_dir "${USER_BIN_DIR}" "user install directory"
printf '%s/%s\n' "${USER_BIN_DIR}" "${BIN_NAME}"
return 0
;;
*)
die "unknown install mode: ${INSTALL_MODE}"
;;
esac
}
append_candidate() {
url="$1"
if [ -n "${url}" ]; then
printf '%s\n' "${url}"
fi
}
build_candidate_list() {
api_url="${PROTOCOL}://${API_HOST}${API_PATH}/${ASSET_NAME}"
response="$(fetch_text "${api_url}" 2>/dev/null || true)"
if [ -n "${response}" ]; then
old_ifs="${IFS}"
had_noglob=0
case "$-" in
*f*)
had_noglob=1
;;
*)
set -f
;;
esac
IFS='|'
# Intentionally using word-splitting with IFS='|' to split the mirror list into candidates.
# shellcheck disable=SC2086
set -- ${response}
IFS="${old_ifs}"
if [ "${had_noglob}" -eq 0 ]; then
set +f
fi
for candidate in "$@"; do
append_candidate "${candidate}"
done
else
warn "unable to fetch mirror list from ${api_url}, falling back to GitHub release."
fi
append_candidate "${GITHUB_RELEASE_BASE}/${ASSET_NAME}"
}
path_in_path_env() {
needle="$1"
case ":${PATH:-}:" in
*:"${needle}":*)
return 0
;;
*)
return 1
;;
esac
}
verify_temp_binary() {
[ -s "${TEMP_FILE}" ] || return 1
chmod 0755 "${TEMP_FILE}" >/dev/null 2>&1 || return 1
"${TEMP_FILE}" --version >/dev/null 2>&1
}
download_and_install() {
install_dir="$(dirname "${INSTALL_PATH}")"
mkdir_or_die "${install_dir}" "install directory"
TEMP_FILE="$(mktemp "${install_dir}/.${BIN_NAME}.tmp.XXXXXX")" || die "unable to create temporary file in ${install_dir}"
candidate_file="$(mktemp "${install_dir}/.${BIN_NAME}.urls.XXXXXX")" || die "unable to create candidate list in ${install_dir}"
CANDIDATE_FILE="${candidate_file}"
build_candidate_list >"${candidate_file}"
downloaded_from=""
while IFS= read -r candidate; do
[ -n "${candidate}" ] || continue
if download_file "${candidate}" "${TEMP_FILE}" >/dev/null 2>&1; then
if verify_temp_binary; then
downloaded_from="${candidate}"
break
fi
fi
rm -f "${TEMP_FILE}" >/dev/null 2>&1 || true
TEMP_FILE="$(mktemp "${install_dir}/.${BIN_NAME}.tmp.XXXXXX")" || die "unable to recreate temporary file in ${install_dir}"
done <"${candidate_file}"
rm -f "${candidate_file}" >/dev/null 2>&1 || true
CANDIDATE_FILE=""
[ -n "${downloaded_from}" ] || die "failed to download a working ${ASSET_NAME}. Try again later or install from the release page manually."
mv -f "${TEMP_FILE}" "${INSTALL_PATH}" || die "failed to move ${BIN_NAME} into place: ${INSTALL_PATH}"
TEMP_FILE=""
info "Downloaded from: ${downloaded_from}"
}
print_post_install() {
install_dir="$(dirname "${INSTALL_PATH}")"
info "Flavor: ${FLAVOR}"
info "Installed: ${INSTALL_PATH}"
info "Command: ${BIN_NAME}"
info "Version check:"
"${INSTALL_PATH}" --version || die "installed binary failed version check: ${INSTALL_PATH}"
if ! path_in_path_env "${install_dir}"; then
warn "${install_dir} is not in PATH."
printf '%s\n' "Add it with: export PATH=\"${install_dir}:\$PATH\""
fi
printf '%s\n' "Run: ${BIN_NAME} --help"
}
parse_args() {
while [ $# -gt 0 ]; do
case "$1" in
http)
PROTOCOL="http"
shift
;;
--flavor)
[ $# -ge 2 ] || die "--flavor requires a value"
FLAVOR="$2"
shift 2
;;
--user)
[ "${INSTALL_MODE}" = "auto" ] || die "choose only one of --user / --system / --bin-dir"
INSTALL_MODE="user"
shift
;;
--system)
[ "${INSTALL_MODE}" = "auto" ] || die "choose only one of --user / --system / --bin-dir"
INSTALL_MODE="system"
shift
;;
--bin-dir)
[ $# -ge 2 ] || die "--bin-dir requires a path"
[ "${INSTALL_MODE}" = "auto" ] || die "choose only one of --user / --system / --bin-dir"
BIN_DIR="$2"
INSTALL_MODE="bin-dir"
shift 2
;;
--arch)
[ $# -ge 2 ] || die "--arch requires a value"
ARCH_OVERRIDE="$2"
shift 2
;;
--softfloat)
USE_SOFTFLOAT=1
shift
;;
--help|-h)
print_help
exit 0
;;
*)
die "unknown argument: $1"
;;
esac
done
}
main() {
parse_args "$@"
TARGET_OS="$(detect_os)" || die "unsupported operating system: $(uname -s 2>/dev/null || printf 'unknown'). Please download the release manually."
TARGET_ARCH="$(detect_arch)"
BIN_NAME="$(resolve_bin_name)"
ASSET_NAME="$(resolve_asset_name)"
pick_downloader
if [ "${INSTALL_MODE}" = "bin-dir" ]; then
ensure_writable_dir "${BIN_DIR}" "install directory"
INSTALL_PATH="${BIN_DIR}/${BIN_NAME}"
else
INSTALL_PATH="$(resolve_install_path)"
fi
info "Preparing ${ASSET_NAME}"
info "Installing to ${INSTALL_PATH}"
download_and_install
print_post_install
}
main "$@"